|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author SignpostMarv |
|
4
|
|
|
*/ |
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftObject; |
|
8
|
|
|
|
|
9
|
|
|
use ReflectionException; |
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
|
|
12
|
|
|
class TypeUtilities |
|
13
|
|
|
{ |
|
14
|
|
|
const BOOL_EXPECTING_NON_PUBLIC_METHOD = false; |
|
15
|
|
|
|
|
16
|
|
|
const BOOL_EXPECTING_GETTER = false; |
|
17
|
|
|
|
|
18
|
|
|
const BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION = false; |
|
19
|
|
|
|
|
20
|
|
|
const BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD = true; |
|
21
|
|
|
|
|
22
|
|
|
const BOOL_METHOD_IS_PUBLIC = true; |
|
23
|
|
|
|
|
24
|
|
|
const BOOL_METHOD_IS_NON_PUBLIC = false; |
|
25
|
|
|
|
|
26
|
|
|
const BOOL_DEFAULT_SET_NOT_GET = false; |
|
27
|
|
|
|
|
28
|
|
|
const BOOL_DOES_NOT_HAVE_METHOD = false; |
|
29
|
|
|
|
|
30
|
|
|
const SUPPORTED_INVALID_LEADING_CHARACTERS = [ |
|
31
|
|
|
'@', |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
const BOOL_METHOD_IS_NOT_STATIC = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array<string, array<string, bool>> |
|
38
|
|
|
* |
|
39
|
|
|
* @psalm-var array<class-string<DaftObject>, array<string, bool>> |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $Getters = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array<string, array<int, string>> |
|
45
|
|
|
* |
|
46
|
|
|
* @psalm-var array<class-string<DaftObject>, array<int, string>> |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $publicSetters = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @psalm-param class-string<DaftObject> $class |
|
52
|
|
|
* |
|
53
|
340 |
|
* @return array<int, string> |
|
54
|
|
|
*/ |
|
55
|
340 |
|
public static function DaftObjectPublicGetters(string $class) : array |
|
56
|
|
|
{ |
|
57
|
340 |
|
static::CachePublicGettersAndSetters($class); |
|
58
|
|
|
|
|
59
|
|
|
return array_keys(array_filter(self::$Getters[$class])); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @psalm-param class-string<DaftObject> $class |
|
64
|
|
|
* |
|
65
|
64 |
|
* @return array<int, string> |
|
66
|
|
|
*/ |
|
67
|
64 |
|
public static function DaftObjectPublicOrProtectedGetters(string $class) : array |
|
68
|
|
|
{ |
|
69
|
64 |
|
static::CachePublicGettersAndSetters($class); |
|
70
|
|
|
|
|
71
|
|
|
return array_keys(self::$Getters[$class]); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
328 |
|
* @psalm-param class-string<DaftObject> $class |
|
76
|
|
|
*/ |
|
77
|
328 |
|
public static function DaftObjectPublicSetters(string $class) : array |
|
78
|
|
|
{ |
|
79
|
328 |
|
static::CachePublicGettersAndSetters($class); |
|
80
|
|
|
|
|
81
|
|
|
return self::$publicSetters[$class]; |
|
82
|
429 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
public static function MethodNameFromProperty( |
|
85
|
|
|
string $prop, |
|
86
|
|
|
bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET |
|
87
|
429 |
|
) : string { |
|
88
|
429 |
|
if ( |
|
89
|
429 |
|
in_array( |
|
90
|
429 |
|
mb_substr($prop, 0, 1), |
|
91
|
|
|
self::SUPPORTED_INVALID_LEADING_CHARACTERS, |
|
92
|
|
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
|
93
|
21 |
|
) |
|
94
|
|
|
) { |
|
95
|
|
|
return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1)); |
|
96
|
418 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop); |
|
99
|
34 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
private static function HasMethod( |
|
102
|
|
|
string $class, |
|
103
|
|
|
string $property, |
|
104
|
|
|
bool $SetNotGet, |
|
105
|
34 |
|
bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD |
|
106
|
|
|
) : bool { |
|
107
|
|
|
$method = static::MethodNameFromProperty($property, $SetNotGet); |
|
108
|
34 |
|
|
|
109
|
|
|
try { |
|
110
|
30 |
|
$ref = new ReflectionMethod($class, $method); |
|
111
|
8 |
|
|
|
112
|
8 |
|
return ( |
|
113
|
|
|
($pub ? $ref->isPublic() : $ref->isProtected()) && |
|
114
|
|
|
self::BOOL_METHOD_IS_NOT_STATIC === $ref->isStatic() |
|
115
|
|
|
); |
|
116
|
|
|
} catch (ReflectionException $e) { |
|
117
|
|
|
return self::BOOL_DOES_NOT_HAVE_METHOD; |
|
118
|
|
|
} |
|
119
|
664 |
|
} |
|
120
|
|
|
|
|
121
|
664 |
|
/** |
|
122
|
34 |
|
* @psalm-param class-string<DaftObject> $class |
|
123
|
34 |
|
*/ |
|
124
|
|
|
private static function CachePublicGettersAndSetters(string $class) : void |
|
125
|
|
|
{ |
|
126
|
34 |
|
if (false === isset(self::$Getters[$class])) { |
|
127
|
26 |
|
self::$Getters[$class] = []; |
|
128
|
34 |
|
self::$publicSetters[$class] = []; |
|
129
|
34 |
|
|
|
130
|
|
|
if ( |
|
131
|
|
|
is_a( |
|
132
|
4 |
|
$class, |
|
133
|
|
|
DefinesOwnIdPropertiesInterface::class, |
|
134
|
|
|
true |
|
135
|
|
|
) |
|
136
|
|
|
) { |
|
137
|
|
|
self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC; |
|
138
|
34 |
|
} |
|
139
|
|
|
|
|
140
|
34 |
|
/** |
|
141
|
|
|
* @psalm-var class-string<DaftObject> |
|
142
|
664 |
|
*/ |
|
143
|
|
|
$class = $class; |
|
144
|
|
|
|
|
145
|
|
|
static::CachePublicGettersAndSettersProperties($class); |
|
146
|
|
|
} |
|
147
|
34 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
34 |
|
* @template T as DaftObject |
|
151
|
|
|
* |
|
152
|
34 |
|
* @psalm-param class-string<T> $class |
|
153
|
26 |
|
*/ |
|
154
|
8 |
|
private static function CachePublicGettersAndSettersProperties(string $class) : void |
|
155
|
6 |
|
{ |
|
156
|
4 |
|
if ( |
|
157
|
8 |
|
is_a($class, AbstractDaftObject::class, true) && |
|
158
|
8 |
|
DefinitionAssistant::IsTypeUnregistered($class) |
|
159
|
|
|
) { |
|
160
|
4 |
|
/** |
|
161
|
|
|
* @psalm-var class-string<T> |
|
162
|
|
|
*/ |
|
163
|
34 |
|
$class = DefinitionAssistant::RegisterAbstractDaftObjectType($class); |
|
164
|
33 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
foreach ( |
|
167
|
34 |
|
DefinitionAssistant::ObtainExpectedProperties($class) as $prop |
|
168
|
|
|
) { |
|
169
|
|
|
if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) { |
|
170
|
|
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC; |
|
171
|
|
|
} elseif (static::HasMethod( |
|
172
|
|
|
$class, |
|
173
|
|
|
$prop, |
|
174
|
|
|
self::BOOL_EXPECTING_GETTER, |
|
175
|
|
|
self::BOOL_EXPECTING_NON_PUBLIC_METHOD |
|
176
|
|
|
)) { |
|
177
|
|
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if (static::HasMethod($class, $prop, self::BOOL_METHOD_IS_PUBLIC)) { |
|
181
|
|
|
self::$publicSetters[$class][] = $prop; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|