Completed
Branch dynamicfields (2a748f)
by Derek Stephen
02:55
created

AbstractForm::getFieldValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
crap 4
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 12:13
6
 */
7
8
namespace Del\Form;
9
10
use Del\Form\Collection\FieldCollection;
11
use Del\Form\Field\FieldInterface;
12
use Del\Form\Field\FileUpload;
13
use Del\Form\Renderer\FormRenderer;
14
use Del\Form\Renderer\FormRendererInterface;
15
use Del\Form\Traits\HasAttributesTrait;
16
17
abstract class AbstractForm implements FormInterface
18
{
19
    const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
20
    const ENC_TYPE_URL_ENCODED = 'application/x-www-form-urlencoded';
21
    const ENC_TYPE_TEXT_PLAIN = 'text/plain';
22
23
    const METHOD_POST = 'post';
24
    const METHOD_GET = 'get';
25
26
    /** @var FieldCollection $fieldCollection */
27
    private $fieldCollection;
28
29
    /** @var FormRendererInterface  */
30
    private $formRenderer;
31
32
    /** @var array $errorMessages */
33
    private $errorMessages;
34
35
    /** @var bool $displayErrors */
36
    private $displayErrors;
37
38
    use HasAttributesTrait;
39
40
    /**
41
     * AbstractForm constructor.
42
     * @param $name
43
     */
44 50
    public function __construct($name)
45
    {
46 50
        $this->fieldCollection = new FieldCollection();
47 50
        $this->formRenderer = new FormRenderer();
48 50
        $this->attributes = [
49 50
            'name' => $name,
50 50
            'method' => self::METHOD_POST,
51
        ];
52 50
        $this->displayErrors = false;
53 50
        $this->init();
54 50
    }
55
56
    abstract public function init();
57
58
    /**
59
     * @return bool
60
     */
61 9
    public function isValid()
62
    {
63 9
        $this->errorMessages = [];
64 9
        $fields = $this->fieldCollection;
65 9
        $this->validateFields($fields);
66 9
        $count = count($this->errorMessages);
67 9
        $valid = ($count == 0);
68 9
        if ($valid) {
69 7
            $this->moveUploadedFiles();
70
        }
71 8
        return $valid;
72
    }
73
74
    /**
75
     * @param FieldCollection $fields
76
     */
77 9
    private function validateFields(FieldCollection $fields)
78
    {
79 9
        $fields->rewind();
80 9
        while ($fields->valid()) {
81 9
            $this->checkFieldForErrors($fields->current());
82 9
            $this->checkDynamicFormsForErrors($fields->current());
83 9
            $fields->next();
84
        }
85 9
        $fields->rewind();
86 9
    }
87
88
    /**
89
     * @param FieldInterface $field
90
     */
91 9
    private function checkFieldForErrors(FieldInterface $field)
92
    {
93 9
        if (!$field->isValid()) {
94 7
            $this->errorMessages[$field->getName()] = $field->getMessages();
95
        }
96 9
    }
97
98 9 View Code Duplication
    public function checkDynamicFormsForErrors(FieldInterface $field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100 9
        if ($field->hasDynamicForms()) {
101 1
            $forms = $field->getDynamicForms();
102 1
            $value = $field->getValue();
103 1
            if (isset($forms[$value])) {
104 1
                $form  = $forms[$value];
105 1
                $fields = $form->getFields();
106 1
                $this->validateFields($fields);
107
            }
108
        }
109 9
    }
110
111
    /**
112
     * @return array
113
     */
114 5
    public function getValues()
115
    {
116 5
        $values = [];
117 5
        $fields = $this->fieldCollection;
118 5
        $values = $this->getFieldValues($fields, $values);
119 5
        return $values;
120
    }
121
122
    /**
123
     * @param FieldCollection $fields
124
     * @param array $values
125
     * @return array
126
     */
127 5
    private function getFieldValues(FieldCollection $fields, array $values)
128
    {
129 5
        $fields->rewind();
130 5
        while ($fields->valid()) {
131
            /** @var FieldInterface $field */
132 5
            $field = $fields->current();
133 5
            $value = $field->getValue();
134 5
            $values[$field->getName()] = $value;
135 5
            if ($field->hasDynamicForms()) {
136 1
                $forms = $field->getDynamicForms();
137 1
                if (isset($forms[$value])) {
138 1
                    $form = $forms[$value];
139 1
                    $dynamicFormFields = $form->getFields();
140 1
                    $values = $this->getFieldValues($dynamicFormFields, $values);
141
                }
142
            }
143 5
            $fields->next();
144
        }
145 5
        $fields->rewind();
146 5
        return $values;
147
    }
148
149
    /**
150
     * @param array $data
151
     * @return $this
152
     */
153 7
    public function populate(array $data)
154
    {
155 7
        $fields = $this->fieldCollection;
156 7
        $this->populateFields($fields, $data);
157 7
        $this->displayErrors = true;
158 7
        return $this;
159
    }
160
161
    /**
162
     * @param array $dynamicForms
163
     * @param array $data
164
     */
165 2
    private function populateDynamicForms(array $dynamicForms, array $data)
166
    {
167
        /** @var FormInterface $form **/
168 2
        foreach ($dynamicForms as $form) {
169 2
            $fields = $form->getFields();
170 2
            $this->populateFields($fields, $data);
171
        }
172 2
    }
173
174
    /**
175
     * @param FieldCollection $fields
176
     * @param array $data
177
     */
178 7
    private function populateFields(FieldCollection $fields, array $data)
179
    {
180 7
        $fields->rewind();
181 7
        while ($fields->valid()) {
182 7
            $field = $fields->current();
183 7
            $this->populateField($field, $data);
184 7
            $fields->next();
185
        }
186 7
        $fields->rewind();
187 7
    }
188
189
    /**
190
     * @param FieldInterface $field
191
     * @param array $data
192
     */
193 7 View Code Duplication
    private function populateField(FieldInterface $field, array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
    {
195 7
        $name = $field->getName();
196 7
        if (isset($data[$name])) {
197 4
            $field->setValue($data[$name]);
198
        }
199 7
        if ($field->hasDynamicForms()) {
200 2
            $forms = $field->getDynamicForms();
201 2
            $this->populateDynamicForms($forms, $data);
202
        }
203 7
    }
204
205
    /**
206
     * @param string $name
207
     * @return FieldInterface|null
208
     */
209 2
    public function getField($name)
210
    {
211 2
        return $this->fieldCollection->findByName($name);
212
    }
213
214
    /**
215
     * @return FieldCollection
216
     */
217 33
    public function getFields()
218
    {
219 33
        return $this->fieldCollection;
220
    }
221
222
    /**
223
     * @param FieldInterface $field
224
     * @return $this
225
     */
226 41
    public function addField(FieldInterface $field)
227
    {
228 41
        $this->fieldCollection->append($field);
229 41
        return $this;
230
    }
231
232
    /**
233
     * @return string
234
     */
235 29
    public function render()
236
    {
237 29
        return $this->formRenderer->render($this, $this->isDisplayErrors());
238
    }
239
240
    /**
241
     * @param $url
242
     * @return $this
243
     */
244 2
    public function setAction($url)
245
    {
246 2
        $this->setAttribute('action', $url);
247 2
        return $this;
248
    }
249
250
    /**
251
     * @return string
252
     */
253 2
    public function getAction()
254
    {
255 2
        return $this->getAttribute('action');
256
    }
257
258
    /**
259
     * @return string
260
     */
261 30
    public function getId()
262
    {
263 30
        return $this->getAttribute('id');
264
    }
265
266
    /**
267
     * @param string $id
268
     * @return $this
269
     */
270 2
    public function setId($id)
271
    {
272 2
        $this->setAttribute('id', $id);
273 2
        return $this;
274
    }
275
276
    /**
277
     * @param $encType
278
     * @return $this
279
     */
280 2
    public function setEncType($encType)
281
    {
282 2
        $this->setAttribute('enctype', $encType);
283 2
        return $this;
284
    }
285
286
    /**
287
     * @return string
288
     */
289 1
    public function getEncType()
290
    {
291 1
        return $this->getAttribute('enctype');
292
    }
293
294
    /**
295
     * @param string $method
296
     * @return FormInterface
297
     */
298 2
    public function setMethod($method)
299
    {
300 2
        $this->setAttribute('method', $method);
301 2
        return $this;
302
    }
303
304
    /**
305
     * @return string
306
     */
307 30
    public function getMethod()
308
    {
309 30
        return $this->getAttribute('method');
310
    }
311
312
    /**
313
     * @param $class
314
     * @return FormInterface
315
     */
316 2
    public function setClass($class)
317
    {
318 2
        $this->setAttribute('class', $class);
319 2
        return $this;
320
    }
321
322
    /**
323
     * @return string
324
     */
325 2
    public function getClass()
326
    {
327 2
        return $this->getAttribute('class');
328
    }
329
330
    /**
331
     * @return boolean
332
     */
333 30
    public function isDisplayErrors()
334
    {
335 30
        return $this->displayErrors;
336
    }
337
338
    /**
339
     * @param boolean $displayErrors
340
     * @return AbstractForm
341
     */
342 3
    public function setDisplayErrors($displayErrors)
343
    {
344 3
        $this->displayErrors = $displayErrors;
345 3
        return $this;
346
    }
347
348
    /**
349
     * @param FormRendererInterface $renderer
350
     * @return AbstractForm
351
     */
352 5
    public function setFormRenderer(FormRendererInterface $renderer)
353
    {
354 5
        $this->formRenderer = $renderer;
355 5
        return $this;
356
    }
357
358 7
    public function moveUploadedFiles()
359
    {
360 7
        $this->fieldCollection->rewind();
361 7
        while ($this->fieldCollection->valid()) {
362 7
            $current = $this->fieldCollection->current();
363 7
            $this->moveFileIfUploadField($current);
364 6
            $this->fieldCollection->next() ;
365
        }
366 6
    }
367
368
    /**
369
     * @param FieldInterface $field
370
     * @return bool
371
     */
372 7
    public function moveFileIfUploadField(FieldInterface $field)
373
    {
374 7
        if ($field instanceof FileUpload) {
375 2
            $field->moveUploadToDestination();
376 1
            return true;
377
        }
378 5
        return false;
379
    }
380
}