1 | <?php |
||
21 | trait FieldsAwareTrait |
||
22 | { |
||
23 | protected $fields = []; |
||
24 | |||
25 | 20 | public function buildFields() |
|
37 | |||
38 | /** |
||
39 | * @param array $fieldsArray |
||
40 | */ |
||
41 | public function addFields($fieldsArray) |
||
42 | { |
||
43 | foreach($fieldsArray as $fieldName => $fieldConfig) { |
||
44 | if (is_object($fieldConfig)) { |
||
45 | $this->addField($fieldName, $fieldConfig); |
||
46 | } else { |
||
47 | $this->addField($fieldName, $fieldConfig['type'], $fieldConfig); |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | 17 | public function addField($name, $type, $config = []) |
|
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | * |
||
74 | * @return Field |
||
75 | */ |
||
76 | 14 | public function getField($name) |
|
80 | |||
81 | /** |
||
82 | * @param $name |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | 14 | public function hasField($name) |
|
90 | |||
91 | /** |
||
92 | * @return Field[] |
||
93 | */ |
||
94 | 7 | public function getFields() |
|
98 | |||
99 | public function removeField($name) |
||
105 | } |
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: