1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace HSkrasek\JSONSchema; |
4
|
|
|
|
5
|
|
|
use League\JsonGuard\ValidationError; |
6
|
|
|
use League\JsonGuard\Validator as JsonGuardValidator; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
|
9
|
|
|
class Validator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var JsonGuardValidator |
13
|
|
|
*/ |
14
|
|
|
private $validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Validator constructor. |
18
|
|
|
* |
19
|
|
|
* @param mixed $data |
20
|
|
|
* @param object $schema |
21
|
|
|
* @param null|ContainerInterface $ruleSet |
22
|
|
|
*/ |
23
|
6 |
|
public function __construct($data, $schema, ?ContainerInterface $ruleSet = null) |
24
|
|
|
{ |
25
|
6 |
|
$this->validator = new JsonGuardValidator($data, $schema, $ruleSet); |
26
|
6 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Determines if the data passes schema validation. |
30
|
|
|
* |
31
|
|
|
* @return bool |
32
|
|
|
* |
33
|
|
|
* @throws \League\JsonGuard\Exception\InvalidSchemaException |
34
|
|
|
* @throws \League\JsonGuard\Exception\MaximumDepthExceededException |
35
|
|
|
*/ |
36
|
3 |
|
public function passes(): bool |
37
|
|
|
{ |
38
|
3 |
|
return $this->validator->passes(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determines if the data does not pass schema validation. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
* |
46
|
|
|
* @throws \League\JsonGuard\Exception\InvalidSchemaException |
47
|
|
|
* @throws \League\JsonGuard\Exception\MaximumDepthExceededException |
48
|
|
|
*/ |
49
|
6 |
|
public function fails(): bool |
50
|
|
|
{ |
51
|
6 |
|
return $this->validator->fails(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array|ValidationError[] |
56
|
|
|
* |
57
|
|
|
* @throws \League\JsonGuard\Exception\InvalidSchemaException |
58
|
|
|
* @throws \League\JsonGuard\Exception\MaximumDepthExceededException |
59
|
|
|
*/ |
60
|
3 |
|
public function errors(): array |
61
|
|
|
{ |
62
|
3 |
|
return $this->validator->errors(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get a Problem Details object describing the errors with the validated data |
67
|
|
|
* |
68
|
|
|
* @param null|string $title |
69
|
|
|
* @param int|null $status |
70
|
|
|
* @param null|string $detail |
71
|
|
|
* |
72
|
|
|
* @return ProblemDetails |
73
|
|
|
*/ |
74
|
3 |
|
public function getProblemDetails( |
75
|
|
|
?string $title = null, |
76
|
|
|
?int $status = null, |
77
|
|
|
?string $detail = null |
78
|
|
|
): ProblemDetails { |
79
|
3 |
|
return new ProblemDetails( |
80
|
3 |
|
$title ?? 'Validation errors were found', |
81
|
3 |
|
$status ?? 400, |
82
|
3 |
|
$detail, |
83
|
3 |
|
['invalid-params' => $this->getContextForProblemDetails()] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
3 |
|
private function getContextForProblemDetails(): array |
91
|
|
|
{ |
92
|
|
|
return array_reduce($this->errors(), function (array $carry, ValidationError $error) { |
93
|
3 |
|
$carry[] = [ |
94
|
3 |
|
'name' => $error->getDataPath(), |
95
|
3 |
|
'reason' => $error->getMessage(), |
96
|
3 |
|
'pointer' => $error->getSchemaPath(), |
97
|
|
|
]; |
98
|
|
|
|
99
|
3 |
|
return $carry; |
100
|
3 |
|
}, []); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|