Completed
Push — master ( 849039...32eb69 )
by Shcherbak
03:58
created

BaseElement   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 97.83%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 24
c 2
b 1
f 1
lcom 1
cbo 4
dl 0
loc 195
ccs 45
cts 46
cp 0.9783
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A addValidator() 0 14 4
A setText() 0 4 1
A getText() 0 3 1
A addFilter() 0 13 4
A setValue() 0 4 1
A setAttribute() 0 17 3
A getFilters() 0 3 1
A validate() 0 17 4
A getValue() 0 3 1
A getValidators() 0 3 1
A getValidatorsErrors() 0 8 2
A required() 0 4 1
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 9
    public function setText($text) {
74 9
      $this->text = $text;
75 9
      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 2
    public function addFilter($filter) {
95 2
      if (!is_array($filter)) {
96 1
        $filter = [$filter];
97
      }
98 2
      foreach ($filter as $filterClass) {
99 2
        if (!($filterClass instanceof FilterInterface)) {
100
          throw new \Exception('Invalid filter class: ' . get_class($filterClass));
101
        }
102 2
        $this->filters[] = $filterClass;
103
      }
104
105 2
      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 17
    public function setAttribute($name, $value) {
125 17
      if ($name === 'value') {
126
127 15
        $this->validationResult = null;
128
129
        # apply filters to the value
130 15
        $filters = $this->getFilters();
131 15
        foreach ($filters as $filter) {
132 2
          $value = $filter->apply($value);
133
        }
134
135 15
        $value = htmlentities($value, ENT_COMPAT);
136 15
        $this->value = $value;
137
      }
138
139 17
      return parent::setAttribute($name, $value);
140
    }
141
142
143
    /**
144
     * @return \Fiv\Form\Filter\FilterInterface[]
145
     */
146 15
    public function getFilters() {
147 15
      return $this->filters;
148
    }
149
150
151
    /**
152
     * Return true if element is valid
153
     * @return boolean
154
     */
155 7
    public function validate() {
156
157 7
      if ($this->validationResult !== null) {
158
        return $this->validationResult;
159
      }
160
161 7
      $this->validationResult = true;
162 7
      $value = $this->getValue();
163 7
      foreach ($this->getValidators() as $validator) {
164 6
        $validator->flushErrors();
165 6
        if (!$validator->isValid($value)) {
166 6
          $this->validationResult = false;
167
        }
168
      }
169
170 7
      return $this->validationResult;
171
    }
172
173
174
    /**
175
     * @return string
176
     */
177 10
    public function getValue() {
178 10
      return $this->value;
179
    }
180
181
182
    /**
183
     * @return \Fiv\Form\Validator\Base[]
184
     */
185 7
    public function getValidators() {
186 7
      return $this->validators;
187
    }
188
189
190
    /**
191
     * @return array
192
     */
193
    public function getValidatorsErrors() {
194
      $errors = [];
195
      foreach ($this->validators as $validator) {
196
        $errors = array_merge($errors, $validator->getErrors());
197
      }
198
199
      return $errors;
200
    }
201
202
203
    /**
204
     * @deprecated use addValidator(new \Fiv\Form\Validator\Required()) instead
205
     * @see addValidator
206
     * @return $this
207
     */
208
    public function required() {
209
      trigger_error('Deprecated', E_USER_DEPRECATED);
210
      return $this->addValidator(new \Fiv\Form\Validator\Required());
211
    }
212
  }