Completed
Push — master ( 1aee73...d60b75 )
by Shcherbak
05:52
created

BaseElement::getValidatorsErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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