Completed
Push — master ( 60d066...9b845d )
by Shcherbak
03:05
created

BaseElement::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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