Completed
Pull Request — master (#28)
by Vitaliy
03:13
created

BaseElement::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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