Completed
Push — master ( d70912...e1269d )
by Shcherbak
03:23
created

BaseElement::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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