1 | <?php |
||
12 | abstract class Field |
||
13 | { |
||
14 | /** |
||
15 | * @var CrudFields |
||
16 | */ |
||
17 | protected $fields; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $identifier; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $label; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $placeholder; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $rules; |
||
38 | |||
39 | /** |
||
40 | * Field constructor. |
||
41 | * |
||
42 | * @param string $identifier |
||
43 | * @param array|string $rules |
||
44 | */ |
||
45 | public function __construct($identifier, $rules = null) |
||
54 | |||
55 | /** |
||
56 | * Constructs staticly. |
||
57 | * |
||
58 | * @param string $idenfitier |
||
59 | * @param null|string|array $rules |
||
60 | * @return static |
||
61 | */ |
||
62 | public static function handle($idenfitier, $rules = null) |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Add validation rules to the field. |
||
70 | * |
||
71 | * @param string|array $rules |
||
72 | * @return mixed |
||
73 | */ |
||
74 | public function withRules($rules) |
||
100 | |||
101 | /** |
||
102 | * Add a validation rule. |
||
103 | * |
||
104 | * @param string $rule |
||
105 | * @return self |
||
106 | */ |
||
107 | public function addRule($rule) |
||
113 | |||
114 | /** |
||
115 | * @param CrudFields $fields |
||
116 | * @return self |
||
117 | */ |
||
118 | public function setFields(CrudFields $fields) |
||
124 | |||
125 | /** |
||
126 | * Get the field identifier. |
||
127 | * |
||
128 | * @param void |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getIdentifier() |
||
135 | |||
136 | /** |
||
137 | * Set a custom label for the field. |
||
138 | * |
||
139 | * @param string $name |
||
140 | * @return self |
||
141 | */ |
||
142 | public function withLabel($name) |
||
148 | |||
149 | /** |
||
150 | * Defines a placeholder for the field. |
||
151 | * |
||
152 | * @param string $placeholder |
||
153 | * @return self |
||
154 | */ |
||
155 | public function withPlaceholder($placeholder) |
||
161 | |||
162 | /** |
||
163 | * Appends an help message to the input. |
||
164 | * |
||
165 | * @param string $help |
||
166 | * @return self |
||
167 | */ |
||
168 | public function withHelp($help) |
||
174 | |||
175 | /** |
||
176 | * Returns the field's form. |
||
177 | * |
||
178 | * @param void |
||
179 | * @return string |
||
180 | */ |
||
181 | public function form() |
||
199 | |||
200 | /** |
||
201 | * Get the field view name. |
||
202 | * |
||
203 | * @param void |
||
204 | * @return string |
||
205 | */ |
||
206 | abstract public function getViewName(); |
||
207 | |||
208 | /** |
||
209 | * Checks if the field has an error. |
||
210 | * |
||
211 | * @param void |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function hasError() |
||
218 | |||
219 | /** |
||
220 | * Returns the error. |
||
221 | * |
||
222 | * @param void |
||
223 | * @return null|string |
||
224 | */ |
||
225 | public function getError() |
||
234 | |||
235 | /** |
||
236 | * Returns the field's placeholder. |
||
237 | * |
||
238 | * @param void |
||
239 | * @return string |
||
240 | */ |
||
241 | public function getPlaceholder() |
||
250 | |||
251 | /** |
||
252 | * Returns the field's help. |
||
253 | * |
||
254 | * @param void |
||
255 | * @return string |
||
256 | */ |
||
257 | public function getHelp() |
||
266 | |||
267 | /** |
||
268 | * Checks if the field has a previous value. |
||
269 | * |
||
270 | * @param void |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function hasOld() |
||
277 | |||
278 | /** |
||
279 | * Returns the old value. |
||
280 | * |
||
281 | * @param void |
||
282 | * @return string|null |
||
283 | */ |
||
284 | public function getOld() |
||
293 | |||
294 | /** |
||
295 | * Returns the field's label. |
||
296 | * |
||
297 | * @param void |
||
298 | * @return string |
||
299 | */ |
||
300 | public function getLabel() |
||
309 | |||
310 | /** |
||
311 | * Get the field value. |
||
312 | * |
||
313 | * @param void |
||
314 | * @return mixed |
||
315 | */ |
||
316 | public function getValue() |
||
320 | |||
321 | /** |
||
322 | * Get the value to be displayed on a table. |
||
323 | * |
||
324 | * @param void |
||
325 | * @return mixed |
||
326 | */ |
||
327 | public function getTableValue() |
||
331 | |||
332 | /** |
||
333 | * Set a new value to the model. |
||
334 | * |
||
335 | * @param mixed $value |
||
336 | * @return self |
||
337 | */ |
||
338 | public function newValue($value) |
||
344 | |||
345 | /** |
||
346 | * @param void |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getRules() |
||
353 | } |
||
354 |
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: