PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.21
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.21
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / app / Services / Parser / Extractor.php

Extractor.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.21, at app/Services/Parser/Extractor.php

352 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\Parser;
4
5 use FluentForm\Framework\Helpers\ArrayHelper as Arr;
6
7 class Extractor
8 {
9 /**
10 * The form field settings.
11 *
12 * @var array
13 */
14 protected $fields;
15
16 /**
17 * The properties that need to be extracted.
18 *
19 * @var array
20 */
21 protected $with;
22
23 /**
24 * The supported form input types defined for Fluent Form.
25 *
26 * @var array
27 */
28 protected $inputTypes;
29
30 /**
31 * The extracted result.
32 *
33 * @var array
34 */
35 protected $result = [];
36
37 /**
38 * The current form field that's being set when we loop
39 * through all the form fields in the looper method.
40 *
41 * @var array
42 */
43 protected $field = [];
44
45 /**
46 * The current field attribute that's being set when we loop
47 * through all the form fields in the looper method.
48 *
49 * @var string
50 */
51 protected $attribute;
52
53 /**
54 * Extractor constructor.
55 *
56 * @param array $fields
57 * @param array $with
58 * @param array $inputTypes
59 */
60 public function __construct($fields, $with, $inputTypes)
61 {
62 $this->fields = $fields;
63 $this->with = $with;
64 $this->inputTypes = $inputTypes;
65 }
66
67 /**
68 * The extractor initializer for getting the extracted data.
69 *
70 * @return array
71 */
72 public function extract()
73 {
74 $this->looper($this->fields);
75
76 return $this->result;
77 }
78
79 /**
80 * The recursive looper method to loop each
81 * of the fields and extract it's data.
82 *
83 * @param array $fields
84 */
85 protected function looper($fields = [])
86 {
87 foreach ($fields as $field) {
88 // If the field is a Container (collection of other fields)
89 // then we will recursively call this function to resolve.
90 if ($field['element'] === 'container') {
91 foreach ($field['columns'] as $item) {
92 $this->looper($item['fields']);
93 }
94 }
95
96 // Now the field is supposed to be a flat field.
97 // We can extract the desired keys as we want.
98 else {
99 if (in_array($field['element'], $this->inputTypes)) {
100 $this->extractField($field);
101 }
102 }
103 }
104 }
105
106 /**
107 * Extract the form field.
108 *
109 * @param array $field
110 * @return $this
111 */
112 protected function extractField($field)
113 {
114 // Before starting the extraction we'll set the current
115 // field and it's attribute name at first. And then we
116 // will proceed to extract the field settings that
117 // the developer demanded using $with initially.
118 $this->prepareIteration($field, Arr::get($field, 'attributes.name'))
119 ->setElement()
120 ->setAdminLabel()
121 ->setLabel()
122 ->setOptions()
123 ->setAdvancedOptions()
124 ->setSettings()
125 ->setRaw()
126 ->setAttributes()
127 ->setValidations()
128 ->handleCustomField();
129
130 return $this;
131 }
132
133 /**
134 * Set the field and attribute of the current iteration when
135 * we loop through the form fields using the looper method.
136 *
137 * @param array $field
138 * @param string $attribute
139 * @return $this
140 */
141 protected function prepareIteration($field, $attribute)
142 {
143 $this->field = $field;
144
145 $this->attribute = $attribute;
146
147 return $this;
148 }
149
150 /**
151 * Set the element of the form field.
152 *
153 * @param array $field
154 * @param string $attributeName
155 * @return $this
156 */
157 protected function setElement()
158 {
159 $this->result[$this->attribute]['element'] = $this->field['element'];
160 return $this;
161 }
162
163
164 /**
165 * Set the label of the form field.
166 *
167 * @return $this
168 */
169 protected function setLabel()
170 {
171 if (in_array('label', $this->with)) {
172 $this->result[$this->attribute]['label'] = Arr::get($this->field, 'settings.label', '');
173 }
174
175 return $this;
176 }
177
178 /**
179 * Set the admin label of the form field.
180 *
181 * @return $this
182 */
183 protected function setAdminLabel()
184 {
185 if (in_array('admin_label', $this->with)) {
186 $adminLabel = Arr::get($this->field, 'settings.admin_field_label') ?:
187 Arr::get($this->field, 'settings.label') ?:
188 Arr::get($this->field, 'element');
189
190 $this->result[$this->attribute]['admin_label'] = $adminLabel;
191 }
192
193
194 return $this;
195 }
196
197 /**
198 * Set the options of the form field.
199 *
200 * @return $this
201 */
202 protected function setOptions()
203 {
204 if (in_array('options', $this->with)) {
205 $options = Arr::get($this->field, 'options', []);
206 if(!$options) {
207 $newOptions = Arr::get($this->field, 'settings.advanced_options', []);
208 $options = [];
209 if($newOptions) {
210 foreach ($newOptions as $option) {
211 $options[$option['value']] = $option['label'];
212 }
213 }
214 }
215 $this->result[$this->attribute]['options'] = $options;
216 }
217 return $this;
218 }
219
220 /**
221 * Set the advanced options of the form field.
222 *
223 * @return $this
224 */
225 protected function setAdvancedOptions()
226 {
227 if (in_array('advanced_options', $this->with)) {
228 $this->result[$this->attribute]['advanced_options'] = Arr::get($this->field, 'settings.advanced_options', []);
229 }
230 return $this;
231 }
232
233 protected function setSettings()
234 {
235 if (in_array('settings', $this->with)) {
236 $this->result[$this->attribute]['settings'] = Arr::get($this->field, 'settings', []);
237 }
238 return $this;
239 }
240
241 /**
242 * Set the attributes of the form field.
243 *
244 * @return $this
245 */
246 protected function setAttributes()
247 {
248 if (in_array('attributes', $this->with)) {
249 $this->result[$this->attribute]['attributes'] = Arr::get($this->field, 'attributes');
250 }
251
252
253 return $this;
254 }
255
256 /**
257 * Set the validation rules and conditions of the form field.
258 *
259 * @return $this
260 */
261 protected function setValidations()
262 {
263 if (in_array('rules', $this->with)) {
264 $this->result[$this->attribute]['rules'] = Arr::get(
265 $this->field,
266 'settings.validation_rules'
267 );
268
269 $this->result[$this->attribute]['conditionals'] = Arr::get(
270 $this->field,
271 'settings.conditional_logics'
272 );
273 }
274
275 return $this;
276 }
277
278 /**
279 * Handle the child fields of the custom field.
280 *
281 * @return $this
282 */
283 protected function handleCustomField()
284 {
285 // If this field is a custom field we'll assume it has it's child fields
286 // under the `fields` key. Then we are gonna modify those child fields'
287 // attribute `name`, `label` & `conditional_logics` properties using
288 // the parent field. The current implementation will modify those
289 // properties in a way so that we can use dot notation to access.
290 $customFields = Arr::get($this->field, 'fields');
291
292 if ($customFields) {
293 $parentAttribute = Arr::get($this->field, 'attributes.name');
294
295 $parentConditionalLogics = Arr::get($this->field, 'settings.conditional_logics', []);
296
297 $isAddressOrNameField = in_array(Arr::get($this->field, 'element'), ['address', 'input_name']);
298
299 $isRepeatField = Arr::get($this->field, 'element') === 'input_repeat' || Arr::get($this->field, 'element') == 'repeater_field';
300
301 foreach ($customFields as $index => $customField) {
302 // If the current field is in fact `address` || `name` field
303 // then we have to only keep the enabled child fields
304 // by the user from the form editor settings.
305 if ($isAddressOrNameField) {
306 if (!Arr::get($customField, 'settings.visible', false)) {
307 unset($customFields[$index]);
308 continue;
309 }
310 }
311
312 // Depending on whether the parent field is a repeat field or not
313 // the modified attribute name of the child field will vary.
314 if ($isRepeatField) {
315 $modifiedAttribute = $parentAttribute.'['.$index.'].*';
316 } else {
317 $modifiedAttribute = $parentAttribute.'['.Arr::get($customField, 'attributes.name').']';
318 }
319
320 $modifiedLabel = $parentAttribute.'['.Arr::get($customField, 'settings.label').']';
321
322 $customField['attributes']['name'] = $modifiedAttribute;
323
324 $customField['settings']['label'] = $modifiedLabel;
325
326 // Now, we'll replace the `conditional_logics` property
327 $customField['settings']['conditional_logics'] = $parentConditionalLogics;
328
329 // Now that this field's properties are handled we can pass
330 // it to the extract field method to extract it's data.
331 $this->extractField($customField);
332 }
333 }
334
335 return $this;
336 }
337
338 /**
339 * Set the raw field of the form field.
340 *
341 * @return $this
342 */
343 protected function setRaw()
344 {
345 if (in_array('raw', $this->with)) {
346 $this->result[$this->attribute]['raw'] = $this->field;
347 }
348
349 return $this;
350 }
351 }
352