1 | <?php |
||
45 | class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder |
||
46 | { |
||
47 | /** |
||
48 | * Field Type |
||
49 | * @var string |
||
50 | */ |
||
51 | private $type; |
||
52 | |||
53 | /** |
||
54 | * Additional Field Configuration |
||
55 | * @var array |
||
56 | */ |
||
57 | private $config; |
||
58 | |||
59 | /** |
||
60 | * @param string $name Field Name, conventionally 'snake_case'. |
||
61 | * @param string $type Field Type. |
||
62 | * @param array $config Additional Field Configuration. |
||
63 | */ |
||
64 | public function __construct($name, $type, $config = []) |
||
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | private function getConfig() |
||
83 | |||
84 | /** |
||
85 | * Set a config key -> value pair |
||
86 | * @param string $key |
||
87 | * @param mixed $value |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function setConfig($key, $value) |
||
94 | |||
95 | /** |
||
96 | * Update multiple config values using and array of key -> value pairs. |
||
97 | * @param array $config |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function updateConfig($config) |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getName() |
||
113 | |||
114 | /** |
||
115 | * @return string |
||
116 | */ |
||
117 | public function getKey() |
||
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | public function getLabel() |
||
129 | |||
130 | /** |
||
131 | * Will prepend `field_` if missing. |
||
132 | * @param string $key |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setKey($key) |
||
143 | |||
144 | /** |
||
145 | * Will set field required. |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function setRequired() |
||
152 | |||
153 | /** |
||
154 | * Will set field unrequired. |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function setUnrequired() |
||
161 | |||
162 | /** |
||
163 | * Will set field's label. |
||
164 | * @param string $label |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setLabel($label) |
||
171 | |||
172 | /** |
||
173 | * Will set field's instructions. |
||
174 | * @param string $instructions |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setInstructions($instructions) |
||
181 | |||
182 | /** |
||
183 | * Will set field's defaultValue. |
||
184 | * @param string $defaultValue |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setDefaultValue($defaultValue) |
||
191 | |||
192 | /** |
||
193 | * Add a conditional logic statement that will determine if the last added |
||
194 | * field will display or not. You can add `or` or `and` calls after |
||
195 | * to build complex logic. Any other function call will return you to the |
||
196 | * parentContext. |
||
197 | * @param string $name Dependent field name |
||
198 | * (choice type: radio, checkbox, select, trueFalse) |
||
199 | * @param string $operator ==, != |
||
200 | * @param string $value 1 or choice value |
||
201 | * @return ConditionalBuilder |
||
202 | */ |
||
203 | public function conditional($name, $operator, $value) |
||
212 | |||
213 | /** |
||
214 | * Set Wrapper container tag attributes |
||
215 | * |
||
216 | * @param array $config |
||
217 | * |
||
218 | * @return FieldBuilder |
||
219 | */ |
||
220 | public function setWrapper($config) |
||
224 | |||
225 | /** |
||
226 | * Get Wrapper container tag attributes |
||
227 | * |
||
228 | * @return array|mixed |
||
229 | */ |
||
230 | public function getWrapper() |
||
238 | |||
239 | /** |
||
240 | * Set width of a Wrapper container |
||
241 | * |
||
242 | * @param string $width Width of a container in % or px. |
||
243 | * |
||
244 | * @return FieldBuilder |
||
245 | */ |
||
246 | public function setWidth($width) |
||
253 | |||
254 | /** |
||
255 | * Set specified Attr of a Wrapper container |
||
256 | * |
||
257 | * @param string $name Attribute name, ex. 'class'. |
||
258 | * @param string|null $value Attribute value, ex. 'my-class'. |
||
259 | * |
||
260 | * @return FieldBuilder |
||
261 | */ |
||
262 | public function setAttr($name, $value = null) |
||
271 | |||
272 | /** |
||
273 | * Set Class and/or ID attribute of a Wrapper container |
||
274 | * use CSS-like selector string to specify css or id |
||
275 | * example: #my-id.foo-class.bar-class |
||
276 | * |
||
277 | * @param string $css_selector |
||
278 | * |
||
279 | * @return FieldBuilder |
||
280 | */ |
||
281 | public function setSelector($css_selector) |
||
306 | |||
307 | /** |
||
308 | * Build the field configuration array |
||
309 | * @return array Field configuration array |
||
310 | */ |
||
311 | public function build() |
||
325 | |||
326 | /** |
||
327 | * Create a field label based on the field's name. Generates title case. |
||
328 | * @param string $name |
||
329 | * @return string label |
||
330 | */ |
||
331 | protected function generateLabel($name) |
||
335 | |||
336 | /** |
||
337 | * Generates a snaked cased name. |
||
338 | * @param string $name |
||
339 | * @return string |
||
340 | */ |
||
341 | protected function generateName($name) |
||
345 | } |
||
346 |