1 | <?php |
||
26 | abstract class AbstractInput extends AbstractElement |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * @var int int |
||
31 | */ |
||
32 | protected $instances = 0; |
||
33 | |||
34 | /** |
||
35 | * @var string used in input id generation |
||
36 | */ |
||
37 | protected static $idPrefix = 'input-'; |
||
38 | |||
39 | /** |
||
40 | * @var Label|ElementInterface |
||
41 | */ |
||
42 | protected $label; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $required = false; |
||
48 | |||
49 | /** |
||
50 | * Add validation methods |
||
51 | */ |
||
52 | use ValidationAwareMethods; |
||
53 | |||
54 | /** |
||
55 | * Add filter methods |
||
56 | */ |
||
57 | use FilterAwareMethods; |
||
58 | |||
59 | /** |
||
60 | * Needed for label translation |
||
61 | */ |
||
62 | use TranslateMethods; |
||
63 | |||
64 | /** |
||
65 | * @var string Renderer class |
||
66 | */ |
||
67 | protected $rendererClass = Input::class; |
||
68 | |||
69 | /** |
||
70 | * Get input name |
||
71 | * |
||
72 | * @return mixed |
||
73 | */ |
||
74 | 66 | public function getName() |
|
78 | |||
79 | /** |
||
80 | * Sets input name |
||
81 | * |
||
82 | * @param string $name |
||
83 | * |
||
84 | * @return self|$this|InputInterface|AbstractInput |
||
85 | */ |
||
86 | 60 | public function setName($name) |
|
98 | |||
99 | /** |
||
100 | * Sets the input label |
||
101 | * |
||
102 | * Label parameter can be a string or a element interface. |
||
103 | * If a string is provided then an ElementInterface MUST be created. |
||
104 | * This element MUST result in a <label> HTML tag when rendering and |
||
105 | * as you may define your own implementation it is advisable that you |
||
106 | * use the Slick\Form\Element\Label object. |
||
107 | * |
||
108 | * @param string|ElementInterface $label |
||
109 | * |
||
110 | * @return self|$this|InputInterface |
||
111 | * |
||
112 | * @throws InvalidArgumentException If the provided label is not a string |
||
113 | * or is not an object of a class implementing the ElementInterface. |
||
114 | */ |
||
115 | 62 | public function setLabel($label) |
|
123 | |||
124 | /** |
||
125 | * Gets the input label element |
||
126 | * |
||
127 | * @return ElementInterface|Label |
||
128 | */ |
||
129 | 66 | public function getLabel() |
|
133 | |||
134 | /** |
||
135 | * Returns the input value as user entered it, without filtering |
||
136 | * |
||
137 | * @return mixed |
||
138 | */ |
||
139 | 28 | public function getRawValue() |
|
143 | |||
144 | /** |
||
145 | * Overrides the default behavior to add the value attribute |
||
146 | * |
||
147 | * @param mixed $value |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | 34 | public function setValue($value) |
|
155 | |||
156 | /** |
||
157 | * Generate input id based on provided name |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 56 | protected function generateId() |
|
170 | |||
171 | /** |
||
172 | * Check provided label |
||
173 | * |
||
174 | * @param string|ElementInterface $label |
||
175 | * |
||
176 | * @return ElementInterface|Label |
||
177 | */ |
||
178 | 62 | protected function checkLabel($label) |
|
186 | |||
187 | /** |
||
188 | * Creates label from string |
||
189 | * |
||
190 | * @param string $label |
||
191 | * |
||
192 | * @return ElementInterface|Label |
||
193 | */ |
||
194 | 58 | protected function createLabel($label) |
|
195 | { |
||
196 | 58 | if (!is_string($label)) { |
|
197 | 4 | throw new InvalidArgumentException( |
|
198 | "Provided label is not a string or an ElementInterface ". |
||
199 | "interface object." |
||
200 | 4 | ); |
|
201 | } |
||
202 | |||
203 | 54 | $element = class_exists($label) |
|
204 | 54 | ? $this->createLabelFromClass($label) |
|
205 | 52 | : new Label('', $label); |
|
206 | |||
207 | 52 | return $element; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Instantiates the provided ElementInterface class |
||
212 | * |
||
213 | * @param string $class |
||
214 | * |
||
215 | * @return ElementInterface|Label |
||
216 | */ |
||
217 | 4 | protected function createLabelFromClass($class) |
|
218 | { |
||
219 | 4 | if (! is_subclass_of($class, ElementInterface::class)) { |
|
220 | 2 | throw new InvalidArgumentException( |
|
221 | 2 | "Class '{$class}' does not implements ElementInterface and ". |
|
222 | "cannot be used as input label." |
||
223 | 2 | ); |
|
224 | } |
||
225 | |||
226 | 2 | $label = new $class('', ucfirst($this->getName())); |
|
227 | 2 | return $label; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Check if this input is required to be filled |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 42 | public function isRequired() |
|
239 | |||
240 | /** |
||
241 | * Sets the required flag for this input |
||
242 | * |
||
243 | * @param boolean $required |
||
244 | * |
||
245 | * @return $this|self|InputInterface |
||
246 | */ |
||
247 | 38 | public function setRequired($required) |
|
260 | |||
261 | /** |
||
262 | * Check if this input is for multiple usage |
||
263 | * |
||
264 | * @return boolean |
||
265 | */ |
||
266 | 66 | public function isMultiple() |
|
270 | |||
271 | /** |
||
272 | * If input is multiple get the instance it belongs |
||
273 | * |
||
274 | * @return int |
||
275 | */ |
||
276 | public function getInstance() |
||
280 | |||
281 | /** |
||
282 | * If input is multiple get the instance value of it |
||
283 | * |
||
284 | * @return mixed |
||
285 | */ |
||
286 | 10 | public function getInstanceValue() |
|
287 | { |
||
288 | 10 | $value = $this->getValue(); |
|
289 | if ( |
||
290 | 10 | is_array($value) && |
|
291 | array_key_exists($this->instances, $this->value) |
||
292 | 10 | ) { |
|
293 | $value = $value[$this->instances]; |
||
294 | } |
||
295 | 10 | return $value; |
|
296 | } |
||
297 | |||
298 | /** |
||
299 | * Returns the HTML string for current element |
||
300 | * |
||
301 | * @param array $context |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | 10 | public function render($context = []) |
|
314 | |||
315 | /** |
||
316 | * Updates input id and link it to its label |
||
317 | * |
||
318 | * @return self|AbstractInput |
||
319 | */ |
||
320 | protected function updateInstance() |
||
331 | } |
||
332 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.