Completed
Pull Request — master (#23)
by Vitaliy
03:07
created

BaseElement::setValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
  namespace Fiv\Form\Element;
4
5
  use Fiv\Form\Filter\FilterInterface;
6
  use Fiv\Form\Validator;
7
  use Fiv\Form\Validator\ValidatorInterface;
8
9
  /**
10
   * @method addFilters
11
   * @package Fiv\Form\Element
12
   * @author Ivan Shcherbak <[email protected]> 2016
13
   */
14
  class BaseElement extends \Fiv\Form\Element\Html implements ElementInterface {
15
16
    /**
17
     * @var null|boolean
18
     */
19
    protected $validationResult = null;
20
21
    /**
22
     * @var \Fiv\Form\Validator\Base[]
23
     */
24
    protected $validators = [];
25
26
    /**
27
     * @var FilterInterface[]
28
     */
29
    protected $filters = [];
30
31
    /**
32
     * @var string
33
     */
34
    protected $text = '';
35
36
    /**
37
     * @var null|string
38
     */
39
    protected $value = null;
40
41
42
    /**
43
     * Attach validator to current element
44
     *
45
     * @param ValidatorInterface $validator
46
     * @return $this
47
     */
48 7
    public function addValidator(ValidatorInterface $validator) {
49 7
      $this->validators[] = $validator;
50 7
      return $this;
51
    }
52
53
54
    /**
55
     * @return \Fiv\Form\Validator\Base[]
56
     */
57 10
    public function getValidators() {
58 10
      return $this->validators;
59
    }
60
61
62
    /**
63
     *
64
     * @param FilterInterface $filter
65
     * @throws \Exception
66
     * @return $this
67
     */
68 3
    public function addFilter(FilterInterface $filter) {
69 3
      $this->filters[] = $filter;
70 3
      return $this;
71
    }
72
73
74
    /**
75
     * @return FilterInterface[]
76
     */
77 23
    public function getFilters() {
78 23
      return $this->filters;
79
    }
80
81
82
    /**
83
     * Alias of setAttribute('value', $value)
84
     *
85
     * @param $value
86
     * @return $this
87
     */
88 2
    public function setValue($value) {
89 2
      $this->setAttribute('value', $value);
90 2
      return $this;
91
    }
92
93
94
    /**
95
     * @return string
96
     */
97 23
    public function getValue() {
98 23
      return $this->value;
99
    }
100
101
102
    /**
103
     * @inheritdoc
104
     */
105 26
    public function setAttribute($name, $value) {
106 26
      if ($name === 'value') {
107
108 23
        $this->validationResult = null;
109
110
        # apply filters to the value
111 23
        $filters = $this->getFilters();
112 23
        foreach ($filters as $filter) {
113 3
          $value = $filter->apply($value);
114
        }
115
116 23
        $this->value = $value;
117
      }
118
119 26
      return parent::setAttribute($name, $value);
120
    }
121
122
123
    /**
124
     * Return true if element is valid
125
     * @return boolean
126
     */
127 10
    public function isValid() {
128
129 10
      if ($this->validationResult !== null) {
130 1
        return $this->validationResult;
131
      }
132
133 10
      $this->validationResult = true;
134 10
      $value = $this->getValue();
135 10
      foreach ($this->getValidators() as $validator) {
136 7
        $validator->flushErrors();
137 7
        if (!$validator->isValid($value)) {
138 7
          $this->validationResult = false;
139
        }
140
      }
141
142 10
      return $this->validationResult;
143
    }
144
145
146
    /**
147
     * @return bool
148
     */
149
    public function validate() {
150
      trigger_error('Deprecated. Use isValid', E_USER_DEPRECATED);
151
      return $this->isValid();
152
    }
153
154
155
    /**
156
     * @return array
157
     */
158 6
    public function getValidatorsErrors() {
159 6
      $errors = [];
160 6
      foreach ($this->validators as $validator) {
161 6
        foreach ($validator->getErrors() as $error) {
162 6
          $errors[] = $error;
163
        }
164
      }
165
166 6
      return $errors;
167
    }
168
169
170
    /**
171
     * @inheritdoc
172
     */
173 6
    public function render() {
174 6
      $value = $this->getValue();
175 6
      $this->attributes['value'] = htmlentities($value, ENT_QUOTES);
176 6
      return parent::render();
177
    }
178
179
180
    /**
181
     * @deprecated
182
     * @see addValidator
183
     * @return $this
184
     */
185
    public function required() {
186
      trigger_error('Deprecated', E_USER_DEPRECATED);
187
      return $this->addValidator(new \Fiv\Form\Validator\Required());
188
    }
189
190
191
    /**
192
     * @param string $text
193
     * @return $this
194
     */
195 14
    public function setText($text) {
196 14
      $this->text = $text;
197 14
      return $this;
198
    }
199
200
201
    /**
202
     * @return mixed
203
     */
204 1
    public function getText() {
205 1
      return $this->text;
206
    }
207
208
209
    /**
210
     * @param string $name
211
     * @return $this
212
     */
213 25
    public function setName($name) {
214 25
      $this->setAttribute('name', $name);
215 25
      return $this;
216
    }
217
218
219
    /**
220
     * @return null|string
221
     */
222 23
    public function getName() {
223 23
      return $this->getAttribute('name');
224
    }
225
226
227
    /**
228
     * @param string $class
229
     * @return $this
230
     */
231 1
    public function setClass($class) {
232 1
      $this->setAttribute('class', $class);
233 1
      return $this;
234
    }
235
236
237
    /**
238
     * @return null|string
239
     */
240 1
    public function getClass() {
241 1
      return $this->getAttribute('class');
242
    }
243
244
245
    /**
246
     * @param string $id
247
     * @return $this
248
     */
249
    public function setId($id) {
250
      $this->setAttribute('id', $id);
251
      return $this;
252
    }
253
254
255
    /**
256
     * @return null|string
257
     */
258
    public function getId() {
259
      return $this->getAttribute('id');
260
    }
261
262
  }