PluginProbe
CSS & JavaScript Toolbox / 11.2
CSS & JavaScript Toolbox v11.2
trunk 0.3 0.8 10 10.1 11 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.9.1 12 12.0 12.0.1 12.0.3 12.0.4 12.0.5 12.0.6 12.0.7 6.0 6.0.11 All 60 releases
css-javascript-toolbox / framework / ServicesFW / Form.class.php

Form.class.php in CSS & JavaScript Toolbox 11.2, at framework/ServicesFW/Form.class.php

293 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 */
5
6
7 /**
8 *
9 */
10 class CJTServicesForm
11 {
12
13 /**
14 * put your comment there...
15 *
16 * @var mixed
17 */
18 protected $data = array();
19
20 /**
21 * put your comment there...
22 *
23 * @var mixed
24 */
25 protected $definition = array();
26
27 /**
28 * put your comment there...
29 *
30 * @var mixed
31 */
32 protected $name;
33
34 /**
35 * put your comment there...
36 *
37 * @param mixed $name
38 * @param mixed $data
39 * @return CJTServicesForm
40 */
41 public function __construct( $name, $data = null )
42 {
43
44 $this->name = $name;
45
46 if ( is_array( $data ) )
47 {
48 $this->setArray( $data );
49 }
50
51 }
52
53 /**
54 * put your comment there...
55 *
56 * @param mixed $value
57 */
58 private function _type_array( $value )
59 {
60 if ( ! is_array( $value ) )
61 {
62 $value = array();
63 }
64
65 return $value;
66 }
67
68 /**
69 * put your comment there...
70 *
71 * @param mixed $value
72 */
73 private function _type_boolean( $value )
74 {
75 return ( bool ) $value;
76 }
77
78 /**
79 * put your comment there...
80 *
81 * @param mixed $value
82 */
83 private function _type_float( $value )
84 {
85 return ( float ) $value;
86 }
87
88 /**
89 * put your comment there...
90 *
91 * @param mixed $value
92 */
93 private function _type_integer( $value )
94 {
95 return intval( $value );
96 }
97
98 /**
99 * put your comment there...
100 *
101 * @param mixed $value
102 */
103 private function _type_string( $value )
104 {
105 return strval( $value );
106 }
107
108 /**
109 * put your comment there...
110 *
111 */
112 protected function apply()
113 {
114
115 $definitionsGroups = array_chunk( $this->definition, 2 );
116
117 foreach ( $definitionsGroups as $groupDef )
118 {
119
120 $groupDefName = $groupDef[ 0 ];
121
122 foreach ( $groupDef[ 1 ] as $name => $definition )
123 {
124
125 # Set containers/groups if not set
126 $groupNames = explode( '.', $groupDefName );
127
128
129 $dataCopy = $this->data;
130 $pointer =& $dataCopy;
131
132 foreach ( $groupNames as $groupName )
133 {
134 # For now lets use array for any doesnt exists container
135 if ( ! isset( $pointer[ $groupName ] ) )
136 {
137 $pointer[ $groupName ] = array();
138 }
139
140 $pointer =& $pointer[ $groupName ];
141
142 }
143
144 # Set cast value
145 $typeHandler = "_type_{$definition[ 'type' ]}";
146 $value = $this->getValue( "{$groupDefName}.{$name}" );
147
148 $pointer[ $name ] = $this->$typeHandler( $value );
149
150 $this->data = $dataCopy;
151 }
152
153 }
154 return $this;
155 }
156
157 /**
158 * put your comment there...
159 *
160 * @param mixed $definition
161 */
162 public function & define( $definitions )
163 {
164
165 $this->definition = func_get_args();
166
167 return $this;
168 }
169
170 /**
171 * put your comment there...
172 *
173 */
174 public function getArray()
175 {
176 return $this->data;
177 }
178
179 /**
180 * put your comment there...
181 *
182 * @param mixed $name
183 */
184 protected function & getElement( $path )
185 {
186 $pointer =& $this->data;
187
188 $names = explode( '.', $path );
189
190 foreach ( $names as $name )
191 {
192
193 if ( ! isset( $pointer[ $name ] ) )
194 {
195 $pointer = null;
196
197 return $pointer;
198 }
199
200 $pointer =& $pointer[ $name ];
201 }
202
203 return $pointer;
204 }
205
206 /**
207 * put your comment there...
208 *
209 * @param mixed $path
210 */
211 public function getElementId( $path )
212 {
213
214 $elementId = "{$this->name}-" . str_replace( '.', '-', $path );
215
216 return $elementId;
217 }
218
219 /**
220 * put your comment there...
221 *
222 * @param mixed $path
223 */
224 public function getElementName( $path )
225 {
226
227 $elementName = "{$this->name}[" . str_replace( '.', '][', $path ) . ']';
228
229 return $elementName;
230 }
231
232 /**
233 * put your comment there...
234 *
235 */
236 public function getName()
237 {
238 return $this->name;
239 }
240
241 /**
242 * put your comment there...
243 *
244 * @param mixed $path
245 */
246 public function getValue( $path )
247 {
248 return $this->getElement( $path );
249 }
250
251 /**
252 * put your comment there...
253 *
254 * @param mixed $data
255 */
256 public function & setArray( $data )
257 {
258
259 $this->data = $data;
260
261 # Apply definition ( e.g cast values if needed )
262 $this->apply();
263
264 return $this;
265 }
266
267 /**
268 * put your comment there...
269 *
270 * @param mixed $path
271 * @param mixed $data
272 */
273 public function & setValue( $path, $value )
274 {
275
276 $element =& $this->getElement( $path );
277
278 $element = $value;
279
280 return $this;
281 }
282
283 /**
284 * put your comment there...
285 *
286 */
287 public function & validate()
288 {
289 return true;
290 }
291
292 }
293