Completed
Pull Request — master (#31)
by Vitaliy
02:53
created

BaseElement   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 82.81%

Importance

Changes 12
Bugs 1 Features 4
Metric Value
wmc 26
c 12
b 1
f 4
lcom 2
cbo 4
dl 0
loc 241
ccs 53
cts 64
cp 0.8281
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
handle() 0 1 ?
A addValidator() 0 4 1
A getValidators() 0 3 1
A addFilter() 0 4 1
A getFilters() 0 3 1
A setValue() 0 4 1
A getValue() 0 3 1
A setAttribute() 0 14 3
A isValid() 0 10 3
A validate() 0 4 1
A getValidatorsErrors() 0 10 3
A render() 0 5 1
A required() 0 4 1
A setText() 0 4 1
A getText() 0 3 1
A setName() 0 4 1
A getName() 0 3 1
A setClass() 0 4 1
A getClass() 0 3 1
A setId() 0 4 1
A getId() 0 3 1
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 ValidatorInterface[]
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 ValidatorInterface[]
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
        if (!$validator->isValid($value)) {
131 7
          return false;
132
        }
133
      }
134
135 11
      return true;
136
    }
137
138
139
    /**
140
     * @return bool
141
     */
142
    public function validate() {
143
      trigger_error('Deprecated. Use isValid', E_USER_DEPRECATED);
144
      return $this->isValid();
145
    }
146
147
148
    /**
149
     * @return array
150
     */
151 6
    public function getValidatorsErrors() {
152 6
      $errors = [];
153 6
      foreach ($this->validators as $validator) {
154 6
        foreach ($validator->getErrors() as $error) {
155 6
          $errors[] = $error;
156
        }
157
      }
158
159 6
      return $errors;
160
    }
161
162
163
    /**
164
     * @inheritdoc
165
     */
166 7
    public function render() {
167 7
      $value = $this->getValue();
168 7
      $this->attributes['value'] = htmlentities($value, ENT_QUOTES);
169 7
      return parent::render();
170
    }
171
172
173
    /**
174
     * @deprecated
175
     * @see addValidator
176
     * @return $this
177
     */
178
    public function required() {
179
      trigger_error('Deprecated', E_USER_DEPRECATED);
180
      return $this->addValidator(new \Fiv\Form\Validator\Required());
181
    }
182
183
184
    /**
185
     * @param string $text
186
     * @return $this
187
     */
188 17
    public function setText($text) {
189 17
      $this->text = $text;
190 17
      return $this;
191
    }
192
193
194
    /**
195
     * @return mixed
196
     */
197 1
    public function getText() {
198 1
      return $this->text;
199
    }
200
201
202
    /**
203
     * @param string $name
204
     * @return $this
205
     */
206 38
    public function setName($name) {
207 38
      $this->setAttribute('name', $name);
208 38
      return $this;
209
    }
210
211
212
    /**
213
     * @return null|string
214
     */
215 34
    public function getName() {
216 34
      return $this->getAttribute('name');
217
    }
218
219
220
    /**
221
     * @param string $class
222
     * @return $this
223
     */
224 1
    public function setClass($class) {
225 1
      $this->setAttribute('class', $class);
226 1
      return $this;
227
    }
228
229
230
    /**
231
     * @return null|string
232
     */
233 1
    public function getClass() {
234 1
      return $this->getAttribute('class');
235
    }
236
237
238
    /**
239
     * @param string $id
240
     * @return $this
241
     */
242
    public function setId($id) {
243
      $this->setAttribute('id', $id);
244
      return $this;
245
    }
246
247
248
    /**
249
     * @return null|string
250
     */
251
    public function getId() {
252
      return $this->getAttribute('id');
253
    }
254
255
  }