/[drupal]/contributions/sandbox/crell/fapioo.php
ViewVC logotype

Contents of /contributions/sandbox/crell/fapioo.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Tue Sep 18 00:05:27 2007 UTC (2 years, 2 months ago) by crell
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +7 -4 lines
File MIME type: text/x-php
Added node OO brainstorm file
1 <?php
2
3 class FormElement implements ArrayAccess, IteratorAggregate {
4
5 protected $properties = array();
6 protected $children = array();
7
8 public function __construct(Array $properties = array()) {
9 foreach ($properties as $key => $value) {
10 $this[$key] = $value;
11 }
12 }
13
14 function offsetExists($offset) {
15 return isset($this->properties[$offset]);
16 }
17
18 function offsetGet($offset) {
19 return $this->properties[$offset];
20 }
21
22 function offsetSet($offset, $value) {
23 return $this->properties[$offset] = $value;
24 }
25
26 function offsetUnset($offset) {
27 unset($this->properties[$offset]);
28 }
29
30 function getIterator() {
31 return new FormElementIterator($this->children);
32 }
33
34 function __get($var) {
35 return $this->children[$var];
36 }
37
38 function __set($var, $val) {
39 return $this->children[$var] = $val;
40 }
41
42 function __isset($var) {
43 return isset($this->children[$var]);
44 }
45
46 function __unset($var) {
47 unset($this->children[$var]);
48 }
49
50 }
51
52 class FormElementIterator implements Iterator {
53 private $var = array();
54
55 public function __construct($array) {
56 if (is_array($array)) {
57 $this->var = $array;
58 }
59 }
60
61 public function rewind() {
62 reset($this->var);
63 }
64
65 public function current() {
66 $var = current($this->var);
67 return $var;
68 }
69
70 public function key() {
71 $var = key($this->var);
72 return $var;
73 }
74
75 public function next() {
76 $var = next($this->var);
77 return $var;
78 }
79
80 public function valid() {
81 $var = $this->current() !== false;
82 return $var;
83 }
84 }
85
86
87 class Form extends FormElement {
88
89 protected $hasErrors = FALSE;
90
91 static function load($form_id) {
92 $form = new Form(array(), $form_id);
93 call_user_func('form_'. $form_id, $form);
94 // drupal_alter($form, ...);
95 return $form;
96 }
97
98 public function __construct(Array $properties, $form_id) {
99 parent::__construct($properties);
100 $this['submit'] = array('form_'. $form_id . '_submit');
101 $this['validate'] = array('form_'. $form_id . '_validate');
102 }
103
104 public function execute() {
105 $this->validate();
106 if ($this->hasErrors) {
107 // Do some redesplay stuff
108 }
109 else {
110 $this->submit();
111 }
112 }
113
114 public function setError(FormElement $field, $error) {
115 $this->hasErrors = TRUE;
116 $field['error'] = $error;
117 }
118
119 protected function validate() {
120 foreach ($this['validate'] as $function) {
121 call_user_func($function, $this);
122 }
123 }
124
125 protected function submit() {
126 foreach ($this['submit'] as $function) {
127 call_user_func($function, $this);
128 }
129 }
130 }
131
132 function form_foo(Form $form) {
133
134 $form->title = new FormElement(array(
135 'title' => 'Title',
136 'type' => 'textfield',
137 'default_value' => 'My title',
138 ));
139
140 $form->bar = new FormElement();
141 $form->bar['title'] = 'Subtitle';
142 $form->bar['type'] = 'textfield';
143
144 $form->baz = new FormElement();
145 $form->baz['title'] = 'Another';
146 $form->baz['type'] = 'textfield';
147
148 }
149
150 function form_foo_validate(Form $form) {
151 if(! $form->title['value']) {
152 $form->setError($form->title, 'You suck');
153 }
154 }
155
156 function form_foo_submit(Form $form) {
157 foreach ($form as $key => $element) {
158 print $key .': '. $element['title'] .': '. $element['value'] . PHP_EOL;
159 }
160 }
161
162 class TextField extends FormElement {
163
164 function __construct(Array $properties = array()) {
165 $properties += array(
166 'type' => 'textfield',
167 'size' => 20,
168 'maxlength' => 30,
169 );
170 parent::__construct($properties);
171 }
172 }
173
174 class TestForm extends Form {
175
176 function __construct(Array $properties = array()) {
177 parent::__construct($properties, __CLASS__);
178
179 $this->title = new TextField(array(
180 'title' => 'My title',
181 'length' => 20,
182 ));
183 }
184
185 }
186
187
188 $form = Form::load('foo');
189
190 foreach ($form as $child) {
191 print $child['title'] . PHP_EOL;
192 }
193
194 $form->title['value'] = 'I like OOP';
195
196 $form->execute();
197
198 // $form->render() &c is left as an exercise to the reader.

  ViewVC Help
Powered by ViewVC 1.1.2