|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AlgoWeb\ODataMetadata\Edm\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use AlgoWeb\ODataMetadata\Asserts; |
|
8
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement; |
|
9
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\ILocation; |
|
10
|
|
|
use AlgoWeb\ODataMetadata\Interfaces\IModel; |
|
11
|
|
|
use ReflectionFunction; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use ReflectionNamedType; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Context that records errors reported by validation rules. |
|
17
|
|
|
* |
|
18
|
|
|
* @package AlgoWeb\ODataMetadata\Edm\Validation |
|
19
|
|
|
*/ |
|
20
|
|
|
final class ValidationContext |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var EdmError[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $errors = []; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var IModel |
|
28
|
|
|
*/ |
|
29
|
|
|
private $model; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var callable(object): bool |
|
32
|
|
|
*/ |
|
33
|
|
|
private $isBad; |
|
34
|
|
|
/** @noinspection PhpDocMissingThrowsInspection throws only in assert*/ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ValidationContext constructor. |
|
38
|
|
|
* @param IModel $model |
|
39
|
|
|
* @param callable(IEdmElement): bool $isBad |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \ReflectionException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(IModel $model, callable $isBad) |
|
44
|
|
|
{ |
|
45
|
|
|
assert( |
|
46
|
|
|
Asserts::assertSignatureMatches( |
|
47
|
|
|
function (/** @scrutinizer ignore-unused */IEdmElement $one): bool { |
|
48
|
|
|
return false; |
|
49
|
|
|
}, |
|
50
|
|
|
$isBad, |
|
51
|
|
|
'$isBad' |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$stem = is_array($isBad) ? new ReflectionMethod(...$isBad) : new ReflectionFunction($isBad); |
|
|
|
|
|
|
56
|
|
|
$stemReturnType = $stem->getReturnType(); |
|
57
|
|
|
$stemName = $stemReturnType instanceof ReflectionNamedType ? |
|
58
|
|
|
$stemReturnType->getName() : |
|
59
|
|
|
strval($stemReturnType); |
|
60
|
|
|
$stemParmType = $stem->getParameters()[0]->getType(); |
|
61
|
|
|
$stemParmName = $stemParmType instanceof ReflectionNamedType ? |
|
62
|
|
|
$stemParmType->getName() : |
|
63
|
|
|
strval($stemParmType); |
|
64
|
|
|
|
|
65
|
|
|
/* @noinspection PhpUnhandledExceptionInspection suppressing exceptions for asserts. */ |
|
66
|
|
|
assert( |
|
67
|
|
|
IEdmElement::class === $stemParmName, |
|
68
|
|
|
'$isBad should be a callable taking one parameter of Type IEdmElement' |
|
69
|
|
|
); |
|
70
|
|
|
/* @noinspection PhpUnhandledExceptionInspection suppressing exceptions for asserts. */ |
|
71
|
|
|
assert( |
|
72
|
|
|
'bool' === $stemName, |
|
73
|
|
|
'$isBad should be a callable returning a boolean' |
|
74
|
|
|
); |
|
75
|
|
|
$this->model = $model; |
|
76
|
|
|
$this->isBad = $isBad; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return IModel gets the model being validated |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getModel(): IModel |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->model; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return EdmError[] |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getErrors(): array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->errors; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Method returns true if the element is known to have structural errors associated with it. |
|
97
|
|
|
* |
|
98
|
|
|
* @param IEdmElement $element the element to test |
|
99
|
|
|
* @return bool true if the element has structural errors associated with it |
|
100
|
|
|
*/ |
|
101
|
|
|
public function checkIsBad(IEdmElement $element): bool |
|
102
|
|
|
{ |
|
103
|
|
|
$callable = $this->isBad; |
|
104
|
|
|
return $callable($element); |
|
105
|
|
|
} |
|
106
|
|
|
/** |
|
107
|
|
|
* Register an error with the validation context. |
|
108
|
|
|
* |
|
109
|
|
|
* @param ILocation $location location of the error |
|
110
|
|
|
* @param EdmErrorCode $errorCode value representing the error |
|
111
|
|
|
* @param string $errorMessage message text describing the error |
|
112
|
|
|
*/ |
|
113
|
|
|
public function addError(ILocation $location, EdmErrorCode $errorCode, string $errorMessage): void |
|
114
|
|
|
{ |
|
115
|
|
|
$this->addRawError(new EdmError($location, $errorCode, $errorMessage)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Register an error with the validation context. |
|
120
|
|
|
* |
|
121
|
|
|
* @param EdmError $error error to register |
|
122
|
|
|
*/ |
|
123
|
|
|
public function addRawError(EdmError $error): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->errors[] = $error; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|