|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
|
8
|
|
|
|
|
9
|
|
|
use Closure; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use SignpostMarv\DaftMagicPropertyAnalysis\DefinitionAssistant as Base; |
|
12
|
|
|
|
|
13
|
|
|
class DefinitionAssistant extends Base |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array<string, array<int, string>> |
|
17
|
|
|
*/ |
|
18
|
|
|
protected static $types = []; |
|
19
|
|
|
|
|
20
|
50 |
|
public static function IsTypeUnregistered(string $type) : bool |
|
21
|
|
|
{ |
|
22
|
50 |
|
if ( ! is_a($type, DaftObject::class, true)) { |
|
23
|
4 |
|
throw new InvalidArgumentException( |
|
24
|
|
|
'Argument 1 passed to ' . |
|
25
|
|
|
__METHOD__ . |
|
26
|
|
|
'() must be an implementation of ' . |
|
27
|
|
|
DaftObject::class . |
|
28
|
|
|
', ' . |
|
29
|
4 |
|
$type . |
|
30
|
4 |
|
' given!' |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
46 |
|
return parent::IsTypeUnregistered($type); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
38 |
|
public static function RegisterAbstractDaftObjectType(string $maybe) : void |
|
38
|
|
|
{ |
|
39
|
38 |
|
if ( ! is_a($maybe, AbstractDaftObject::class, true)) { |
|
40
|
2 |
|
throw new InvalidArgumentException( |
|
41
|
|
|
'Argument 1 passed to ' . |
|
42
|
|
|
__METHOD__ . |
|
43
|
|
|
'() must be an implementation of ' . |
|
44
|
|
|
AbstractDaftObject::class . |
|
45
|
|
|
', ' . |
|
46
|
2 |
|
$maybe . |
|
47
|
2 |
|
' given!' |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array<int, string> |
|
53
|
|
|
*/ |
|
54
|
36 |
|
$props = TypeCertainty::EnsureArgumentIsArray($maybe::PROPERTIES); |
|
55
|
|
|
|
|
56
|
36 |
|
static::RegisterDaftObjectTypeFromTypeAndProps($maybe, ...$props); |
|
57
|
36 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param mixed $maybe |
|
61
|
|
|
* |
|
62
|
|
|
* @return array<int, string> |
|
63
|
|
|
*/ |
|
64
|
38 |
|
public static function ObtainExpectedProperties($maybe) : array |
|
65
|
|
|
{ |
|
66
|
|
|
/** |
|
67
|
|
|
* @var scalar|array|object|resource|null |
|
68
|
|
|
*/ |
|
69
|
38 |
|
$maybe = is_object($maybe) ? get_class($maybe) : $maybe; |
|
70
|
|
|
|
|
71
|
38 |
|
if (is_string($maybe)) { |
|
72
|
38 |
|
if (static::IsTypeUnregistered($maybe)) { |
|
73
|
34 |
|
if (TypeParanoia::IsThingStrings($maybe, AbstractDaftObject::class)) { |
|
74
|
34 |
|
static::RegisterAbstractDaftObjectType($maybe); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
36 |
|
self::MaybeRegisterAdditionalTypes($maybe); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
36 |
|
return parent::ObtainExpectedProperties($maybe); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
40 |
|
public static function GetterClosure(string $type, string ...$props) : Closure |
|
85
|
|
|
{ |
|
86
|
40 |
|
return static::SetterOrGetterClosure($type, false, ...$props); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
40 |
|
public static function SetterClosure(string $type, string ...$props) : Closure |
|
90
|
|
|
{ |
|
91
|
40 |
|
return static::SetterOrGetterClosure($type, false, ...$props); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
40 |
|
public static function TypeAndGetterAndSetterClosureWithProps( |
|
95
|
|
|
string $type, |
|
96
|
|
|
string ...$props |
|
97
|
|
|
) : array { |
|
98
|
40 |
|
return array_merge( |
|
99
|
|
|
[ |
|
100
|
40 |
|
$type, |
|
101
|
40 |
|
static::GetterClosure($type, ...$props), |
|
102
|
40 |
|
static::SetterClosure($type, ...$props), |
|
103
|
|
|
], |
|
104
|
40 |
|
$props |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
6 |
|
public static function AutoRegisterType(string $type, string ...$properties) : void |
|
109
|
|
|
{ |
|
110
|
6 |
|
$args = static::TypeAndGetterAndSetterClosureWithProps($type, ...$properties); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var string |
|
114
|
|
|
*/ |
|
115
|
6 |
|
$type = array_shift($args); |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var Closure|null |
|
119
|
|
|
*/ |
|
120
|
6 |
|
$getter = array_shift($args); |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @var Closure|null |
|
124
|
|
|
*/ |
|
125
|
6 |
|
$setter = array_shift($args); |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @var array<int, string> |
|
129
|
|
|
*/ |
|
130
|
6 |
|
$properties = $args; |
|
131
|
|
|
|
|
132
|
6 |
|
static::RegisterType($type, $getter, $setter, ...$properties); |
|
133
|
4 |
|
} |
|
134
|
|
|
|
|
135
|
36 |
|
protected static function RegisterDaftObjectTypeFromTypeAndProps( |
|
136
|
|
|
string $maybe, |
|
137
|
|
|
string ...$props |
|
138
|
|
|
) : void { |
|
139
|
36 |
|
$args = static::TypeAndGetterAndSetterClosureWithProps($maybe, ...$props); |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @var string |
|
143
|
|
|
*/ |
|
144
|
36 |
|
$type = array_shift($args); |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @var Closure|null |
|
148
|
|
|
*/ |
|
149
|
36 |
|
$getter = array_shift($args); |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @var Closure|null |
|
153
|
|
|
*/ |
|
154
|
36 |
|
$setter = array_shift($args); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @var array<int, string> |
|
158
|
|
|
*/ |
|
159
|
36 |
|
$props = $args; |
|
160
|
|
|
|
|
161
|
36 |
|
static::RegisterType($type, $getter, $setter, ...$props); |
|
162
|
36 |
|
self::MaybeRegisterAdditionalTypes($type); |
|
163
|
36 |
|
} |
|
164
|
|
|
|
|
165
|
40 |
|
protected static function SetterOrGetterClosure( |
|
166
|
|
|
string $type, |
|
167
|
|
|
bool $SetNotGet, |
|
168
|
|
|
string ...$props |
|
169
|
|
|
) : Closure { |
|
170
|
|
|
return function (string $property) use ($type, $props, $SetNotGet) : ? string { |
|
171
|
6 |
|
if (TypeParanoia::MaybeInArray($property, $props)) { |
|
172
|
|
|
/** |
|
173
|
|
|
* @var string |
|
174
|
|
|
*/ |
|
175
|
6 |
|
$method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet); |
|
176
|
|
|
|
|
177
|
6 |
|
if (method_exists($type, $method)) { |
|
178
|
6 |
|
return $method; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
2 |
|
return null; |
|
183
|
40 |
|
}; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
36 |
|
protected static function MaybeRegisterAdditionalTypes(string $maybe) : void |
|
187
|
|
|
{ |
|
188
|
|
|
foreach ( |
|
189
|
|
|
[ |
|
190
|
36 |
|
DefinesOwnArrayIdInterface::class, |
|
191
|
|
|
DefinesOwnIntegerIdInterface::class, |
|
192
|
|
|
DefinesOwnStringIdInterface::class, |
|
193
|
|
|
] as $otherType |
|
194
|
|
|
) { |
|
195
|
36 |
|
if ($otherType !== $maybe && self::IsTypeUnregistered($otherType)) { |
|
196
|
36 |
|
self::RegisterDaftObjectTypeFromTypeAndProps($otherType, 'id'); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
36 |
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|