Completed
Push — master ( b0ba81...041dbc )
by Shcherbak
09:45
created

BaseElement::setAttribute()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 1 Features 1
Metric Value
dl 0
loc 16
rs 9.4285
c 3
b 1
f 1
ccs 8
cts 8
cp 1
cc 3
eloc 8
nc 2
nop 2
crap 3
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 {
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 6
    public function addValidator(ValidatorInterface $validator) {
49 6
      $this->validators[] = $validator;
50 6
      return $this;
51
    }
52
53
54
    /**
55
     * @return \Fiv\Form\Validator\Base[]
56
     */
57 8
    public function getValidators() {
58 8
      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 16
    public function getFilters() {
78 16
      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 14
    public function getValue() {
98 14
      return $this->value;
99
    }
100
101
102
    /**
103
     * @inheritdoc
104
     */
105 18
    public function setAttribute($name, $value) {
106 18
      if ($name === 'value') {
107
108 16
        $this->validationResult = null;
109
110
        # apply filters to the value
111 16
        $filters = $this->getFilters();
112 16
        foreach ($filters as $filter) {
113 3
          $value = $filter->apply($value);
114
        }
115
116 16
        $this->value = $value;
117
      }
118
119 18
      return parent::setAttribute($name, $value);
120
    }
121
122
123
    /**
124
     * Return true if element is valid
125
     * @return boolean
126
     */
127 8
    public function validate() {
128
129 8
      if ($this->validationResult !== null) {
130
        return $this->validationResult;
131
      }
132
133 8
      $this->validationResult = true;
134 8
      $value = $this->getValue();
135 8
      foreach ($this->getValidators() as $validator) {
136 6
        $validator->flushErrors();
137 6
        if (!$validator->isValid($value)) {
138 6
          $this->validationResult = false;
139
        }
140
      }
141
142 8
      return $this->validationResult;
143
    }
144
145
146
    /**
147
     * @return array
148
     */
149
    public function getValidatorsErrors() {
150
      $errors = [];
151
      foreach ($this->validators as $validator) {
152
        $errors = array_merge($errors, $validator->getErrors());
153
      }
154
155
      return $errors;
156
    }
157
158
159
    /**
160
     * @inheritdoc
161
     */
162 3
    public function render() {
163 3
      $value = $this->getValue();
164 3
      $this->attributes['value'] = htmlentities($value, ENT_QUOTES);
165 3
      return parent::render();
166
    }
167
168
169
    /**
170
     * @deprecated
171
     * @see addValidator
172
     * @return $this
173
     */
174
    public function required() {
175
      trigger_error('Deprecated', E_USER_DEPRECATED);
176
      return $this->addValidator(new \Fiv\Form\Validator\Required());
177
    }
178
179
180
    /**
181
     * @param string $text
182
     * @return $this
183
     */
184 10
    public function setText($text) {
185 10
      $this->text = $text;
186 10
      return $this;
187
    }
188
189
190
    /**
191
     * @return mixed
192
     */
193
    public function getText() {
194
      return $this->text;
195
    }
196
197
198
    /**
199
     * @param string $name
200
     * @return $this
201
     */
202 17
    public function setName($name) {
203 17
      $this->setAttribute('name', $name);
204 17
      return $this;
205
    }
206
207
208
    /**
209
     * @return null|string
210
     */
211 16
    public function getName() {
212 16
      return $this->getAttribute('name');
213
    }
214
215
216
    /**
217
     * @param string $class
218
     * @return $this
219
     */
220 1
    public function setClass($class) {
221 1
      $this->setAttribute('class', $class);
222 1
      return $this;
223
    }
224
225
226
    /**
227
     * @return null|string
228
     */
229 1
    public function getClass() {
230 1
      return $this->getAttribute('class');
231
    }
232
233
234
    /**
235
     * @param string $id
236
     * @return $this
237
     */
238
    public function setId($id) {
239
      $this->setAttribute('id', $id);
240
      return $this;
241
    }
242
243
244
    /**
245
     * @return null|string
246
     */
247
    public function getId() {
248
      return $this->getAttribute('id');
249
    }
250
251
  }