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

FieldAbstract::getDynamicForms()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 19/11/2016
5
 * Time: 21:41
6
 */
7
8
namespace Del\Form\Field;
9
10
use Del\Form\Collection\FilterCollection;
11
use Del\Form\Collection\ValidatorCollection;
12
use Del\Form\Filter\FilterInterface;
13
use Del\Form\FormInterface;
14
use Del\Form\Renderer\Field\FieldRendererInterface;
15
use Del\Form\Renderer\Field\TextRender;
16
use Del\Form\Traits\HasAttributesTrait;
17
use Del\Form\Validator\NotEmpty;
18
use Del\Form\Validator\ValidatorInterface;
19
use Exception;
20
21
abstract class FieldAbstract implements FieldInterface
22
{
23
24
    /**  @var FormInterface[] $dynamicFormCollection */
25
    private $dynamicFormCollection;
26
27
    /**  @var FilterCollection $filterCollection */
28
    private $filterCollection;
29
30
    /**  @var ValidatorCollection $validatorCollection */
31
    private $validatorCollection;
32
33
    /** @var FieldRendererInterface $renderer  */
34
    private $renderer;
35
36
    /** @var array $errorMessages */
37
    private $errorMessages;
38
39
    /** @var string $customErrorMessage */
40
    private $customErrorMessage;
41
42
    /** @var string $label */
43
    private $label;
44
45
    /** @var bool $required */
46
    private $required;
47
48
    use HasAttributesTrait;
49
50
    /**
51
     * @return string
52
     */
53
    abstract public function getTag();
54
55
    abstract public function init();
56
57 47
    public function __construct($name, $value = null)
58
    {
59 47
        $this->required = false;
60 47
        $this->dynamicFormCollection = [];
61 47
        $this->filterCollection = new FilterCollection();
62 47
        $this->validatorCollection = new ValidatorCollection();
63 47
        $this->renderer = new TextRender();
64 47
        $this->setName($name);
65 47
        is_null($value) ? null : $this->setValue($value);
66 47
        $this->init();
67 47
    }
68
69
    /**
70
     * @return string
71
     */
72 31
    public function getName()
73
    {
74 31
        return $this->getAttribute('name');
75
    }
76
77
    /**
78
     * @param string $name
79
     * @return FieldAbstract
80
     */
81 47
    public function setName($name)
82
    {
83 47
        $this->setAttribute('name', $name);
84 47
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 28
    public function getId()
91
    {
92 28
        return $this->getAttribute('id');
93
    }
94
95
    /**
96
     * @param string $id
97
     * @return FieldAbstract
98
     */
99 8
    public function setId($id)
100
    {
101 8
        $this->setAttribute('id', $id);
102 8
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108 1
    public function getClass()
109
    {
110 1
        return $this->getAttribute('class') ?: 'form-control';
111
    }
112
113
    /**
114
     * @param string $class
115
     * @return FieldAbstract
116
     */
117 8
    public function setClass($class)
118
    {
119 8
        $this->setAttribute('class', $class);
120 8
        return $this;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126 34
    public function getValue()
127
    {
128 34
        return $this->getAttribute('value');
129
    }
130
131
    /**
132
     * @param mixed $value
133
     * @return FieldAbstract
134
     */
135 28
    public function setValue($value)
136
    {
137 28
        $this->setAttribute('value', $value);
138 28
        $this->filterValue();
139 28
        return $this;
140
    }
141
142
    /**
143
     * @param ValidatorInterface $validator
144
     * @return $this
145
     */
146 19
    public function addValidator(ValidatorInterface $validator)
147
    {
148 19
        $this->validatorCollection->append($validator);
149 19
        return $this;
150
    }
151
152
    /**
153
     * @return ValidatorCollection
154
     */
155 2
    public function getValidators()
156
    {
157 2
        return $this->validatorCollection;
158
    }
159
160
    /**
161
     * @param FilterInterface $filter
162
     * @return $this
163
     */
164 27
    public function addFilter(FilterInterface $filter)
165
    {
166 27
        $this->filterCollection->append($filter);
167 27
        return $this;
168
    }
169
170
    /**
171
     * @return FilterCollection
172
     */
173 1
    public function getFilters()
174
    {
175 1
        return $this->filterCollection;
176
    }
177
178
    /**
179
     *  Runs the checkForErrors method for each field, which adds to errorMessages if invalid
180
     *
181
     * @return bool
182
     * @throws Exception If validation of $value is impossible
183
     */
184 29
    public function isValid()
185
    {
186 29
        $this->errorMessages = [];
187 29
        $this->validatorCollection->rewind();
188 29
        while ($this->validatorCollection->valid()) {
189 16
            $this->checkForErrors($this->validatorCollection->current());
190 16
            $this->validatorCollection->next();
191
        }
192 29
        $count = count($this->errorMessages);
193 29
        return $count == 0;
194
    }
195
196
    /**
197
     * @param ValidatorInterface $validator
198
     */
199 16
    private function checkForErrors(ValidatorInterface $validator)
200
    {
201 16
        $value = $this->getValue();
202
203 16
        if ( (!$validator->isValid($value)) && $this->isRequired()) {
204 12
            $this->errorMessages = array_merge($this->errorMessages, $validator->getMessages());
205
        }
206 16
    }
207
208 28
    private function filterValue()
209
    {
210 28
        $value = $this->getAttribute('value');
211 28
        $this->filterCollection->rewind();
212 28
        while ($this->filterCollection->valid()) {
213 9
            $value = $this->filterCollection->current()->filter($value);
214 9
            $this->filterCollection->next();
215
        }
216 28
        $this->filterCollection->rewind();
217 28
        $this->setAttribute('value', $value);
218 28
    }
219
220
    /**
221
     * @return array
222
     */
223 9
    public function getMessages()
224
    {
225 9
        return array_values($this->errorMessages);
226
    }
227
228
    /**
229
     * @return string
230
     */
231 27
    public function getLabel()
232
    {
233 27
        return $this->label;
234
    }
235
236
    /**
237
     * @param string $label
238
     * @return $this
239
     */
240 17
    public function setLabel($label)
241
    {
242 17
        $this->label = $label;
243 17
        return $this;
244
    }
245
246
    /**
247
     * @param string $message
248
     * @return $this
249
     */
250 5
    public function setCustomErrorMessage($message)
251
    {
252 5
        $this->customErrorMessage = $message;
253 5
        return $this;
254
    }
255
256
    /**
257
     * @return bool
258
     */
259 4
    public function hasCustomErrorMessage()
260
    {
261 4
        return $this->customErrorMessage != null;
262
    }
263
264
    /**
265
     * @return string
266
     */
267 2
    public function getCustomErrorMessage()
268
    {
269 2
        return $this->customErrorMessage;
270
    }
271
272
    /**
273
     * @return FieldRendererInterface
274
     */
275 28
    public function getRenderer()
276
    {
277 28
        return $this->renderer;
278
    }
279
280
    /**
281
     * @param FieldRendererInterface $renderer
282
     * @return $this
283
     */
284 32
    public function setRenderer(FieldRendererInterface $renderer)
285
    {
286 32
        $this->renderer = $renderer;
287 32
        return $this;
288
    }
289
290
    /**
291
     * If a field is required then it must have a value
292
     * We add a not empty validator
293
     *
294
     * @return boolean
295
     */
296 33
    public function isRequired()
297
    {
298 33
        return $this->required;
299
    }
300
301
    /**
302
     * @param boolean $required
303
     * @return FieldAbstract
304
     */
305 16
    public function setRequired($required)
306
    {
307 16
        $required ? $this->addNotEmptyValidator() : $this->removeNotEmptyValidator();
308 16
        $this->required = $required;
309 16
        return $this;
310
    }
311
312 16
    private function addNotEmptyValidator()
313
    {
314 16
        $notEmpty = new NotEmpty();
315 16
        $this->addValidator($notEmpty);
316 16
    }
317
318 2
    private function removeNotEmptyValidator()
319
    {
320 2
        $this->validatorCollection->rewind();
321 2
        while ($this->validatorCollection->valid()) {
322 2
            $validator = $this->validatorCollection->current();
323 2
            $validator instanceof NotEmpty
324 2
                ? $this->validatorCollection->offsetUnset($this->validatorCollection->key())
325 1
                : null;
326 2
            $this->validatorCollection->next();
327
        }
328 2
    }
329
330
    /**
331
     * @param FormInterface $form
332
     * @param $triggerValue
333
     * @return $this
334
     */
335 3
    public function addDynamicForm(FormInterface $form, $triggerValue)
336
    {
337 3
        $this->dynamicFormCollection[$triggerValue] = $form;
338 3
        return $this;
339
    }
340
341
    /**
342
     * @return bool
343
     */
344 33
    public function hasDynamicForms()
345
    {
346 33
        return count($this->dynamicFormCollection) > 0;
347
    }
348
349
    /**
350
     * @return FormInterface[]
351
     * @throws Exception
352
     */
353 4
    public function getDynamicForms()
354
    {
355 4
        if (!$this->hasDynamicForms()) {
356 1
            throw new Exception('No dynamic form for this value - Did you check hasDynamicForm() ?');
357
        }
358 3
        return $this->dynamicFormCollection;
359
    }
360
}