1 | <?php |
||
13 | abstract class Field |
||
14 | { |
||
15 | /** |
||
16 | * @var CrudFields |
||
17 | */ |
||
18 | protected $fields; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $identifier; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $label; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $placeholder; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $rules; |
||
39 | |||
40 | /** |
||
41 | * Field constructor. |
||
42 | * |
||
43 | * @param string $identifier |
||
44 | * @param array|string $rules |
||
45 | */ |
||
46 | public function __construct($identifier, $rules = null) |
||
55 | |||
56 | /** |
||
57 | * Constructs staticly. |
||
58 | * |
||
59 | * @param string $idenfitier |
||
60 | * @param null|string|array $rules |
||
61 | * @return static |
||
62 | */ |
||
63 | public static function handle($idenfitier, $rules = null) |
||
67 | |||
68 | |||
69 | /** |
||
70 | * Add validation rules to the field. |
||
71 | * |
||
72 | * @param string|array $rules |
||
73 | * @return mixed |
||
74 | */ |
||
75 | public function withRules($rules) |
||
101 | |||
102 | /** |
||
103 | * Add a validation rule. |
||
104 | * |
||
105 | * @param string $rule |
||
106 | * @return self |
||
107 | */ |
||
108 | public function addRule($rule) |
||
114 | |||
115 | /** |
||
116 | * @param CrudFields $fields |
||
117 | * @return self |
||
118 | */ |
||
119 | public function setFields(CrudFields $fields) |
||
125 | |||
126 | /** |
||
127 | * Get the field identifier. |
||
128 | * |
||
129 | * @param void |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getIdentifier() |
||
136 | |||
137 | /** |
||
138 | * Set a custom label for the field. |
||
139 | * |
||
140 | * @param string $name |
||
141 | * @return self |
||
142 | */ |
||
143 | public function withLabel($name) |
||
149 | |||
150 | /** |
||
151 | * Defines a placeholder for the field. |
||
152 | * |
||
153 | * @param string $placeholder |
||
154 | * @return self |
||
155 | */ |
||
156 | public function withPlaceholder($placeholder) |
||
162 | |||
163 | /** |
||
164 | * Appends an help message to the input. |
||
165 | * |
||
166 | * @param string $help |
||
167 | * @return self |
||
168 | */ |
||
169 | public function withHelp($help) |
||
175 | |||
176 | /** |
||
177 | * Render the field form. |
||
178 | * |
||
179 | * @param void |
||
180 | * @return string |
||
181 | */ |
||
182 | public function form() |
||
186 | |||
187 | /** |
||
188 | * Get the form view. |
||
189 | * |
||
190 | * @param void |
||
191 | * @return View |
||
192 | */ |
||
193 | protected function getForm() |
||
197 | |||
198 | /** |
||
199 | * Get the field view name. |
||
200 | * |
||
201 | * @param void |
||
202 | * @return string |
||
203 | */ |
||
204 | abstract public function getViewName(); |
||
205 | |||
206 | /** |
||
207 | * Returns additionnal variables to the views. |
||
208 | * |
||
209 | * @param void |
||
210 | * @return array |
||
211 | */ |
||
212 | protected function getViewVariables() |
||
216 | |||
217 | /** |
||
218 | * Returns all base variables for the view. |
||
219 | * |
||
220 | * @param void |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function getViewBaseVariables() |
||
241 | |||
242 | /** |
||
243 | * Checks if the field has an error. |
||
244 | * |
||
245 | * @param void |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function hasError() |
||
252 | |||
253 | /** |
||
254 | * Returns the error. |
||
255 | * |
||
256 | * @param void |
||
257 | * @return null|string |
||
258 | */ |
||
259 | public function getError() |
||
268 | |||
269 | /** |
||
270 | * Returns the field's placeholder. |
||
271 | * |
||
272 | * @param void |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getPlaceholder() |
||
284 | |||
285 | /** |
||
286 | * Returns the field's help. |
||
287 | * |
||
288 | * @param void |
||
289 | * @return string |
||
290 | */ |
||
291 | public function getHelp() |
||
300 | |||
301 | /** |
||
302 | * Checks if the field has a previous value. |
||
303 | * |
||
304 | * @param void |
||
305 | * @return bool |
||
306 | */ |
||
307 | public function hasOld() |
||
311 | |||
312 | /** |
||
313 | * Returns the old value. |
||
314 | * |
||
315 | * @param void |
||
316 | * @return string|null |
||
317 | */ |
||
318 | public function getOld() |
||
327 | |||
328 | /** |
||
329 | * Returns the field's label. |
||
330 | * |
||
331 | * @param void |
||
332 | * @return string |
||
333 | */ |
||
334 | public function getLabel() |
||
343 | |||
344 | /** |
||
345 | * Get the field value. |
||
346 | * |
||
347 | * @param void |
||
348 | * @return mixed |
||
349 | */ |
||
350 | public function getValue() |
||
354 | |||
355 | /** |
||
356 | * Get the value to be displayed on a table. |
||
357 | * |
||
358 | * @param void |
||
359 | * @return mixed |
||
360 | */ |
||
361 | public function getTableValue() |
||
365 | |||
366 | /** |
||
367 | * Set a new value to the model. |
||
368 | * |
||
369 | * @param mixed $value |
||
370 | * @return self |
||
371 | */ |
||
372 | public function newValue($value) |
||
378 | |||
379 | /** |
||
380 | * @param void |
||
381 | * @return string |
||
382 | */ |
||
383 | public function getRules() |
||
387 | } |
||
388 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: