<?php
/**
* HTML選択オブジェクトクラス
*
* PHP version 5
*
* @package    core
* @author     stk2k <stk2k@sazysoft.com>
* @copyright  2008 stk2k, sazysoft
*/

class HTMLSelectObject extends HTMLLeafObject
{
	private $_choices;
	private $_selected_value;

	/*
	 *	コンストラクタ
	 */
	public function __construct( $attributes = NULL, $choices = array(), $selected_value = NULL )
	{
		parent::__construct( 'select', $attributes );

		$this->_choices = $choices;
		$this->_selected_value = $selected_value;
	}
 
	/*
	 *	オプションタグ出力
	 */
	public function optionTags()
	{
		$conv = EncodingConverter::fromString( 'PHP_TO_HTML' );

		$selected_value = $this->_selected_value;

		$html = '';
		$choices = $this->_choices;
		if ( $choices ){
			foreach( $choices as $key => $value ){
				$key = $conv->convertEncoding( $key );
				$value = $conv->convertEncoding( $value );
				if ( $selected_value && $selected_value === $key ){
					$html .= '<option value="' . $key . '" selected>' . $value . '</option>' . PHP_EOL;
				}
				else{
					$html .= '<option value="' . $key . '">' . $value . '</option>' . PHP_EOL;
				}
			}
		}
		return $html;
	}

	/*
	 *	HTML出力
	 */
	public function toHTML()
	{
		// 開始タグ
		$html = $this->startTag() . PHP_EOL;

		// オプションの出力
		$html .= $this->optionTags();

		// 終了タグ
		$html .= $this->endTag() . PHP_EOL;

		return $html;
	}
  
}
