Luego viene el archivo "core" que es el control en sí:
Select.php:
Código PHP:
<?php
class Select {
private $name = '';
private $selected = '';
private $currSelected = '';
private $html;
private $ds = null;
protected $selectopen = '<select name="%s">';
protected $selectclose = '</select>';
protected $option = '<option value="%s">%s</option>';
protected $optionselected = '<option value="%s" selected="selected">%s</option>';
protected $selectLabel = ' - Selecciona - ';
public function __construct($name) {
$this->name = $name;
if( isset( $_REQUEST[$name] ) ) {
$this->currSelected = $_REQUEST[$name];
$this->selected = $_REQUEST[$name];
}
}
public function setSelected( $selected ) {
$this->selected = $selected;
}
public function setDataSource( DataSourceInterface $ds ) {
$this->ds = $ds;
}
public function getDataSource() {
return $this->ds;
}
public function getControl() {
return $this->html;
}
public function getName() {
return $this->name;
}
public function getSelectedValue() {
return $this->currSelected;
}
public function getSelectedLabel() {
$data = $this->ds->getData();
return $data[$this->currSelected];
}
public function buildControl() {
if( $this->ds == null ) {
throw new Exception( 'No hay ninguna fuente de datos disponible, favor de llamar a Select::setDataSource() antes de llamar a esta función' );
}
$data = $this->ds->getData();
$options = array();
$options[] = sprintf( $this->option, "", $this->selectLabel );
foreach( $data as $val => $label ) {
if( $val == $this->selected ) {
$template = $this->optionselected;
} else {
$template = $this->option;
}
$options[] = sprintf( $template, $val, $label );
}
$html = sprintf( $this->selectopen, $this->name );
$html.= implode( "\n", $options );
$html.= $this->selectclose;
$this->html = $html;
}
}
?>
Espero con esto se den una idea un poco básica de la forma de separar los datos de la vista y que es uno de los principios básicos de la POO.
Saludos.