Completed
Branch dynamicfields (a10e1d)
by Derek Stephen
02:30
created

AbstractForm::populateField()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 8
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 4
nop 2
crap 3.1406
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 47
    public function __construct($name)
45
    {
46 47
        $this->fieldCollection = new FieldCollection();
47 47
        $this->formRenderer = new FormRenderer();
48 47
        $this->attributes = [
49 47
            'name' => $name,
50 47
            'method' => self::METHOD_POST,
51
        ];
52 47
        $this->displayErrors = false;
53 47
        $this->init();
54 47
    }
55
56
    abstract public function init();
57
58
    /**
59
     * @return bool
60
     */
61 8
    public function isValid()
62
    {
63 8
        $this->errorMessages = [];
64 8
        $this->validateFields();
65 8
        $count = count($this->errorMessages);
66 8
        $valid = ($count == 0);
67 8
        if ($valid) {
68 6
            $this->moveUploadedFiles();
69
        }
70 7
        return $valid;
71
    }
72
73 8
    private function validateFields()
74
    {
75 8
        $this->fieldCollection->rewind();
76 8
        while ($this->fieldCollection->valid()) {
77 8
            $this->checkFieldForErrors($this->fieldCollection->current());
78 8
            $this->fieldCollection->next();
79
        }
80 8
        $this->fieldCollection->rewind();
81 8
    }
82
83
    /**
84
     * @param FieldInterface $field
85
     */
86 8
    private function checkFieldForErrors(FieldInterface $field)
87
    {
88 8
        if (!$field->isValid()) {
89 6
            $this->errorMessages[$field->getName()] = $field->getMessages();
90
        }
91 8
    }
92
93
    /**
94
     * @return array
95
     */
96 4
    public function getValues()
97
    {
98 4
        $values = [];
99 4
        $this->fieldCollection->rewind();
100 4
        while ($this->fieldCollection->valid()) {
101 4
            $field = $this->fieldCollection->current();
102 4
            $values[$field->getName()] = $field->getValue();
103 4
            $this->fieldCollection->next();
104
        }
105 4
        $this->fieldCollection->rewind();
106 4
        return $values;
107
    }
108
109
    /**
110
     * @param array $data
111
     * @return $this
112
     */
113 5
    public function populate(array $data)
114
    {
115 5
        $this->fieldCollection->rewind();
116 5
        $this->populateFields($this->fieldCollection, $data);
117 5
        $this->fieldCollection->rewind();
118 5
        $this->displayErrors = true;
119 5
        return $this;
120
    }
121
122
    /**
123
     * @param array $dynamicForms
124
     * @param array $data
125
     */
126
    private function populateDynamicForms(array $dynamicForms, array $data)
127
    {
128
        /** @var FormInterface $form **/
129
        foreach ($dynamicForms as $form) {
130
            $fields = $form->getFields();
131
            $this->populateFields($fields, $data);
132
        }
133
    }
134
135
    /**
136
     * @param FieldCollection $fields
137
     * @param array $data
138
     */
139 5
    private function populateFields(FieldCollection $fields, array $data)
140
    {
141 5
        while ($fields->valid()) {
142 5
            $field = $fields->current();
143 5
            $this->populateField($field, $data);
144 5
            $fields->next();
145
        }
146 5
    }
147
148
    /**
149
     * @param FieldInterface $field
150
     * @param array $data
151
     */
152 5
    private function populateField(FieldInterface $field, array $data)
153
    {
154 5
        $name = $field->getName();
155 5
        if (isset($data[$name])) {
156 2
            $field->setValue($data[$name]);
157
        }
158 5
        if ($field->hasDynamicForms()) {
159
            $forms = $field->getDynamicForms();
160
            $this->populateDynamicForms($forms, $data);
161
        }
162 5
    }
163
164
    /**
165
     * @param string $name
166
     * @return FieldInterface|null
167
     */
168 2
    public function getField($name)
169
    {
170 2
        return $this->fieldCollection->findByName($name);
171
    }
172
173
    /**
174
     * @return FieldCollection
175
     */
176 30
    public function getFields()
177
    {
178 30
        return $this->fieldCollection;
179
    }
180
181
    /**
182
     * @param FieldInterface $field
183
     * @return $this
184
     */
185 38
    public function addField(FieldInterface $field)
186
    {
187 38
        $this->fieldCollection->append($field);
188 38
        return $this;
189
    }
190
191
    /**
192
     * @return string
193
     */
194 28
    public function render()
195
    {
196 28
        return $this->formRenderer->render($this, $this->isDisplayErrors());
197
    }
198
199
    /**
200
     * @param $url
201
     * @return $this
202
     */
203 2
    public function setAction($url)
204
    {
205 2
        $this->setAttribute('action', $url);
206 2
        return $this;
207
    }
208
209
    /**
210
     * @return string
211
     */
212 2
    public function getAction()
213
    {
214 2
        return $this->getAttribute('action');
215
    }
216
217
    /**
218
     * @return string
219
     */
220 29
    public function getId()
221
    {
222 29
        return $this->getAttribute('id');
223
    }
224
225
    /**
226
     * @param string $id
227
     * @return $this
228
     */
229 2
    public function setId($id)
230
    {
231 2
        $this->setAttribute('id', $id);
232 2
        return $this;
233
    }
234
235
    /**
236
     * @param $encType
237
     * @return $this
238
     */
239 2
    public function setEncType($encType)
240
    {
241 2
        $this->setAttribute('enctype', $encType);
242 2
        return $this;
243
    }
244
245
    /**
246
     * @return string
247
     */
248 1
    public function getEncType()
249
    {
250 1
        return $this->getAttribute('enctype');
251
    }
252
253
    /**
254
     * @param string $method
255
     * @return FormInterface
256
     */
257 2
    public function setMethod($method)
258
    {
259 2
        $this->setAttribute('method', $method);
260 2
        return $this;
261
    }
262
263
    /**
264
     * @return string
265
     */
266 29
    public function getMethod()
267
    {
268 29
        return $this->getAttribute('method');
269
    }
270
271
    /**
272
     * @param $class
273
     * @return FormInterface
274
     */
275 2
    public function setClass($class)
276
    {
277 2
        $this->setAttribute('class', $class);
278 2
        return $this;
279
    }
280
281
    /**
282
     * @return string
283
     */
284 2
    public function getClass()
285
    {
286 2
        return $this->getAttribute('class');
287
    }
288
289
    /**
290
     * @return boolean
291
     */
292 29
    public function isDisplayErrors()
293
    {
294 29
        return $this->displayErrors;
295
    }
296
297
    /**
298
     * @param boolean $displayErrors
299
     * @return AbstractForm
300
     */
301 3
    public function setDisplayErrors($displayErrors)
302
    {
303 3
        $this->displayErrors = $displayErrors;
304 3
        return $this;
305
    }
306
307
    /**
308
     * @param FormRendererInterface $renderer
309
     * @return AbstractForm
310
     */
311 2
    public function setFormRenderer(FormRendererInterface $renderer)
312
    {
313 2
        $this->formRenderer = $renderer;
314 2
        return $this;
315
    }
316
317 6
    public function moveUploadedFiles()
318
    {
319 6
        $this->fieldCollection->rewind();
320 6
        while ($this->fieldCollection->valid()) {
321 6
            $current = $this->fieldCollection->current();
322 6
            $this->moveFileIfUploadField($current);
323 5
            $this->fieldCollection->next() ;
324
        }
325 5
    }
326
327
    /**
328
     * @param FieldInterface $field
329
     * @return bool
330
     */
331 6
    public function moveFileIfUploadField(FieldInterface $field)
332
    {
333 6
        if ($field instanceof FileUpload) {
334 2
            $field->moveUploadToDestination();
335 1
            return true;
336
        }
337 4
        return false;
338
    }
339
}