Completed
Branch master (7150c8)
by Derek Stephen
01:56
created

FieldAbstract::setCustomErrorMessage()   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\Validator\ValidatorInterface;
14
use Exception;
15
16
abstract class FieldAbstract implements FieldInterface
17
{
18
    /** @var string $name */
19
    private $name;
20
21
    /** @var string $id */
22
    private $id;
23
24
    /** @var string $class  */
25
    private $class;
26
27
    /**  @var FilterCollection $filterCollection */
28
    private $filterCollection;
29
30
    /**  @var ValidatorCollection $validatorCollection */
31
    private $validatorCollection;
32
33
    private $value;
34
35
    /** @var array $errorMessages */
36
    private $errorMessages;
37
38
    /** @var string $customErrorMessage */
39
    private $customErrorMessage;
40
41
    /** @var string $label */
42
    private $label;
43
44
    /**
45
     * @return string
46
     */
47
    abstract public function getTag();
48
49
    /**
50
     * @return mixed
51
     */
52
    abstract public function getTagType();
53
54 10
    public function __construct($name, $value = null)
55
    {
56 10
        $this->filterCollection = new FilterCollection();
57 10
        $this->validatorCollection = new ValidatorCollection();
58 10
        $this->setName($name);
59 10
        is_null($value) ? null : $this->setValue($value);
60 10
    }
61
62
    /**
63
     * @return string
64
     */
65 9
    public function getName()
66
    {
67 9
        return $this->name;
68
    }
69
70
    /**
71
     * @param string $name
72
     * @return FieldAbstract
73
     */
74 10
    public function setName($name)
75
    {
76 10
        $this->name = $name;
77 10
        return $this;
78
    }
79
80
    /**
81
     * @return string
82
     */
83 5
    public function getId()
84
    {
85 5
        return $this->id;
86
    }
87
88
    /**
89
     * @param string $id
90
     * @return FieldAbstract
91
     */
92 6
    public function setId($id)
93
    {
94 6
        $this->id = $id;
95 6
        return $this;
96
    }
97
98
    /**
99
     * @return string
100
     */
101 5
    public function getClass()
102
    {
103 5
        return $this->class ?: 'form-control';
104
    }
105
106
    /**
107
     * @param string $class
108
     * @return FieldAbstract
109
     */
110 2
    public function setClass($class)
111
    {
112 2
        $this->class = $class;
113 2
        return $this;
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119 8
    public function getValue()
120
    {
121 8
        return $this->value;
122
    }
123
124
    /**
125
     * @param mixed $value
126
     * @return FieldAbstract
127
     */
128 7
    public function setValue($value)
129
    {
130 7
        $this->value = $value;
131 7
        $this->filterValue();
132 7
        return $this;
133
    }
134
135
    /**
136
     * @param ValidatorInterface $validator
137
     * @return $this
138
     */
139 5
    public function addValidator(ValidatorInterface $validator)
140
    {
141 5
        $this->validatorCollection->append($validator);
142 5
        return $this;
143
    }
144
145
    /**
146
     * @return ValidatorCollection
147
     */
148 1
    public function getValidators()
149
    {
150 1
        return $this->validatorCollection;
151
    }
152
153
    /**
154
     * @param FilterInterface $filter
155
     * @return $this
156
     */
157 2
    public function addFilter(FilterInterface $filter)
158
    {
159 2
        $this->filterCollection->append($filter);
160 2
        return $this;
161
    }
162
163
    /**
164
     * @return FilterCollection
165
     */
166 1
    public function getFilters()
167
    {
168 1
        return $this->filterCollection;
169
    }
170
171
    /**
172
     * @return bool
173
     * @throws Exception If validation of $value is impossible
174
     */
175 5 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...
176
    {
177 5
        $this->errorMessages = [];
178 5
        $this->validatorCollection->rewind();
179 5
        while ($this->validatorCollection->valid()) {
180 4
            $this->checkForErrors($this->validatorCollection->current());
181 4
            $this->validatorCollection->next();
182
        }
183 5
        $count = count($this->errorMessages);
184 5
        return $count == 0;
185
    }
186
187
    /**
188
     * @param FieldInterface $field
0 ignored issues
show
Bug introduced by
There is no parameter named $field. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
189
     */
190 4
    private function checkForErrors(ValidatorInterface $validator)
191
    {
192 4
        $value = $this->getValue();
193
194 4
        if (!$validator->isValid($value)) {
195 4
            $this->errorMessages = array_merge($this->errorMessages, $validator->getMessages());
196
        }
197 4
    }
198
199 7
    private function filterValue()
200
    {
201 7
        $value = $this->value;
202 7
        $this->filterCollection->rewind();
203 7
        while ($this->filterCollection->valid()) {
204 1
            $value = $this->filterCollection->current()->filter($value);
205 1
            $this->filterCollection->next();
206
        }
207 7
        $this->filterCollection->rewind();
208 7
        $this->value = $value;
209 7
    }
210
211
    /**
212
     * @return array
213
     */
214 2
    public function getMessages()
215
    {
216 2
        return array_values($this->errorMessages);
217
    }
218
219
    /**
220
     * @return string
221
     */
222 4
    public function getLabel()
223
    {
224 4
        return $this->label;
225
    }
226
227
    /**
228
     * @param string $label
229
     * @return FieldAbstract
230
     */
231 1
    public function setLabel($label)
232
    {
233 1
        $this->label = $label;
234 1
        return $this;
235
    }
236
237
    /**
238
     * @param string $message
239
     * @return $this
240
     */
241 1
    public function setCustomErrorMessage($message)
242
    {
243 1
        $this->customErrorMessage = $message;
244 1
        return $this;
245
    }
246
247
    /**
248
     * @return bool
249
     */
250 2
    public function hasCustomErrorMessage()
251
    {
252 2
        return $this->customErrorMessage != null;
253
    }
254
255
    /**
256
     * @return string
257
     */
258 1
    public function getCustomErrorMessage()
259
    {
260 1
        return $this->customErrorMessage;
261
    }
262
263
264
}