|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
|
8
|
|
|
|
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
class DefinitionAssistant |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array<string, array<int, string>> |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $types = []; |
|
17
|
|
|
|
|
18
|
24 |
|
public static function IsTypeUnregistered(string $type) : bool |
|
19
|
|
|
{ |
|
20
|
24 |
|
if ( ! is_a($type, DaftObject::class, true)) { |
|
21
|
1 |
|
throw new InvalidArgumentException( |
|
22
|
|
|
'Argument 1 passed to ' . |
|
23
|
|
|
__METHOD__ . |
|
24
|
|
|
'() must be an instance of ' . |
|
25
|
|
|
DaftObject::class . |
|
26
|
|
|
', ' . |
|
27
|
1 |
|
$type . |
|
28
|
1 |
|
' given!' |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
23 |
|
return ! isset(self::$types[$type]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
public static function RegisterType(string $type, array $matrix) |
|
36
|
|
|
{ |
|
37
|
5 |
|
if ( ! static::IsTypeUnregistered($type)) { |
|
38
|
1 |
|
throw new InvalidArgumentException( |
|
39
|
|
|
'Argument 1 passed to ' . |
|
40
|
|
|
__METHOD__ . |
|
41
|
1 |
|
'() has already been registered!' |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
$initialCount = count($matrix); |
|
46
|
|
|
|
|
47
|
5 |
|
if ($initialCount < 1) { |
|
48
|
1 |
|
throw new InvalidArgumentException( |
|
49
|
|
|
'Argument 2 passed to ' . |
|
50
|
|
|
__METHOD__ . |
|
51
|
1 |
|
'() must be a non-empty array!' |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array<int, mixed> |
|
57
|
|
|
*/ |
|
58
|
4 |
|
$matrix = array_filter($matrix, 'is_int', ARRAY_FILTER_USE_KEY); |
|
59
|
|
|
|
|
60
|
4 |
|
if (count($matrix) !== $initialCount) { |
|
61
|
1 |
|
throw new InvalidArgumentException( |
|
62
|
|
|
'Argument 2 passed to ' . |
|
63
|
|
|
__METHOD__ . |
|
64
|
1 |
|
'() must be an array with only integer keys!' |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var array<int, string> |
|
70
|
|
|
*/ |
|
71
|
3 |
|
$matrix = array_filter($matrix, 'is_string'); |
|
72
|
|
|
|
|
73
|
3 |
|
if (count($matrix) !== $initialCount) { |
|
74
|
1 |
|
throw new InvalidArgumentException( |
|
75
|
|
|
'Argument 2 passed to ' . |
|
76
|
|
|
__METHOD__ . |
|
77
|
1 |
|
'() must be an array of shape array<int, string>!' |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
self::$types[$type] = $matrix; |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
20 |
|
public static function RegisterAbstractDaftObjectType(string $maybe) |
|
85
|
|
|
{ |
|
86
|
20 |
|
if ( ! is_a($maybe, AbstractDaftObject::class, true)) { |
|
87
|
1 |
|
throw new InvalidArgumentException( |
|
88
|
|
|
'Argument 1 passed to ' . |
|
89
|
|
|
__METHOD__ . |
|
90
|
|
|
'() must be an implementation of ' . |
|
91
|
|
|
AbstractDaftObject::class . |
|
92
|
|
|
', ' . |
|
93
|
1 |
|
$maybe . |
|
94
|
1 |
|
' given!' |
|
95
|
|
|
); |
|
96
|
19 |
|
} elseif ( ! static::IsTypeUnregistered($maybe)) { |
|
97
|
1 |
|
throw new InvalidArgumentException( |
|
98
|
|
|
'Argument 1 passed to ' . |
|
99
|
|
|
__METHOD__ . |
|
100
|
1 |
|
'() has already been registered!' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
19 |
|
self::$types[$maybe] = TypeCertainty::EnsureArgumentIsArray($maybe::PROPERTIES); |
|
105
|
19 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param scalar|object $maybe |
|
109
|
|
|
* |
|
110
|
|
|
* @return array<int, string> |
|
111
|
|
|
*/ |
|
112
|
20 |
|
public static function ObtainExpectedProperties($maybe) : array |
|
113
|
|
|
{ |
|
114
|
20 |
|
if (is_object($maybe)) { |
|
115
|
1 |
|
if ( ! ($maybe instanceof DaftObject)) { |
|
116
|
1 |
|
throw new InvalidArgumentException( |
|
117
|
|
|
'Argument 1 passed to ' . |
|
118
|
|
|
__METHOD__ . |
|
119
|
|
|
'() must be either a string or an instance of ' . |
|
120
|
|
|
DaftObject::class . |
|
121
|
|
|
', ' . |
|
122
|
1 |
|
get_class($maybe) . |
|
123
|
1 |
|
' given!' |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
19 |
|
} elseif ( ! is_string($maybe)) { |
|
127
|
1 |
|
throw new InvalidArgumentException( |
|
128
|
|
|
'Argument 1 passed to ' . |
|
129
|
|
|
__METHOD__ . |
|
130
|
|
|
'() must be either a string or an object, ' . |
|
131
|
1 |
|
gettype($maybe) . |
|
132
|
1 |
|
' given!' |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @var array<int, string> |
|
138
|
|
|
*/ |
|
139
|
18 |
|
$out = array_values(array_unique(array_reduce( |
|
140
|
18 |
|
array_filter( |
|
141
|
18 |
|
self::$types, |
|
142
|
18 |
|
function (string $type) use ($maybe) : bool { |
|
143
|
18 |
|
return is_a($maybe, $type, is_string($maybe)); |
|
144
|
18 |
|
}, |
|
145
|
18 |
|
ARRAY_FILTER_USE_KEY |
|
146
|
|
|
), |
|
147
|
18 |
|
'array_merge', |
|
148
|
18 |
|
[] |
|
149
|
|
|
))); |
|
150
|
|
|
|
|
151
|
18 |
|
return $out; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return array<int, string> |
|
156
|
|
|
*/ |
|
157
|
17 |
|
public static function ObtainExpectedOrDefaultPropertiesWithAutoRegister(string $class) : array |
|
158
|
|
|
{ |
|
159
|
|
|
if ( |
|
160
|
17 |
|
static::IsTypeUnregistered($class) && |
|
161
|
17 |
|
TypeParanoia::IsThingStrings($class, AbstractDaftObject::class) |
|
162
|
|
|
) { |
|
163
|
17 |
|
static::RegisterAbstractDaftObjectType($class); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @var array<int, string> |
|
168
|
|
|
*/ |
|
169
|
|
|
$out = |
|
170
|
17 |
|
! static::IsTypeUnregistered($class) |
|
171
|
17 |
|
? static::ObtainExpectedProperties($class) |
|
172
|
17 |
|
: $class::DaftObjectProperties(); |
|
173
|
|
|
|
|
174
|
17 |
|
return $out; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|