1 | <?php |
||
13 | abstract class Field |
||
14 | { |
||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | const TYPE = 'type'; |
||
19 | |||
20 | /** |
||
21 | * @var CrudFields |
||
22 | */ |
||
23 | protected $fields; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $identifier; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $label; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $placeholder; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $rules; |
||
44 | |||
45 | /** |
||
46 | * Field constructor. |
||
47 | * |
||
48 | * @param string $identifier |
||
49 | * @param array|string $rules |
||
50 | */ |
||
51 | public function __construct($identifier, $rules = null) |
||
60 | |||
61 | /** |
||
62 | * Constructs staticly. |
||
63 | * |
||
64 | * @param string $idenfitier |
||
65 | * @param null|string|array $rules |
||
66 | * @return static |
||
67 | */ |
||
68 | public static function handle($idenfitier, $rules = null) |
||
72 | |||
73 | |||
74 | /** |
||
75 | * Add validation rules to the field. |
||
76 | * |
||
77 | * @param string|array $rules |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function withRules($rules) |
||
106 | |||
107 | /** |
||
108 | * Add a validation rule. |
||
109 | * |
||
110 | * @param string $rule |
||
111 | * @return self |
||
112 | */ |
||
113 | public function addRule($rule) |
||
119 | |||
120 | /** |
||
121 | * @param CrudFields $fields |
||
122 | * @return self |
||
123 | */ |
||
124 | public function setFields(CrudFields $fields) |
||
130 | |||
131 | /** |
||
132 | * Get the field identifier. |
||
133 | * |
||
134 | * @param void |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getIdentifier() |
||
141 | |||
142 | /** |
||
143 | * Set a custom label for the field. |
||
144 | * |
||
145 | * @param string $name |
||
146 | * @return self |
||
147 | */ |
||
148 | public function withLabel($name) |
||
154 | |||
155 | /** |
||
156 | * Defines a placeholder for the field. |
||
157 | * |
||
158 | * @param string $placeholder |
||
159 | * @return self |
||
160 | */ |
||
161 | public function withPlaceholder($placeholder) |
||
167 | |||
168 | /** |
||
169 | * Appends an help message to the input. |
||
170 | * |
||
171 | * @param string $help |
||
172 | * @return self |
||
173 | */ |
||
174 | public function withHelp($help) |
||
180 | |||
181 | /** |
||
182 | * Render the field form. |
||
183 | * |
||
184 | * @param void |
||
185 | * @return string |
||
186 | */ |
||
187 | public function form() |
||
191 | |||
192 | /** |
||
193 | * Get the form view. |
||
194 | * |
||
195 | * @param void |
||
196 | * @return View |
||
197 | */ |
||
198 | protected function getForm() |
||
202 | |||
203 | /** |
||
204 | * Get the field view name. |
||
205 | * |
||
206 | * @param void |
||
207 | * @return string |
||
208 | */ |
||
209 | abstract public function getViewName(); |
||
210 | |||
211 | /** |
||
212 | * Returns additionnal variables to the views. |
||
213 | * |
||
214 | * @param void |
||
215 | * @return array |
||
216 | */ |
||
217 | protected function getViewVariables() |
||
221 | |||
222 | /** |
||
223 | * Returns all base variables for the view. |
||
224 | * |
||
225 | * @param void |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function getViewBaseVariables() |
||
246 | |||
247 | /** |
||
248 | * Checks if the field has an error. |
||
249 | * |
||
250 | * @param void |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function hasError() |
||
257 | |||
258 | /** |
||
259 | * Returns the error. |
||
260 | * |
||
261 | * @param void |
||
262 | * @return null|string |
||
263 | */ |
||
264 | public function getError() |
||
273 | |||
274 | /** |
||
275 | * Returns the field's placeholder. |
||
276 | * |
||
277 | * @param void |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getPlaceholder() |
||
289 | |||
290 | /** |
||
291 | * Returns the field's help. |
||
292 | * |
||
293 | * @param void |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getHelp() |
||
305 | |||
306 | /** |
||
307 | * Checks if the field has a previous value. |
||
308 | * |
||
309 | * @param void |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function hasOld() |
||
316 | |||
317 | /** |
||
318 | * Returns the old value. |
||
319 | * |
||
320 | * @param void |
||
321 | * @return string|null |
||
322 | */ |
||
323 | public function getOld() |
||
332 | |||
333 | /** |
||
334 | * Returns the field's label. |
||
335 | * |
||
336 | * @param void |
||
337 | * @return string |
||
338 | */ |
||
339 | public function getLabel() |
||
348 | |||
349 | /** |
||
350 | * Get the field value. |
||
351 | * |
||
352 | * @param void |
||
353 | * @return mixed |
||
354 | */ |
||
355 | public function getValue() |
||
364 | |||
365 | /** |
||
366 | * Get the value to be displayed on a table. |
||
367 | * |
||
368 | * @param void |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function getTableValue() |
||
375 | |||
376 | /** |
||
377 | * Set a new value to the model. |
||
378 | * |
||
379 | * @param mixed $value |
||
380 | * @return self |
||
381 | */ |
||
382 | public function newValue($value) |
||
388 | |||
389 | /** |
||
390 | * @param void |
||
391 | * @return string |
||
392 | */ |
||
393 | public function getRules() |
||
397 | |||
398 | /** |
||
399 | * Return fields specific scripts files from public folder. |
||
400 | * Example: ['js/field.js'] |
||
401 | * |
||
402 | * @param void |
||
403 | * @return array |
||
404 | */ |
||
405 | public function getScripts() |
||
409 | |||
410 | /** |
||
411 | * Return fields specific stylesheets files from public folder. |
||
412 | * Example: ['css/field.css'] |
||
413 | * |
||
414 | * @param void |
||
415 | * @return array |
||
416 | */ |
||
417 | public function getCss() |
||
421 | } |
||
422 |
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: