|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Backend; |
|
11
|
|
|
|
|
12
|
|
|
use JsonSchema\Constraints\Constraint; |
|
13
|
|
|
use Railt\Io\File; |
|
14
|
|
|
use Railt\Io\Readable; |
|
15
|
|
|
use Railt\SDL\Exception\InternalException; |
|
16
|
|
|
use Railt\SDL\IR\Definition; |
|
17
|
|
|
use JsonSchema\Validator as JsonSchema; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Validator |
|
21
|
|
|
*/ |
|
22
|
|
|
class Validator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public const RAILT_SDL_1_2 = __DIR__ . '/../../resources/rlsdl-1.2.json'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected const DEFAULT_VERSION = self::RAILT_SDL_1_2; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var JsonSchema |
|
36
|
|
|
*/ |
|
37
|
|
|
private $validator; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array|object |
|
41
|
|
|
*/ |
|
42
|
|
|
private $schema; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Validator constructor. |
|
46
|
|
|
* @param Readable|null $schema |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(Readable $schema = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->validator = new JsonSchema(); |
|
51
|
|
|
$this->schema = $this->loadJsonSchema($schema); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param null|Readable|File $schema |
|
56
|
|
|
* @return mixed|array|object |
|
57
|
|
|
*/ |
|
58
|
|
|
private function loadJsonSchema(?Readable $schema) |
|
59
|
|
|
{ |
|
60
|
|
|
$schema = $schema ?? File::fromPathname(static::DEFAULT_VERSION); |
|
61
|
|
|
|
|
62
|
|
|
return \json_decode($schema->getContents()); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Definition $definition |
|
67
|
|
|
* @return Definition |
|
68
|
|
|
* @throws InternalException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function validate(Definition $definition): Definition |
|
71
|
|
|
{ |
|
72
|
|
|
$json = $definition->toObject(); |
|
73
|
|
|
|
|
74
|
|
|
$this->validator->reset(); |
|
75
|
|
|
$this->validator->validate($json, $this->schema, Constraint::CHECK_MODE_VALIDATE_SCHEMA); |
|
76
|
|
|
|
|
77
|
|
|
if (! $this->validator->isValid()) { |
|
78
|
|
|
$error = \implode(\PHP_EOL, [ |
|
79
|
|
|
'An internal representation code errors was found: ', |
|
80
|
|
|
$this->errorsToString($this->validator->getErrors()) |
|
81
|
|
|
]); |
|
82
|
|
|
|
|
83
|
|
|
throw new InternalException($error); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $definition; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $errors |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
private function errorsToString(array $errors): string |
|
94
|
|
|
{ |
|
95
|
|
|
$applicator = function (array $error): string { |
|
96
|
|
|
if ($error['property']) { |
|
97
|
|
|
return \sprintf(' - [%s] %s.', $error['property'], $error['message']); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return \sprintf(' - %s.', $error['message']); |
|
101
|
|
|
}; |
|
102
|
|
|
|
|
103
|
|
|
return \implode(\PHP_EOL, \array_map($applicator, $errors)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: