|
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\Reflection\Validation\Base; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Exceptions\SchemaException; |
|
13
|
|
|
use Railt\SDL\Reflection\Validation\Validator; |
|
14
|
|
|
use Railt\SDL\Runtime\CallStack; |
|
15
|
|
|
use Railt\SDL\Runtime\CallStackInterface; |
|
16
|
|
|
use Railt\SDL\Support; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class BaseValidator |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class BaseValidator implements ValidatorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use Support; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Validator |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $validator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var CallStack |
|
32
|
|
|
*/ |
|
33
|
|
|
private $stack; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* BaseValidator constructor. |
|
42
|
|
|
* @param Validator $factory |
|
43
|
|
|
* @param CallStackInterface $stack |
|
44
|
|
|
* @param null|string $name |
|
45
|
|
|
*/ |
|
46
|
283 |
|
public function __construct(Validator $factory, CallStackInterface $stack, ?string $name) |
|
47
|
|
|
{ |
|
48
|
283 |
|
$this->validator = $factory; |
|
49
|
283 |
|
$this->stack = $stack; |
|
|
|
|
|
|
50
|
283 |
|
$this->name = $name ?? static::class; |
|
51
|
283 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
283 |
|
public function getGroupName(): string |
|
57
|
|
|
{ |
|
58
|
283 |
|
return $this->name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $group |
|
63
|
|
|
* @return ValidatorInterface |
|
64
|
|
|
* @throws \OutOfBoundsException |
|
65
|
|
|
*/ |
|
66
|
123 |
|
public function getValidator(string $group): ValidatorInterface |
|
67
|
|
|
{ |
|
68
|
123 |
|
return $this->validator->group($group); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return CallStack|CallStackInterface |
|
73
|
|
|
*/ |
|
74
|
6517 |
|
public function getCallStack(): CallStackInterface |
|
75
|
|
|
{ |
|
76
|
6517 |
|
return $this->stack; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $exception |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
* @param array ...$args |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
public function throw(string $exception, string $message, ...$args): void |
|
86
|
|
|
{ |
|
87
|
|
|
$message = \sprintf($message, ...$args); |
|
88
|
|
|
|
|
89
|
|
|
if (\is_subclass_of($exception, SchemaException::class)) { |
|
|
|
|
|
|
90
|
|
|
throw new $exception($message, $this->stack); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
throw new $exception($message); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.