-
Notifications
You must be signed in to change notification settings - Fork 9
/
Volan.php
273 lines (224 loc) · 7.94 KB
/
Volan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
namespace Volan;
use \Psr\Log\LoggerInterface;
class Volan
{
/**
* @var array
*/
private $error;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger = null;
/**
* @var bool
*/
private $strictMode;
/**
* @var string
*/
private $currentNode = '';
/**
* @var array
*/
private $schema = [];
const ERROR_NODE_HAS_EXCESSIVE_KEYS = 1;
const ERROR_NODE_HAS_NO_FIELD_TYPE = 2;
const ERROR_SCHEMA_HAS_NO_ROOT_ELEMENT = 3;
const ERROR_VALIDATOR_CLASS_NOT_FOUND = 4;
const ERROR_REQUIRED_FIELD_IS_EMPTY = 5;
const ERROR_NODE_IS_NOT_VALID = 6;
const ERROR_NESTED_ELEMENT_NOT_VALID = 7;
/**
* @return array
*/
public function getErrorInfo()
{
return $this->error;
}
/**
* @param array $schema
* @param bool $strictMode
*/
public function __construct($schema, $strictMode = true)
{
$this->schema = $schema;
$this->strictMode = $strictMode;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function log($message)
{
if (!is_null($this->logger)):
$this->logger->info($message);
endif;
}
/**
* @param string $message
* @param int $code
*/
private function setError($message, $code)
{
$this->error = ['code' => $code, 'error' => $message];
}
/**
* @param array $arr
*
* @return bool
*/
public function validate($arr)
{
$returnValue = true;
try {
if (empty($this->schema['root'])):
throw new \Exception('Sorry no root element in schema', self::ERROR_SCHEMA_HAS_NO_ROOT_ELEMENT);
endif;
$this->validateNode('root', new CustomArrayObject($this->schema), $arr);
} catch (\Exception $exc) {
$this->setError($exc->getMessage(), $exc->getCode());
$this->log($exc->getMessage());
$returnValue = false;
}
return $returnValue;
}
/**
* @param string $node
* @param \Volan\CustomArrayObject $schema
* @param mixed $element
*
* @throws \Exception
*/
private function validateNode($node, CustomArrayObject $schema, $element = [])
{
$nodeSchema = new CustomArrayObject($schema[$node]);
foreach ($nodeSchema->getArrayKeys() as $key):
$this->currentNode = $node.'.'.$key;
$this->log("We in element: {$this->currentNode}");
$nodeData = isset($element[$key]) ? $element[$key] : null;
$this->validatingTypeField($nodeSchema[$key]);
$validator = $this->getValidatorClass($nodeSchema[$key]);
$this->validateRequiredField($validator, $nodeData);
if ($validator->isRequired() === false && empty($nodeData)):
$this->log("Element: {$this->currentNode} has empty nonrequired data. We skip other check");
continue;
endif;
$this->validatingExcessiveKeys($validator, new CustomArrayObject($nodeSchema[$key]), $nodeData);
$this->validateField($validator, new CustomArrayObject($nodeSchema[$key]), $nodeData);
$this->validateNestedField($validator, $nodeData);
if ($validator->isNested()):
$this->log("Element: {$this->currentNode} is has children");
foreach ($nodeData as $record):
$this->validateNode($key, $nodeSchema, $record);
endforeach;
else:
$this->validateNode($key, $nodeSchema, $nodeData);
endif;
$this->log("Element: {$this->currentNode} finished checking successfully.");
endforeach;
}
/**
* @param \Volan\CustomArrayObject $nodeSchema
* @param array $nodeData
*
* @return bool
*/
private function isChildElementHasStrictKeys(\Volan\CustomArrayObject $nodeSchema, $nodeData)
{
$returnValue = false;
if (!empty($nodeData) && is_array($nodeData)):
$schemaKeys = $nodeSchema->getArrayKeys();
$dataKeys = array_keys($nodeData);
$returnValue = (bool) array_diff($dataKeys, $schemaKeys);
endif;
return $returnValue;
}
/**
* @param \Volan\Validator\AbstractValidator $validator
* @param \Volan\CustomArrayObject $schema
* @param mixed $nodeData
*
* @throws \Exception
*/
private function validatingExcessiveKeys(\Volan\Validator\AbstractValidator $validator, CustomArrayObject $schema, $nodeData = null)
{
if (!$validator->isNested() && $this->strictMode && $this->isChildElementHasStrictKeys($schema, $nodeData)):
throw new \Exception("Sorry {$this->currentNode} element has excessive keys", self::ERROR_NODE_HAS_EXCESSIVE_KEYS);
endif;
if ($validator->isNested() && $this->strictMode):
foreach ($nodeData as $record):
if ($this->isChildElementHasStrictKeys($schema, $record)):
throw new \Exception("Sorry children of element: {$this->currentNode} has excessive keys", self::ERROR_NODE_HAS_EXCESSIVE_KEYS);
endif;
endforeach;
endif;
}
/**
* @param array $node
*
* @throws \Exception
*/
private function validatingTypeField($node)
{
if (empty($node['_type'])):
throw new \Exception("Sorry element: {$this->currentNode} has no compulsory field: _type", self::ERROR_NODE_HAS_NO_FIELD_TYPE);
endif;
$this->log("Element: {$this->currentNode} has field: _type");
}
/**
* @param \Volan\Validator\AbstractValidator $validator
* @param mixed $nodeData
*
* @throws \Exception
*/
private function validateRequiredField(\Volan\Validator\AbstractValidator $validator, $nodeData = null)
{
if ($validator->isRequired() && empty($nodeData)):
throw new \Exception("Sorry {$this->currentNode} element has flag *required*", self::ERROR_REQUIRED_FIELD_IS_EMPTY);
endif;
$this->log('*required* check passed');
}
/**
* @param array $node
*
* @return \Volan\Validator\AbstractValidator
*
* @throws \Exception
*/
private function getValidatorClass($node)
{
$nodeTypeClassString = '\Volan\Validator\\'.$node['_type'].'_validator';
if (!class_exists($nodeTypeClassString)):
throw new \Exception("Sorry validator class {$nodeTypeClassString} not found", self::ERROR_VALIDATOR_CLASS_NOT_FOUND);
endif;
$this->log("validatot class $nodeTypeClassString exists");
return new $nodeTypeClassString();
}
/**
* @param \Volan\Validator\AbstractValidator $validator
* @param \Volan\CustomArrayObject $schema
* @param mixed $nodeData
*
* @throws \Exception
*/
private function validateField(\Volan\Validator\AbstractValidator $validator, CustomArrayObject $schema, $nodeData = null)
{
if ($validator->isValid($schema, $nodeData) === false):
throw new \Exception("Sorry {$this->currentNode} element has invalid associated data", self::ERROR_NODE_IS_NOT_VALID);
endif;
}
/**
* @param \Volan\Validator\AbstractValidator $validator
* @param mixed $nodeData
*
* @throws \Exception
*/
private function validateNestedField(\Volan\Validator\AbstractValidator $validator, $nodeData)
{
if ($validator->isNested() && !is_array($nodeData[0])):
throw new \Exception("{$this->currentNode} element supposed to be nested but it is not", self::ERROR_NESTED_ELEMENT_NOT_VALID);
endif;
}
}