1 | <?php |
||
16 | trait FormFieldSchemaTrait { |
||
17 | |||
18 | /** |
||
19 | * The type of front-end component to render the FormField as. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $schemaComponent; |
||
24 | |||
25 | /** |
||
26 | * Structured schema data representing the FormField. |
||
27 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $schemaData = []; |
||
32 | |||
33 | /** |
||
34 | * Structured schema state representing the FormField's current data and validation. |
||
35 | * Used to render the FormField as a ReactJS Component on the front-end. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $schemaState = []; |
||
40 | |||
41 | /** |
||
42 | * Sets the component type the FormField will be rendered as on the front-end. |
||
43 | * |
||
44 | * @param string $componentType |
||
45 | * @return FormField |
||
46 | */ |
||
47 | public function setSchemaComponent($componentType) { |
||
51 | |||
52 | /** |
||
53 | * Gets the type of front-end component the FormField will be rendered as. |
||
54 | * |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getSchemaComponent() { |
||
60 | |||
61 | /** |
||
62 | * Sets the schema data used for rendering the field on the front-end. |
||
63 | * Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}. |
||
64 | * Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored. |
||
65 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
66 | * |
||
67 | * @param array $schemaData - The data to be merged with $this->schemaData. |
||
68 | * @return FormField |
||
69 | * |
||
70 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
71 | */ |
||
72 | public function setSchemaData($schemaData = []) { |
||
78 | |||
79 | /** |
||
80 | * Gets the schema data used to render the FormField on the front-end. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getSchemaData() { |
||
87 | |||
88 | /** |
||
89 | * Gets the defaults for $schemaData. |
||
90 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaData()} are ignored. |
||
91 | * Instead the `data` array should be used to pass around ad hoc data. |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getSchemaDataDefaults() { |
||
115 | |||
116 | /** |
||
117 | * Sets the schema data used for rendering the field on the front-end. |
||
118 | * Merges the passed array with the current `$schemaData` or {@link getSchemaDataDefaults()}. |
||
119 | * Any passed keys that are not defined in {@link getSchemaDataDefaults()} are ignored. |
||
120 | * If you want to pass around ad hoc data use the `data` array e.g. pass `['data' => ['myCustomKey' => 'yolo']]`. |
||
121 | * |
||
122 | * @param array $schemaData - The data to be merged with $this->schemaData. |
||
123 | * @return FormField |
||
124 | * |
||
125 | * @todo Add deep merging of arrays like `data` and `attributes`. |
||
126 | */ |
||
127 | public function setSchemaState($schemaState = []) { |
||
133 | |||
134 | /** |
||
135 | * Gets the schema state used to render the FormField on the front-end. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | public function getSchemaState() { |
||
142 | |||
143 | /** |
||
144 | * Gets the defaults for $schemaState. |
||
145 | * The keys defined here are immutable, meaning undefined keys passed to {@link setSchemaState()} are ignored. |
||
146 | * Instead the `data` array should be used to pass around ad hoc data. |
||
147 | * Includes validation data if the field is associated to a {@link Form}, |
||
148 | * and {@link Form->validate()} has been called. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getSchemaStateDefaults() { |
||
174 | } |
||
175 |
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: