This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * Caridea |
||
5 | * |
||
6 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
||
7 | * use this file except in compliance with the License. You may obtain a copy of |
||
8 | * the License at |
||
9 | * |
||
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
11 | * |
||
12 | * Unless required by applicable law or agreed to in writing, software |
||
13 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
||
14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
||
15 | * License for the specific language governing permissions and limitations under |
||
16 | * the License. |
||
17 | * |
||
18 | * @copyright 2015-2018 LibreWorks contributors |
||
19 | * @license Apache-2.0 |
||
20 | */ |
||
21 | namespace Caridea\Validate\Rule; |
||
22 | |||
23 | /** |
||
24 | * Rules for nested list and object validation. |
||
25 | * |
||
26 | * @copyright 2015-2018 LibreWorks contributors |
||
27 | * @license Apache-2.0 |
||
28 | */ |
||
29 | class Nested implements \Caridea\Validate\Draft |
||
30 | { |
||
31 | /** |
||
32 | * @var string The operator type |
||
33 | */ |
||
34 | private $operator; |
||
35 | /** |
||
36 | * @var \Caridea\Validate\Validator Optional. A validator for nested objects. |
||
37 | */ |
||
38 | private $validator; |
||
39 | /** |
||
40 | * @var string Optional field on object that chooses rules. |
||
41 | */ |
||
42 | private $field; |
||
43 | |||
44 | /** |
||
45 | * Creates a new NestedRule. |
||
46 | * |
||
47 | * @param string $operator The operator type |
||
48 | * @param mixed|\Caridea\Validate\Validator The validator to use, or definitions to create one |
||
49 | * @param string $field Optional field on object that chooses rules |
||
50 | */ |
||
51 | 5 | protected function __construct(string $operator, $validator, string $field = null) |
|
52 | { |
||
53 | 5 | $this->operator = $operator; |
|
54 | 5 | $this->validator = $validator; |
|
55 | 5 | $this->field = $field; |
|
56 | 5 | } |
|
57 | |||
58 | /** |
||
59 | * Finishes creating a rule using the parent builder. |
||
60 | * |
||
61 | * @param \Caridea\Validate\Registry $registry |
||
62 | * @return \Caridea\Validate\Rule The fully created rule |
||
63 | */ |
||
64 | 5 | public function finish(\Caridea\Validate\Registry $registry): \Caridea\Validate\Rule |
|
65 | { |
||
66 | 5 | if ($this->validator instanceof \Caridea\Validate\Validator) { |
|
67 | 5 | return $this; |
|
68 | } else { |
||
69 | 5 | $rule = clone $this; |
|
70 | 5 | $builder = $registry->builder(); |
|
71 | 5 | switch ($this->operator) { |
|
72 | 5 | case "nested_object": |
|
73 | 4 | case "list_objects": |
|
74 | 2 | $rule->validator = $builder->build($this->validator); |
|
75 | 2 | return $rule; |
|
76 | 3 | case "list": |
|
77 | 1 | $rule->validator = $builder->build((object)['entry' => $this->validator]); |
|
78 | 1 | return $rule; |
|
79 | 2 | case "list_different_objects": |
|
80 | 1 | case "variable_object": |
|
81 | 2 | $validators = []; |
|
82 | 2 | foreach ($this->validator as $value => $ruleset) { |
|
83 | 2 | $validators[$value] = $builder->build($ruleset); |
|
84 | } |
||
85 | 2 | $rule->validator = new \Caridea\Validate\SwitchValidator($this->field, $validators); |
|
86 | 2 | return $rule; |
|
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * {@inheritDoc} |
||
93 | */ |
||
94 | 8 | public function apply($value, $data = []): ?array |
|
95 | { |
||
96 | 8 | if (!($this->validator instanceof \Caridea\Validate\Validator)) { |
|
97 | 1 | throw new \BadMethodCallException("This rule is a Draft. Try calling the 'finish' method to get the full Rule."); |
|
98 | } |
||
99 | 7 | if (!is_array($value) && !($value instanceof \Traversable)) { |
|
100 | 1 | return ['FORMAT_ERROR']; |
|
101 | } |
||
102 | 6 | switch ($this->operator) { |
|
103 | 6 | case "nested_object": |
|
0 ignored issues
–
show
|
|||
104 | 4 | case "variable_object": |
|
105 | 3 | $result = $this->validator->validate($value); |
|
106 | 3 | return $result->hasErrors() ? $result->getErrors() : null; |
|
107 | 3 | case "list": |
|
108 | 1 | $errors = []; |
|
109 | 1 | foreach ($value as $entry) { |
|
110 | 1 | $result = $this->validator->validate(['entry' => $entry]); |
|
111 | 1 | $errors[] = $result->hasErrors() ? |
|
112 | 1 | $result->getErrors()['entry'] : null; |
|
113 | } |
||
114 | 1 | return array_filter($errors) ? $errors : null; |
|
115 | 2 | case "list_objects": |
|
116 | 1 | case "list_different_objects": |
|
117 | 2 | $errors = []; |
|
118 | 2 | foreach ($value as $entry) { |
|
119 | try { |
||
120 | 2 | $result = $this->validator->validate($entry); |
|
121 | 2 | $errors[] = $result->hasErrors() ? |
|
122 | 2 | $result->getErrors() : null; |
|
123 | 1 | } catch (\InvalidArgumentException $e) { |
|
124 | 2 | $errors[] = 'FORMAT_ERROR'; |
|
125 | } |
||
126 | } |
||
127 | 2 | return array_filter($errors) ? $errors : null; |
|
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Verifies an object value against separate validator rules. |
||
133 | * |
||
134 | * @param object $ruleset The validation ruleset |
||
135 | * @return \Caridea\Validate\Rule\Nested the created rule |
||
136 | */ |
||
137 | 1 | public static function nestedObject(\stdClass $ruleset): Nested |
|
138 | { |
||
139 | 1 | return new Nested("nested_object", $ruleset); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Verifies an object value using one of several validators based on a field value. |
||
144 | * |
||
145 | * @param string $field The deciding field name |
||
146 | * @param object $rulesets The rulesets |
||
147 | * @return \Caridea\Validate\Rule\Nested the created rule |
||
148 | */ |
||
149 | 1 | public static function variableObject(string $field, \stdClass $rulesets): Nested |
|
150 | { |
||
151 | 1 | return new Nested("variable_object", $rulesets, $field); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Verifies each entry in a list using one or more rules. |
||
156 | * |
||
157 | * @param mixed $rules The rule or rules to enforce |
||
158 | * @return \Caridea\Validate\Rule\Nested the created rule |
||
159 | */ |
||
160 | 1 | public static function listOf($rules): Nested |
|
161 | { |
||
162 | 1 | return new Nested("list", $rules); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * Verifies each entry in a list against separate validator rules. |
||
167 | * |
||
168 | * @param object $ruleset The validation ruleset |
||
169 | * @return \Caridea\Validate\Rule\Nested the created rule |
||
170 | */ |
||
171 | 1 | public static function listOfObjects(\stdClass $ruleset): Nested |
|
172 | { |
||
173 | 1 | return new Nested("list_objects", $ruleset); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Verifies each entry in a list using one of several validators based on a field value. |
||
178 | * |
||
179 | * @param string $field The deciding field name |
||
180 | * @param object $rulesets The rulesets |
||
181 | * @return \Caridea\Validate\Rule\Nested the created rule |
||
182 | */ |
||
183 | 1 | public static function listOfDifferentObjects(string $field, \stdClass $rulesets): Nested |
|
184 | { |
||
185 | 1 | return new Nested('list_different_objects', $rulesets, $field); |
|
186 | } |
||
187 | } |
||
188 |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.