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 | * Construct a new {@link ValidationField} object. |
||
31 | * |
||
32 | * @param Validation $validation The validation object that contains errors. |
||
33 | * @param array|Schema $field The field definition. |
||
34 | * @param string $name The path to the field. |
||
35 | */ |
||
36 | 117 | public function __construct(Validation $validation, $field, $name) { |
|
41 | |||
42 | /** |
||
43 | * Add a validation error. |
||
44 | * |
||
45 | * @param string $error The message code. |
||
46 | * @param int|array $options An array of additional information to add to the error entry or a numeric error code. |
||
47 | * @return $this |
||
48 | * @see Validation::addError() |
||
49 | */ |
||
50 | 18 | public function addError($error, $options = []) { |
|
51 | 18 | $this->validation->addError($this->getName(), $error, $options); |
|
52 | 18 | return $this; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * Add an invalid type error. |
||
57 | * |
||
58 | * @param string $type The type that was checked. |
||
59 | * @return $this |
||
60 | */ |
||
61 | 52 | public function addTypeError($type = '') { |
|
62 | 52 | $type = $type ?: $this->getType(); |
|
63 | |||
64 | 52 | $this->validation->addError( |
|
65 | 52 | $this->getName(), |
|
66 | 52 | 'invalid', |
|
67 | [ |
||
68 | 52 | 'type' => $type, |
|
69 | 52 | 'messageCode' => '{field} is not a valid {type}.', |
|
70 | 52 | 'status' => 422 |
|
71 | ] |
||
72 | ); |
||
73 | |||
74 | 52 | return $this; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Check whether or not this field is has errors. |
||
79 | * |
||
80 | * @return bool Returns true if the field has no errors, false otherwise. |
||
81 | */ |
||
82 | 50 | public function isValid() { |
|
83 | 50 | return $this->getValidation()->isValidField($this->getName()); |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Merge a validation object to this one. |
||
88 | * |
||
89 | * @param Validation $validation The validation object to merge. |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function merge(Validation $validation) { |
||
96 | |||
97 | /** |
||
98 | * Get the field. |
||
99 | * |
||
100 | * @return array|Schema Returns the field. |
||
101 | */ |
||
102 | 117 | public function getField() { |
|
105 | |||
106 | /** |
||
107 | * Set the field. |
||
108 | * |
||
109 | * This method is only meant to be called from within the schema class. |
||
110 | * |
||
111 | * @param array|Schema $field The new field. |
||
112 | * @return $this |
||
113 | */ |
||
114 | 76 | public function setField($field) { |
|
118 | |||
119 | /** |
||
120 | * Get the validation. |
||
121 | * |
||
122 | * @return Validation Returns the validation. |
||
123 | */ |
||
124 | 117 | public function getValidation() { |
|
127 | |||
128 | /** |
||
129 | * Get the name. |
||
130 | * |
||
131 | * @return string Returns the name. |
||
132 | */ |
||
133 | 117 | public function getName() { |
|
136 | |||
137 | /** |
||
138 | * Set the name. |
||
139 | * |
||
140 | * This method is only meant to be called from within the schema class. |
||
141 | * |
||
142 | * @param string $name The new name. |
||
143 | * @return $this |
||
144 | */ |
||
145 | 76 | public function setName($name) { |
|
149 | |||
150 | /** |
||
151 | * Get the field type. |
||
152 | * |
||
153 | * @return string|null Returns a type string or null if there isn't one. |
||
154 | */ |
||
155 | 117 | public function getType() { |
|
158 | |||
159 | /** |
||
160 | * Get a value fom the field. |
||
161 | * |
||
162 | * @param string $key The key to look at. |
||
163 | * @param mixed $default The default to return if the key isn't found. |
||
164 | * @return mixed Returns a value or the default. |
||
165 | */ |
||
166 | 116 | public function val($key, $default = null) { |
|
169 | |||
170 | /** |
||
171 | * Whether or not the field has a value. |
||
172 | * |
||
173 | * @param string $key The key to look at. |
||
174 | * @return bool Returns **true** if the field has a key or **false** otherwise. |
||
175 | */ |
||
176 | 20 | public function hasVal($key) { |
|
179 | |||
180 | /** |
||
181 | * Get the error count for this field. |
||
182 | */ |
||
183 | 49 | public function getErrorCount() { |
|
186 | } |
||
187 |
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.