<?php
/*--------------------------------------------------------- 
 * File:radioButtonList.php
 * Author:Tomix
 * Description: 
 * Created:2009
 * -------------------------------------------------------
 * Modified history:2023/08/23
 * Copyright © 2023 蘋果世界股份有限公司 版權所有
 * -------------------------------------------------------
 */
class radioButtonList {
	public string $id = "radioButtonList1";	// ID
	public string $name;					// 名稱
	public string $cssClass;				// 樣式
	public string $valueField;				// 值字段
	public string $titleField;				// 標題(文本)字段
	public array $dataSource = [];			// 數據源
	public string $checkedValue;			// 當前選中值

	public function getContent(): string {
		$text = "";
		if (!is_array($this->dataSource)) return "Data source is not a array!";

		foreach ($this->dataSource as $row) {
			$text .= $this->buildListItem($row) . "\r\n";
		}
		return $text;
	}

	public function render(): void {
		echo $this->getContent();
	}

	private function buildListItem($row): string {
		return ($row[$this->valueField] === $this->checkedValue)
			? "<input type=\"radio\" name=\"{$this->name}\" value=\"{$row[$this->valueField]}\" checked=\"checked\" /><label>{$row[$this->titleField]}</label>"
			: "<input type=\"radio\" name=\"{$this->name}\" value=\"{$row[$this->valueField]}\" /><label>{$row[$this->titleField]}</label>";
	}
}
?>