1 | <?php |
||
41 | class FieldBuilder extends ParentDelegationBuilder implements NamedBuilder |
||
42 | { |
||
43 | /** |
||
44 | * Field Type |
||
45 | * @var string |
||
46 | */ |
||
47 | private $type; |
||
48 | |||
49 | /** |
||
50 | * Additional Field Configuration |
||
51 | * @var array |
||
52 | */ |
||
53 | private $config; |
||
54 | |||
55 | /** |
||
56 | * @param string $name Field Name, conventionally 'snake_case'. |
||
57 | * @param string $type Field Type. |
||
58 | * @param array $config Additional Field Configuration. |
||
59 | */ |
||
60 | public function __construct($name, $type, $config = []) |
||
71 | |||
72 | /** |
||
73 | * @return array |
||
74 | */ |
||
75 | private function getConfig() |
||
79 | |||
80 | /** |
||
81 | * Set a config key -> value pair |
||
82 | * @param string $key |
||
83 | * @param mixed $value |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setConfig($key, $value) |
||
90 | |||
91 | /** |
||
92 | * Update multiple config values using and array of key -> value pairs. |
||
93 | * @param array $config |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function updateConfig($config) |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getName() |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getKey() |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getLabel() |
||
125 | |||
126 | /** |
||
127 | * Will prepend `field_` if missing. |
||
128 | * @param string $key |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setKey($key) |
||
139 | |||
140 | /** |
||
141 | * Will set field required. |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setRequired() |
||
148 | |||
149 | /** |
||
150 | * Will set field unrequired. |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setUnrequired() |
||
157 | |||
158 | /** |
||
159 | * Will set field's instructions. |
||
160 | * @param string $instructions |
||
161 | * @return $this |
||
162 | */ |
||
163 | public function setInstructions($instructions) |
||
167 | |||
168 | /** |
||
169 | * Will set field's defaultValue. |
||
170 | * @param string $defaultValue |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setDefaultValue($defaultValue) |
||
177 | |||
178 | /** |
||
179 | * Add a conditional logic statement that will determine if the last added |
||
180 | * field will display or not. You can add `or` or `and` calls after |
||
181 | * to build complex logic. Any other function call will return you to the |
||
182 | * parentContext. |
||
183 | * @param string $name Dependent field name |
||
184 | * (choice type: radio, checkbox, select, trueFalse) |
||
185 | * @param string $operator ==, != |
||
186 | * @param string $value 1 or choice value |
||
187 | * @return ConditionalBuilder |
||
188 | */ |
||
189 | public function conditional($name, $operator, $value) |
||
198 | |||
199 | /** |
||
200 | * Set Wrapper container tag attributes |
||
201 | * |
||
202 | * @param array $config |
||
203 | * |
||
204 | * @return FieldBuilder |
||
205 | */ |
||
206 | public function setWrapper($config) |
||
210 | |||
211 | /** |
||
212 | * Get Wrapper container tag attributes |
||
213 | * |
||
214 | * @return array|mixed |
||
215 | */ |
||
216 | public function getWrapper() |
||
224 | |||
225 | /** |
||
226 | * Set width of a Wrapper container |
||
227 | * |
||
228 | * @param string $width Width of a container in % or px. |
||
229 | * |
||
230 | * @return FieldBuilder |
||
231 | */ |
||
232 | public function setWidth($width) |
||
239 | |||
240 | /** |
||
241 | * Set specified Attr of a Wrapper container |
||
242 | * |
||
243 | * @param string $name Attribute name, ex. 'class'. |
||
244 | * @param string|null $value Attribute value, ex. 'my-class'. |
||
245 | * |
||
246 | * @return FieldBuilder |
||
247 | */ |
||
248 | public function setAttr($name, $value = null) |
||
257 | |||
258 | /** |
||
259 | * Set Class and/or ID attribute of a Wrapper container |
||
260 | * use CSS-like selector string to specify css or id |
||
261 | * example: #my-id.foo-class.bar-class |
||
262 | * |
||
263 | * @param string $css_selector |
||
264 | * |
||
265 | * @return FieldBuilder |
||
266 | */ |
||
267 | public function setSelector($css_selector) |
||
292 | |||
293 | /** |
||
294 | * Build the field configuration array |
||
295 | * @return array Field configuration array |
||
296 | */ |
||
297 | public function build() |
||
311 | |||
312 | /** |
||
313 | * Create a field label based on the field's name. Generates title case. |
||
314 | * @param string $name |
||
315 | * @return string label |
||
316 | */ |
||
317 | protected function generateLabel($name) |
||
321 | |||
322 | /** |
||
323 | * Generates a snaked cased name. |
||
324 | * @param string $name |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function generateName($name) |
||
331 | } |
||
332 |