1 | <?php |
||
13 | class ValidationField { |
||
14 | /** |
||
15 | * @var array|Schema |
||
16 | */ |
||
17 | private $field; |
||
18 | |||
19 | /** |
||
20 | * @var Validation |
||
21 | */ |
||
22 | private $validation; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $name; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | private $sparse; |
||
33 | |||
34 | /** |
||
35 | * Construct a new {@link ValidationField} object. |
||
36 | * |
||
37 | * @param Validation $validation The validation object that contains errors. |
||
38 | * @param array|Schema $field The field definition. |
||
39 | * @param string $name The path to the field. |
||
40 | * @param bool $sparse Whether this is a sparse validation or not. |
||
41 | */ |
||
42 | 144 | public function __construct(Validation $validation, $field, $name, $sparse = false) { |
|
48 | |||
49 | /** |
||
50 | * Add a validation error. |
||
51 | * |
||
52 | * @param string $error The message code. |
||
53 | * @param int|array $options An array of additional information to add to the error entry or a numeric error code. |
||
54 | * @return $this |
||
55 | * @see Validation::addError() |
||
56 | */ |
||
57 | 22 | public function addError($error, $options = []) { |
|
61 | |||
62 | /** |
||
63 | * Add an invalid type error. |
||
64 | * |
||
65 | * @param string $type The type that was checked. |
||
66 | * @return $this |
||
67 | */ |
||
68 | 38 | public function addTypeError($type = '') { |
|
83 | |||
84 | /** |
||
85 | * Check whether or not this field is has errors. |
||
86 | * |
||
87 | * @return bool Returns true if the field has no errors, false otherwise. |
||
88 | */ |
||
89 | 63 | public function isValid() { |
|
92 | |||
93 | /** |
||
94 | * Merge a validation object to this one. |
||
95 | * |
||
96 | * @param Validation $validation The validation object to merge. |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function merge(Validation $validation) { |
||
103 | |||
104 | /** |
||
105 | * Get the field. |
||
106 | * |
||
107 | * @return array|Schema Returns the field. |
||
108 | */ |
||
109 | 144 | public function getField() { |
|
112 | |||
113 | /** |
||
114 | * Set the field. |
||
115 | * |
||
116 | * This method is only meant to be called from within the schema class. |
||
117 | * |
||
118 | * @param array|Schema $field The new field. |
||
119 | * @return $this |
||
120 | */ |
||
121 | 79 | public function setField($field) { |
|
125 | |||
126 | /** |
||
127 | * Get the validation. |
||
128 | * |
||
129 | * @return Validation Returns the validation. |
||
130 | */ |
||
131 | 144 | public function getValidation() { |
|
134 | |||
135 | /** |
||
136 | * Get the name. |
||
137 | * |
||
138 | * @return string Returns the name. |
||
139 | */ |
||
140 | 144 | public function getName() { |
|
143 | |||
144 | /** |
||
145 | * Set the name. |
||
146 | * |
||
147 | * This method is only meant to be called from within the schema class. |
||
148 | * |
||
149 | * @param string $name The new name. |
||
150 | * @return $this |
||
151 | */ |
||
152 | 84 | public function setName($name) { |
|
156 | |||
157 | /** |
||
158 | * Get the field type. |
||
159 | * |
||
160 | * @return string|string[]|null Returns a type string, array of type strings, or null if there isn't one. |
||
161 | */ |
||
162 | 144 | public function getType() { |
|
165 | |||
166 | /** |
||
167 | * Whether or not the field has a given type. |
||
168 | * |
||
169 | * @param string $type The single type to test. |
||
170 | * @return bool Returns **true** if the field has the given type or **false** otherwise. |
||
171 | */ |
||
172 | 27 | public function hasType($type) { |
|
173 | 27 | return in_array($type, (array)$this->getType()); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get a value fom the field. |
||
178 | * |
||
179 | * @param string $key The key to look at. |
||
180 | * @param mixed $default The default to return if the key isn't found. |
||
181 | * @return mixed Returns a value or the default. |
||
182 | */ |
||
183 | 144 | public function val($key, $default = null) { |
|
186 | |||
187 | /** |
||
188 | * Whether or not the field has a value. |
||
189 | * |
||
190 | * @param string $key The key to look at. |
||
191 | * @return bool Returns **true** if the field has a key or **false** otherwise. |
||
192 | */ |
||
193 | 18 | public function hasVal($key) { |
|
196 | |||
197 | /** |
||
198 | * Get the error count for this field. |
||
199 | */ |
||
200 | 61 | public function getErrorCount() { |
|
203 | |||
204 | /** |
||
205 | * Whether or not this is a sparse validation.. |
||
206 | * |
||
207 | * @return bool Returns **true** if this is a sparse validation or **false** otherwise. |
||
208 | */ |
||
209 | 3 | public function isSparse() { |
|
212 | } |
||
213 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.