Hi all,
I am trying to find some clever way to define all kinds of HTML selection lists from my domain model collections. Because its close to christmas my example here will have a "sweets" domain model instead of the standard car/product whatever example
A collection of sweets if fetched by a datamapper from the databasePHP Code:class Sweets {
public $id,$description,$calories;
public function __construct($id,$desc,$calories='too many') {
$this->id = $id;
$this->description = $desc;
$this->calories = $calories;
}
}
So far this is pretty standard and has nothing to do with selection lists or HTMLPHP Code:class FakeDataMapper {
// stuff here ...
/**
* @return array
*/
public function fetchAll() {
// Usually SQL ;-)
$list = array(
new Sweets('1','Chocolade'),
new Sweets('2','Nuts'),
new Sweets('3','Cake')
);
return $list;
}
// ... stuff there
}
But now I want to have a dropdown list from my collection to display in a form. What would be a pattern or solution to achieve that?
Right now I have a ThesaurusList class which takes a collection of domain models. This collection is transformed into an assoc. array.
In the real world, sweets would implement an interface to make sure that there is a getId() and getDesc() method on the model.PHP Code:class ThesaurusList implements ThesaurusListInterface {
/**
* @var array
*/
protected $list = array();
public function setList(Array $array) {
foreach ($array as $sweets) {
$this->list[$sweets->id] = $sweets->description;
}
}
public function getList() {
return $this->list;
}
}
The transformed object collection is passed to a "visualizer" class which takes a ThesaurusList as an argument in the constructor
I could create as many "visualizer" classes as needed. They all would work with the ThesaurusList class to create the desired output. Each visualizer class would take a number of other display and html arguments which are needed to create the HTML script for the selection element. Id's, classes, styles, default values and so on would be set before ->setHtml is called.PHP Code:class DropDown implements FormSelector {
/**
* @var ThesaurusList
*/
protected $list;
protected $id,$name = '';
public function __construct(ThesaurusListInterface $list) {
$this->list = $list;
}
// ... other methods
public function toHTML() {
$html = '<select id="'.$this->id.'" name="'.$this->name.'" size="1">';
foreach ($this->list->getList() as $id => $name) {
$html .= '<option value="'.$id.'">'.$name.'</option>';
}
$html .= '</select>';
return $html;
}
}
class RadioButton implements FormSelector {
// make radio buttons
}
Is it ok to create my select lists like that? Any drawbacks that you can see? Are there some fancy patterns which might help me with my task? I had the impression that the visitor pattern might help me with that, but the examples I found in a book and on wikipedia are too much like an example - I simply can't transfer what I see there to my codePHP Code:<?php
$fm = new FakeDataMapper();
$list = new ThesaurusList();
$list->setList($fm->fetchAll());
$dropdown = new DropDown($list);
?>
<form>
<p>Select Field</p>
<?php echo $dropdown->toHTML(); ?>
</form>This happens when geographers mess with things they never really learned
So any tips and help are greatly appreciated - take some sweets![]()




This happens when geographers mess with things they never really learned 


Bookmarks