| 1 |
<?php
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
class SimpleIterator implements Iterator {
|
| 6 |
private $var = array();
|
| 7 |
|
| 8 |
public function __construct(Array $array) {
|
| 9 |
if (is_array($array)) {
|
| 10 |
$this->var = $array;
|
| 11 |
}
|
| 12 |
}
|
| 13 |
|
| 14 |
public function rewind() {
|
| 15 |
reset($this->var);
|
| 16 |
}
|
| 17 |
|
| 18 |
public function current() {
|
| 19 |
$var = current($this->var);
|
| 20 |
return $var;
|
| 21 |
}
|
| 22 |
|
| 23 |
public function key() {
|
| 24 |
$var = key($this->var);
|
| 25 |
return $var;
|
| 26 |
}
|
| 27 |
|
| 28 |
public function next() {
|
| 29 |
$var = next($this->var);
|
| 30 |
return $var;
|
| 31 |
}
|
| 32 |
|
| 33 |
public function valid() {
|
| 34 |
$var = $this->current() !== false;
|
| 35 |
return $var;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
class Field implements Countable, IteratorAggregate {
|
| 40 |
protected $instances = array();
|
| 41 |
|
| 42 |
function __construct() {
|
| 43 |
|
| 44 |
|
| 45 |
}
|
| 46 |
|
| 47 |
function getIterator() {
|
| 48 |
return new SimpleIterator($this->instances);
|
| 49 |
}
|
| 50 |
|
| 51 |
function count() {
|
| 52 |
return count($instances);
|
| 53 |
}
|
| 54 |
|
| 55 |
function setWidget(FormWidget $widget) {
|
| 56 |
$this->widget = $widget;
|
| 57 |
}
|
| 58 |
|
| 59 |
function render() {
|
| 60 |
return $this->widget($this);
|
| 61 |
}
|
| 62 |
|
| 63 |
|
| 64 |
}
|
| 65 |
|
| 66 |
class TextField extends Field {
|
| 67 |
protected $widget;
|
| 68 |
|
| 69 |
function __construct($title, $module) {
|
| 70 |
parent::__construct($title, $module);
|
| 71 |
$this->setWidget(new FormTextField());
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
class Node implements IteratorAggregate, Countable {
|
| 76 |
protected $fields = array();
|
| 77 |
|
| 78 |
function __construct($nid = NULL, $type = NULL) {
|
| 79 |
if (isset($nid)) {
|
| 80 |
// Do some load stuff here
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
// create a new node of the specified type
|
| 84 |
}
|
| 85 |
|
| 86 |
}
|
| 87 |
|
| 88 |
function load($nid) {
|
| 89 |
if (!isset($nodes[$nid])) {
|
| 90 |
// Load
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
| 94 |
static function create($type) {
|
| 95 |
return new Node(NULL, $type);
|
| 96 |
}
|
| 97 |
|
| 98 |
public function save() {
|
| 99 |
|
| 100 |
}
|
| 101 |
|
| 102 |
public function count() {
|
| 103 |
return count($fields);
|
| 104 |
}
|
| 105 |
|
| 106 |
public function addField(Field $field, $module) {
|
| 107 |
$this->fields[$module][$field->getName()] = $field;
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|
| 111 |
class FormSelect extends FormWidget {
|
| 112 |
|
| 113 |
}
|
| 114 |
|
| 115 |
class TextField extends Field {
|
| 116 |
|
| 117 |
}
|
| 118 |
|
| 119 |
$node = Node::load();
|
| 120 |
$field = new TextField('body', 'node');
|
| 121 |
$field->allowMultiple = TRUE;
|
| 122 |
$field->setValue('mytitle', 0);
|
| 123 |
$field->setWidget(new FormSelect());
|
| 124 |
$node->addField($field);
|
| 125 |
|
| 126 |
$field = new TextField('path', 'path');
|
| 127 |
$field->allowMultiple = FALSE;
|
| 128 |
$field->setValue('about/history');
|
| 129 |
$node->addField($field);
|
| 130 |
|
| 131 |
echo count($node); // 2
|
| 132 |
foreach ($node as $field) {
|
| 133 |
echo count($field);
|
| 134 |
foreach ($field as $instance) {
|
| 135 |
echo $instance->getValue();
|
| 136 |
}
|
| 137 |
}
|
| 138 |
|