|
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 |
|
$args = static::TypeAndGetterAndSetterClosureWithProps($maybe, ...$props); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
36 |
|
$type = array_shift($args); |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var Closure|null |
|
65
|
|
|
*/ |
|
66
|
36 |
|
$getter = array_shift($args); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var Closure|null |
|
70
|
|
|
*/ |
|
71
|
36 |
|
$setter = array_shift($args); |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var array<int, string> |
|
75
|
|
|
*/ |
|
76
|
36 |
|
$props = $args; |
|
77
|
|
|
|
|
78
|
36 |
|
static::RegisterType($type, $getter, $setter, ...$props); |
|
79
|
36 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param mixed $maybe |
|
83
|
|
|
* |
|
84
|
|
|
* @return array<int, string> |
|
85
|
|
|
*/ |
|
86
|
38 |
|
public static function ObtainExpectedProperties($maybe) : array |
|
87
|
|
|
{ |
|
88
|
|
|
/** |
|
89
|
|
|
* @var scalar|array|object|resource|null |
|
90
|
|
|
*/ |
|
91
|
38 |
|
$maybe = is_object($maybe) ? get_class($maybe) : $maybe; |
|
92
|
|
|
|
|
93
|
|
|
if ( |
|
94
|
38 |
|
is_string($maybe) && |
|
95
|
38 |
|
static::IsTypeUnregistered($maybe) && |
|
96
|
36 |
|
TypeParanoia::IsThingStrings($maybe, AbstractDaftObject::class) |
|
97
|
|
|
) { |
|
98
|
34 |
|
static::RegisterAbstractDaftObjectType($maybe); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
36 |
|
return parent::ObtainExpectedProperties($maybe); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
40 |
|
public static function GetterClosure(string $type, string ...$props) : Closure |
|
105
|
|
|
{ |
|
106
|
40 |
|
return static::SetterOrGetterClosure($type, false, ...$props); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
40 |
|
public static function SetterClosure(string $type, string ...$props) : Closure |
|
110
|
|
|
{ |
|
111
|
40 |
|
return static::SetterOrGetterClosure($type, false, ...$props); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
40 |
|
public static function TypeAndGetterAndSetterClosureWithProps( |
|
115
|
|
|
string $type, |
|
116
|
|
|
string ...$props |
|
117
|
|
|
) : array { |
|
118
|
40 |
|
return array_merge( |
|
119
|
|
|
[ |
|
120
|
40 |
|
$type, |
|
121
|
40 |
|
static::GetterClosure($type, ...$props), |
|
122
|
40 |
|
static::SetterClosure($type, ...$props), |
|
123
|
|
|
], |
|
124
|
40 |
|
$props |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
6 |
|
public static function AutoRegisterType(string $type, string ...$properties) : void |
|
129
|
|
|
{ |
|
130
|
6 |
|
$args = static::TypeAndGetterAndSetterClosureWithProps($type, ...$properties); |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @var string |
|
134
|
|
|
*/ |
|
135
|
6 |
|
$type = array_shift($args); |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @var Closure|null |
|
139
|
|
|
*/ |
|
140
|
6 |
|
$getter = array_shift($args); |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @var Closure|null |
|
144
|
|
|
*/ |
|
145
|
6 |
|
$setter = array_shift($args); |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @var array<int, string> |
|
149
|
|
|
*/ |
|
150
|
6 |
|
$properties = $args; |
|
151
|
|
|
|
|
152
|
6 |
|
static::RegisterType($type, $getter, $setter, ...$properties); |
|
153
|
4 |
|
} |
|
154
|
|
|
|
|
155
|
40 |
|
protected static function SetterOrGetterClosure( |
|
156
|
|
|
string $type, |
|
157
|
|
|
bool $SetNotGet, |
|
158
|
|
|
string ...$props |
|
159
|
|
|
) : Closure { |
|
160
|
|
|
return function (string $property) use ($type, $props, $SetNotGet) : ? string { |
|
161
|
4 |
|
if (TypeParanoia::MaybeInArray($property, $props)) { |
|
162
|
|
|
/** |
|
163
|
|
|
* @var string |
|
164
|
|
|
*/ |
|
165
|
4 |
|
$method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet); |
|
166
|
|
|
|
|
167
|
4 |
|
if (method_exists($type, $method)) { |
|
168
|
4 |
|
return $method; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return null; |
|
173
|
40 |
|
}; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|