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

BaseElement::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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