Completed
Push — master ( ca59d5...30f23d )
by Derek Stephen
01:52
created

FieldAbstract::isRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Renderer\Field\DefaultRender;
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\ValidatorInterface;
18
use Exception;
19
20
abstract class FieldAbstract implements FieldInterface
21
{
22
    /**  @var FilterCollection $filterCollection */
23
    private $filterCollection;
24
25
    /**  @var ValidatorCollection $validatorCollection */
26
    private $validatorCollection;
27
28
    /** @var FieldRendererInterface $renderer  */
29
    private $renderer;
30
31
    /** @var array $errorMessages */
32
    private $errorMessages;
33
34
    /** @var string $customErrorMessage */
35
    private $customErrorMessage;
36
37
    /** @var string $label */
38
    private $label;
39
40
    /** @var bool $required */
41
    private $required;
42
43
    use HasAttributesTrait;
44
45
    /**
46
     * @return string
47
     */
48
    abstract public function getTag();
49
50
    abstract public function init();
51
52 18
    public function __construct($name, $value = null)
53
    {
54 18
        $this->required = false;
55 18
        $this->filterCollection = new FilterCollection();
56 18
        $this->validatorCollection = new ValidatorCollection();
57 18
        $this->renderer = new TextRender();
58 18
        $this->setName($name);
59 18
        is_null($value) ? null : $this->setValue($value);
60 18
        $this->init();
61 18
    }
62
63
    /**
64
     * @return string
65
     */
66 9
    public function getName()
67
    {
68 9
        return $this->getAttribute('name');
69
    }
70
71
    /**
72
     * @param string $name
73
     * @return FieldAbstract
74
     */
75 18
    public function setName($name)
76
    {
77 18
        $this->setAttribute('name', $name);
78 18
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84 10
    public function getId()
85
    {
86 10
        return $this->getAttribute('id');
87
    }
88
89
    /**
90
     * @param string $id
91
     * @return FieldAbstract
92
     */
93 8
    public function setId($id)
94
    {
95 8
        $this->setAttribute('id', $id);
96 8
        return $this;
97
    }
98
99
    /**
100
     * @return string
101
     */
102 1
    public function getClass()
103
    {
104 1
        return $this->getAttribute('class') ?: 'form-control';
105
    }
106
107
    /**
108
     * @param string $class
109
     * @return FieldAbstract
110
     */
111 4
    public function setClass($class)
112
    {
113 4
        $this->setAttribute('class', $class);
114 4
        return $this;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120 12
    public function getValue()
121
    {
122 12
        return $this->getAttribute('value');
123
    }
124
125
    /**
126
     * @param mixed $value
127
     * @return FieldAbstract
128
     */
129 11
    public function setValue($value)
130
    {
131 11
        $this->setAttribute('value', $value);
132 11
        $this->filterValue();
133 11
        return $this;
134
    }
135
136
    /**
137
     * @param ValidatorInterface $validator
138
     * @return $this
139
     */
140 16
    public function addValidator(ValidatorInterface $validator)
141
    {
142 16
        $this->validatorCollection->append($validator);
143 16
        return $this;
144
    }
145
146
    /**
147
     * @return ValidatorCollection
148
     */
149 1
    public function getValidators()
150
    {
151 1
        return $this->validatorCollection;
152
    }
153
154
    /**
155
     * @param FilterInterface $filter
156
     * @return $this
157
     */
158 16
    public function addFilter(FilterInterface $filter)
159
    {
160 16
        $this->filterCollection->append($filter);
161 16
        return $this;
162
    }
163
164
    /**
165
     * @return FilterCollection
166
     */
167 1
    public function getFilters()
168
    {
169 1
        return $this->filterCollection;
170
    }
171
172
    /**
173
     * @return bool
174
     * @throws Exception If validation of $value is impossible
175
     */
176 10 View Code Duplication
    public function isValid()
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...
177
    {
178 10
        $this->errorMessages = [];
179 10
        $this->validatorCollection->rewind();
180 10
        while ($this->validatorCollection->valid()) {
181 8
            $this->checkForErrors($this->validatorCollection->current());
182 8
            $this->validatorCollection->next();
183 8
        }
184 10
        $count = count($this->errorMessages);
185 10
        return $count == 0;
186
    }
187
188
    /**
189
     * @param ValidatorInterface $validator
190
     */
191 8
    private function checkForErrors(ValidatorInterface $validator)
192
    {
193 8
        $value = $this->getValue();
194
195 8
        if (!$validator->isValid($value)) {
196 7
            $this->errorMessages = array_merge($this->errorMessages, $validator->getMessages());
197 7
        }
198 8
    }
199
200 11
    private function filterValue()
201
    {
202 11
        $value = $this->getAttribute('value');
203 11
        $this->filterCollection->rewind();
204 11
        while ($this->filterCollection->valid()) {
205 4
            $value = $this->filterCollection->current()->filter($value);
206 4
            $this->filterCollection->next();
207 4
        }
208 11
        $this->filterCollection->rewind();
209 11
        $this->setAttribute('value', $value);
210 11
    }
211
212
    /**
213
     * @return array
214
     */
215 3
    public function getMessages()
216
    {
217 3
        return array_values($this->errorMessages);
218
    }
219
220
    /**
221
     * @return string
222
     */
223 9
    public function getLabel()
224
    {
225 9
        return $this->label;
226
    }
227
228
    /**
229
     * @param string $label
230
     * @return FieldAbstract
231
     */
232 3
    public function setLabel($label)
233
    {
234 3
        $this->label = $label;
235 3
        return $this;
236
    }
237
238
    /**
239
     * @param string $message
240
     * @return $this
241
     */
242 2
    public function setCustomErrorMessage($message)
243
    {
244 2
        $this->customErrorMessage = $message;
245 2
        return $this;
246
    }
247
248
    /**
249
     * @return bool
250
     */
251 3
    public function hasCustomErrorMessage()
252
    {
253 3
        return $this->customErrorMessage != null;
254
    }
255
256
    /**
257
     * @return string
258
     */
259 2
    public function getCustomErrorMessage()
260
    {
261 2
        return $this->customErrorMessage;
262
    }
263
264
    /**
265
     * @return FieldRendererInterface
266
     */
267 10
    public function getRenderer()
268
    {
269 10
        return $this->renderer;
270
    }
271
272
    /**
273
     * @param FieldRendererInterface $renderer
274
     * @return $this
275
     */
276 7
    public function setRenderer(FieldRendererInterface $renderer)
277
    {
278 7
        $this->renderer = $renderer;
279 7
        return $this;
280
    }
281
282
    /**
283
     * @return boolean
284
     */
285 9
    public function isRequired()
286
    {
287 9
        return $this->required;
288
    }
289
290
    /**
291
     * @param boolean $required
292
     * @return FieldAbstract
293
     */
294 3
    public function setRequired($required)
295
    {
296 3
        $this->required = $required;
297 3
        return $this;
298
    }
299
}