1 | <?php |
||
28 | trait ValidationAwareMethods |
||
29 | { |
||
30 | /** |
||
31 | * @var ValidationChainInterface |
||
32 | */ |
||
33 | protected $validationChain; |
||
34 | |||
35 | /** |
||
36 | * @var boolean |
||
37 | */ |
||
38 | protected $valid = true; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $invalidInstances = []; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $context = []; |
||
49 | |||
50 | /** |
||
51 | * Gets the value to be validated |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | abstract public function getValue(); |
||
56 | |||
57 | /** |
||
58 | * Check if the value is valid |
||
59 | * |
||
60 | * The value should pass through all validators in the validation chain |
||
61 | * |
||
62 | * @return boolean True if is valid, false otherwise |
||
63 | */ |
||
64 | 16 | public function isValid() |
|
72 | |||
73 | /** |
||
74 | * Validates when input is set to be multiple |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | protected function isMultipleValid() |
||
86 | |||
87 | /** |
||
88 | * Validates current value so that isValid can retrieve the result of |
||
89 | * the validation(s) |
||
90 | * |
||
91 | * @return self|$this|ValidationAwareInterface |
||
92 | */ |
||
93 | 6 | public function validate() |
|
105 | |||
106 | /** |
||
107 | * Validates input when is set to be multiple |
||
108 | * |
||
109 | * @param array $values |
||
110 | * @param array $context |
||
111 | */ |
||
112 | protected function validateArray(array $values, $context) |
||
122 | |||
123 | /** |
||
124 | * Mark instance as invalid |
||
125 | * |
||
126 | * @param int $key |
||
127 | * |
||
128 | * @return $this|self |
||
|
|||
129 | */ |
||
130 | protected function setInvalid($key) |
||
135 | |||
136 | /** |
||
137 | * Returns the validation chain for this input |
||
138 | * |
||
139 | * @return ValidationChainInterface |
||
140 | */ |
||
141 | 38 | public function getValidationChain() |
|
148 | |||
149 | /** |
||
150 | * Sets input validation chain |
||
151 | * |
||
152 | * @param ValidationChainInterface $chain |
||
153 | * |
||
154 | * @return self|$this|ValidationAwareInterface |
||
155 | */ |
||
156 | 40 | public function setValidationChain(ValidationChainInterface $chain) |
|
161 | |||
162 | /** |
||
163 | * Adds a validator to the validation chain |
||
164 | * |
||
165 | * The validator param could be a known validator alias, a FQ |
||
166 | * ValidatorInterface class name or an object implementing |
||
167 | * ValidatorInterface. |
||
168 | * |
||
169 | * @param string|ValidatorInterface $validator |
||
170 | * @param string|array $message Error message and possible contexts |
||
171 | * variables. |
||
172 | * |
||
173 | * @return self|$this|ValidationAwareInterface|ElementInterface |
||
174 | * |
||
175 | * @throws InvalidArgumentException If the provided validator is an unknown |
||
176 | * validator alias or not a valid class name or the object passed |
||
177 | * does not implement the ValidatorInterface interface. |
||
178 | */ |
||
179 | 36 | public function addValidator($validator, $message = null) |
|
202 | |||
203 | /** |
||
204 | * Sets the required flag for this input |
||
205 | * |
||
206 | * @param boolean $required |
||
207 | * |
||
208 | * @return $this|self|InputInterface |
||
209 | */ |
||
210 | abstract public function setRequired($required); |
||
211 | |||
212 | /** |
||
213 | * If input is multiple get the instance it belongs |
||
214 | * |
||
215 | * @return int |
||
216 | */ |
||
217 | abstract public function getInstance(); |
||
218 | |||
219 | /** |
||
220 | * Check if this input is for multiple usage |
||
221 | * |
||
222 | * @return boolean |
||
223 | */ |
||
224 | abstract public function isMultiple(); |
||
225 | |||
226 | /** |
||
227 | * Check if input is being rendered |
||
228 | * |
||
229 | * @return boolean |
||
230 | */ |
||
231 | abstract public function isRendering(); |
||
232 | |||
233 | } |
||
234 |
In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.
If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.