1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Factory\Definition; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
8
|
|
|
|
9
|
|
|
use function get_class; |
10
|
|
|
use function gettype; |
11
|
|
|
use function is_array; |
12
|
|
|
use function is_callable; |
13
|
|
|
use function is_object; |
14
|
|
|
use function is_string; |
15
|
|
|
|
16
|
|
|
final class DefinitionValidator |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param mixed $definition |
20
|
|
|
* |
21
|
|
|
* @throws InvalidConfigException |
22
|
|
|
*/ |
23
|
28 |
|
public static function validate($definition, ?string $id = null): void |
24
|
|
|
{ |
25
|
|
|
// Reference or ready object |
26
|
28 |
|
if (is_object($definition)) { |
27
|
2 |
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// Class |
31
|
28 |
|
if (is_string($definition) && $definition !== '') { |
32
|
17 |
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Callable definition |
36
|
14 |
|
if (is_callable($definition, true)) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Array definition |
41
|
14 |
|
if (is_array($definition)) { |
42
|
14 |
|
self::validateArrayDefinition($definition, $id); |
43
|
6 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
throw new InvalidConfigException('Invalid definition:' . var_export($definition, true)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws InvalidConfigException |
51
|
|
|
*/ |
52
|
14 |
|
private static function validateArrayDefinition(array $definition, ?string $id): void |
53
|
|
|
{ |
54
|
14 |
|
foreach ($definition as $key => $value) { |
55
|
13 |
|
if (!is_string($key)) { |
56
|
1 |
|
throw new InvalidConfigException( |
57
|
1 |
|
sprintf( |
58
|
1 |
|
'Invalid definition: invalid key in array definition. Allow only string keys, got %d.', |
59
|
|
|
$key, |
60
|
|
|
), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Class |
65
|
13 |
|
if ($key === ArrayDefinition::CLASS_NAME) { |
66
|
13 |
|
if (!is_string($value)) { |
67
|
1 |
|
throw new InvalidConfigException( |
68
|
1 |
|
sprintf( |
69
|
1 |
|
'Invalid definition: invalid class name. Expected string, got %s.', |
70
|
1 |
|
self::getType($value), |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
} |
74
|
12 |
|
if ($value === '') { |
75
|
1 |
|
throw new InvalidConfigException('Invalid definition: empty class name.'); |
76
|
|
|
} |
77
|
11 |
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Constructor arguments |
81
|
10 |
|
if ($key === ArrayDefinition::CONSTRUCTOR) { |
82
|
2 |
|
if (!is_array($value)) { |
83
|
1 |
|
throw new InvalidConfigException( |
84
|
1 |
|
sprintf( |
85
|
1 |
|
'Invalid definition: incorrect constructor arguments. Expected array, got %s.', |
86
|
1 |
|
self::getType($value) |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
1 |
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Methods and properties |
94
|
8 |
|
if (substr($key, -2) === '()') { |
95
|
6 |
|
if (!is_array($value)) { |
96
|
1 |
|
throw new InvalidConfigException( |
97
|
1 |
|
sprintf( |
98
|
1 |
|
'Invalid definition: incorrect method arguments. Expected array, got %s.', |
99
|
1 |
|
self::getType($value) |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
5 |
|
continue; |
104
|
|
|
} |
105
|
3 |
|
if (strncmp($key, '$', 1) === 0) { |
106
|
1 |
|
continue; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
throw new InvalidConfigException( |
110
|
2 |
|
sprintf( |
111
|
2 |
|
'Invalid definition: key "%s" is not allowed. Did you mean "%s()" or "$%s"?', |
112
|
|
|
$key, |
113
|
|
|
$key, |
114
|
|
|
$key |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
7 |
|
if ($id === null && !isset($definition[ArrayDefinition::CLASS_NAME])) { |
120
|
1 |
|
throw new InvalidConfigException('Invalid definition: no class name specified.'); |
121
|
|
|
} |
122
|
6 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param mixed $value |
126
|
|
|
*/ |
127
|
3 |
|
private static function getType($value): string |
128
|
|
|
{ |
129
|
3 |
|
return is_object($value) ? get_class($value) : gettype($value); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|