1 | <?php |
||
19 | abstract class Base extends \Fiv\Form\Element\Html { |
||
20 | |||
21 | /** |
||
22 | * @var Form |
||
23 | */ |
||
24 | protected $form = null; |
||
25 | |||
26 | /** |
||
27 | * @var null|string |
||
28 | */ |
||
29 | protected $value = null; |
||
30 | |||
31 | /** |
||
32 | * @var \Fiv\Form\Validator\Base[] |
||
33 | */ |
||
34 | protected $validators = []; |
||
35 | |||
36 | /** |
||
37 | * @var null|boolean |
||
38 | */ |
||
39 | protected $validationResult = null; |
||
40 | |||
41 | /** |
||
42 | * @var \Fiv\Form\Filter\Base[] |
||
43 | */ |
||
44 | protected $filters = []; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $text = ''; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @return Form |
||
54 | */ |
||
55 | public function getForm() { |
||
56 | return $this->form; |
||
57 | } |
||
58 | |||
59 | |||
60 | /** |
||
61 | * @param Form $form |
||
62 | * @return $this |
||
63 | */ |
||
64 | public function setForm(Form $form) { |
||
65 | $this->form = $form; |
||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | |||
70 | /** |
||
71 | * Attach validator to current element |
||
72 | * |
||
73 | * @param \Fiv\Form\Validator\Base[]|\Fiv\Form\Validator\Base $validator |
||
74 | * @throws \Exception |
||
75 | * @return $this |
||
76 | */ |
||
77 | 5 | public function addValidator($validator) { |
|
78 | 5 | if (!is_array($validator)) { |
|
79 | 5 | $validator = array($validator); |
|
80 | } |
||
81 | 5 | $this->validationResult = null; |
|
82 | 5 | foreach ($validator as $validatorClass) { |
|
83 | 5 | if (!($validatorClass instanceof \Fiv\Form\Validator\Base)) { |
|
84 | throw new \Exception('Invalid validator class: ' . get_class($validatorClass)); |
||
85 | } |
||
86 | 5 | $this->validators[] = $validatorClass; |
|
87 | } |
||
88 | |||
89 | 5 | return $this; |
|
90 | } |
||
91 | |||
92 | |||
93 | /** |
||
94 | * @param string $text |
||
95 | * @return $this |
||
96 | */ |
||
97 | 9 | public function setText($text) { |
|
101 | |||
102 | |||
103 | /** |
||
104 | * @return mixed |
||
105 | */ |
||
106 | public function getText() { |
||
107 | return $this->text; |
||
108 | } |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Attach filter to current element |
||
113 | * |
||
114 | * @param \Fiv\Form\Filter\Base|\Fiv\Form\Filter\Base[] $filter |
||
115 | * @throws \Exception |
||
116 | * @return $this |
||
117 | */ |
||
118 | 1 | public function addFilter($filter) { |
|
119 | 1 | if (!is_array($filter)) { |
|
120 | 1 | $filter = array($filter); |
|
121 | } |
||
122 | 1 | foreach ($filter as $filterClass) { |
|
123 | 1 | if (!($filterClass instanceof \Fiv\Form\Filter\Base)) { |
|
124 | throw new \Exception('Invalid filter class: ' . get_class($filterClass)); |
||
125 | } |
||
126 | 1 | $this->filters[] = $filterClass; |
|
127 | } |
||
128 | |||
129 | 1 | return $this; |
|
130 | } |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @param $value |
||
135 | * @return $this |
||
136 | */ |
||
137 | 11 | public function setValue($value) { |
|
138 | 11 | $this->validationResult = null; |
|
139 | |||
140 | 11 | $filters = $this->getFilters(); |
|
141 | 11 | foreach ($filters as $filter) { |
|
142 | 1 | $value = $filter->apply($value); |
|
143 | } |
||
144 | |||
145 | 11 | $this->value = $value; |
|
146 | 11 | return $this; |
|
147 | } |
||
148 | |||
149 | |||
150 | /** |
||
151 | * @return \Fiv\Form\Filter\Base[] |
||
152 | */ |
||
153 | 11 | public function getFilters() { |
|
156 | |||
157 | |||
158 | /** |
||
159 | * Return true if element is valid |
||
160 | * @return boolean |
||
161 | */ |
||
162 | 6 | public function validate() { |
|
163 | |||
164 | 6 | if ($this->validationResult !== null) { |
|
165 | return $this->validationResult; |
||
166 | } |
||
167 | |||
168 | 6 | $this->validationResult = true; |
|
169 | 6 | $value = $this->getValue(); |
|
170 | 6 | foreach ($this->getValidators() as $validator) { |
|
171 | 5 | $validator->flushErrors(); |
|
172 | 5 | if (!$validator->isValid($value)) { |
|
173 | 5 | $this->validationResult = false; |
|
174 | } |
||
175 | } |
||
176 | |||
177 | 6 | return $this->validationResult; |
|
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 8 | public function getValue() { |
|
187 | |||
188 | |||
189 | /** |
||
190 | * @return \Fiv\Form\Validator\Base[] |
||
191 | */ |
||
192 | 6 | public function getValidators() { |
|
195 | |||
196 | |||
197 | /** |
||
198 | * @return array |
||
199 | */ |
||
200 | public function getValidatorsErrors() { |
||
201 | $errors = array(); |
||
202 | foreach ($this->validators as $validator) { |
||
208 | |||
209 | |||
210 | /** |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function required() { |
||
216 | |||
217 | |||
218 | } |