Completed
Branch dev-master (5a9550)
by Derek Stephen
02:00
created

FieldAbstract::setRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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