1 | <?php |
||
2 | |||
3 | namespace TS\PHPValidator; |
||
4 | |||
5 | use Psr\Http\Message\ServerRequestInterface as Request; |
||
6 | use Respect\Validation\Exceptions\NestedValidationException; |
||
7 | |||
8 | class Validator |
||
9 | { |
||
10 | /** |
||
11 | * @var array values |
||
12 | */ |
||
13 | protected $values = []; |
||
14 | |||
15 | /** |
||
16 | * @var array errors |
||
17 | */ |
||
18 | protected $errors = []; |
||
19 | |||
20 | /** |
||
21 | * @var bool Use Session variable to store data |
||
22 | * If set to true, use ValidationMiddleware to extract the error from the Session. |
||
23 | */ |
||
24 | protected $useSession; |
||
25 | |||
26 | /** |
||
27 | * Validator constructor. |
||
28 | * @param array $config |
||
29 | */ |
||
30 | public function __construct(array $config = []) |
||
31 | { |
||
32 | $this->useSession = $config['useSession'] ?? false; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @param Request|array|object $params |
||
37 | * @param array $rules |
||
38 | * @param mixed $default |
||
39 | * @return $this |
||
40 | * @throws \Exception |
||
41 | */ |
||
42 | public function validate($params, array $rules, $default = null): Validator |
||
43 | { |
||
44 | if ($params instanceof Request) { |
||
45 | return $this->validateRequest($params, $rules, $default); |
||
46 | } elseif (is_array($params)) { |
||
47 | return $this->validateArray($params, $rules, $default); |
||
48 | } elseif (is_object($params)) { |
||
49 | return $this->validateObject($params, $rules, $default); |
||
50 | } |
||
51 | |||
52 | throw new \Exception('Unknown type given for $params.'); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param Request $request |
||
57 | * @param array $rules |
||
58 | * @param mixed $default |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function validateRequest(Request $request, array $rules, $default = null) |
||
62 | { |
||
63 | $params = $request->getParsedBody() ?? []; |
||
64 | |||
65 | //Call back validate function since params can be an object or array |
||
66 | return $this->validate($params, $rules, $default); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param object $object |
||
71 | * @param array $rules |
||
72 | * @param mixed $default |
||
73 | * @return $this |
||
74 | */ |
||
75 | public function validateObject(object $object, array $rules, $default = null) |
||
76 | { |
||
77 | $params = get_object_vars($object); |
||
78 | return $this->runValidation($params, $rules, $default); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param array $params |
||
83 | * @param array $rules |
||
84 | * @param mixed $default |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function validateArray(array $params, array $rules, $default = null) |
||
88 | { |
||
89 | return $this->runValidation($params, $rules, $default); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param array $params |
||
94 | * @param array $rules |
||
95 | * @param mixed $default |
||
96 | * @return Validator |
||
97 | */ |
||
98 | protected function runValidation(array $params, array $rules, $default = null) |
||
99 | { |
||
100 | foreach ($rules as $field => $rule) { |
||
101 | try { |
||
102 | $param = isset($params[$field]) ? $params[$field] : $default; |
||
103 | $this->values[$field] = $param; |
||
104 | $rule->setName(ucfirst($field))->assert($param); |
||
105 | } catch (NestedValidationException $e) { |
||
106 | $this->errors[$field] = $e->getMessages(); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | if ($this->failed() && $this->useSession) { |
||
111 | $_SESSION['TS_PHPValidator_Errors'] = $this->errors; |
||
112 | $_SESSION['TS_PHPValidator_Values'] = $this->values; |
||
113 | } |
||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Validation failed? |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function failed(): bool |
||
122 | { |
||
123 | return !empty($this->errors); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Is the validation valid. |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function isValid(): bool |
||
131 | { |
||
132 | return empty($this->errors); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param array $errors |
||
137 | */ |
||
138 | public function setErrors(array $errors) |
||
139 | { |
||
140 | $this->errors = $errors; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get all errors |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getErrors(): array |
||
148 | { |
||
149 | return $this->errors; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $values |
||
154 | */ |
||
155 | public function setValues(array $values) |
||
156 | { |
||
157 | $this->values = $values; |
||
158 | } |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Set value by key |
||
163 | * @param $key |
||
164 | * @param mixed $default |
||
165 | */ |
||
166 | public function setValue($key, $value) |
||
167 | { |
||
168 | $this->values[$key] = $value; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get all values |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getValues(): array |
||
176 | { |
||
177 | return $this->values; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get value by key |
||
182 | * @param $key |
||
183 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
184 | * @return mixed|null |
||
185 | */ |
||
186 | public function getValue($key, $default = null) |
||
187 | { |
||
188 | return isset($this->values[$key]) ? $this->values[$key] : $default; |
||
189 | } |
||
190 | } |
||
191 |