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
|
|
|
* @return array<int, string> |
54
|
|
|
*/ |
55
|
540 |
|
public static function DaftObjectPublicGetters(string $class) : array |
56
|
|
|
{ |
57
|
540 |
|
static::CachePublicGettersAndSetters($class); |
58
|
|
|
|
59
|
540 |
|
return array_keys(array_filter(self::$Getters[$class])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @psalm-param class-string<DaftObject> $class |
64
|
|
|
* |
65
|
|
|
* @return array<int, string> |
66
|
|
|
*/ |
67
|
64 |
|
public static function DaftObjectPublicOrProtectedGetters(string $class) : array |
68
|
|
|
{ |
69
|
64 |
|
static::CachePublicGettersAndSetters($class); |
70
|
|
|
|
71
|
64 |
|
return array_keys(self::$Getters[$class]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @psalm-param class-string<DaftObject> $class |
76
|
|
|
*/ |
77
|
368 |
|
public static function DaftObjectPublicSetters(string $class) : array |
78
|
|
|
{ |
79
|
368 |
|
static::CachePublicGettersAndSetters($class); |
80
|
|
|
|
81
|
368 |
|
return self::$publicSetters[$class]; |
82
|
|
|
} |
83
|
|
|
|
84
|
822 |
|
public static function MethodNameFromProperty( |
85
|
|
|
string $prop, |
86
|
|
|
bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET |
87
|
|
|
) : string { |
88
|
|
|
if ( |
89
|
822 |
|
in_array( |
90
|
822 |
|
mb_substr($prop, 0, 1), |
91
|
822 |
|
self::SUPPORTED_INVALID_LEADING_CHARACTERS, |
92
|
822 |
|
DefinitionAssistant::IN_ARRAY_STRICT_MODE |
93
|
|
|
) |
94
|
|
|
) { |
95
|
69 |
|
return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1)); |
96
|
|
|
} |
97
|
|
|
|
98
|
811 |
|
return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop); |
99
|
|
|
} |
100
|
|
|
|
101
|
32 |
|
public static function HasMethod( |
102
|
|
|
string $class, |
103
|
|
|
string $property, |
104
|
|
|
bool $SetNotGet, |
105
|
|
|
bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD |
106
|
|
|
) : bool { |
107
|
32 |
|
$method = static::MethodNameFromProperty($property, $SetNotGet); |
108
|
|
|
|
109
|
|
|
try { |
110
|
32 |
|
$ref = new ReflectionMethod($class, $method); |
111
|
|
|
|
112
|
|
|
return |
113
|
28 |
|
($pub ? $ref->isPublic() : $ref->isProtected()) && |
114
|
28 |
|
self::BOOL_METHOD_IS_NOT_STATIC === $ref->isStatic(); |
115
|
8 |
|
} catch (ReflectionException $e) { |
116
|
8 |
|
return self::BOOL_DOES_NOT_HAVE_METHOD; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @psalm-param class-string<DaftObject> $class |
122
|
|
|
*/ |
123
|
864 |
|
private static function CachePublicGettersAndSetters(string $class) : void |
124
|
|
|
{ |
125
|
864 |
|
if (false === isset(self::$Getters[$class])) { |
126
|
32 |
|
self::$Getters[$class] = []; |
127
|
32 |
|
self::$publicSetters[$class] = []; |
128
|
|
|
|
129
|
|
|
if ( |
130
|
32 |
|
is_a( |
131
|
24 |
|
$class, |
132
|
32 |
|
DefinesOwnIdPropertiesInterface::class, |
133
|
32 |
|
true |
134
|
|
|
) |
135
|
|
|
) { |
136
|
4 |
|
self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @psalm-var class-string<DaftObject> |
141
|
|
|
*/ |
142
|
32 |
|
$class = $class; |
143
|
|
|
|
144
|
32 |
|
static::CachePublicGettersAndSettersProperties($class); |
145
|
|
|
} |
146
|
864 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @template T as DaftObject |
150
|
|
|
* |
151
|
|
|
* @psalm-param class-string<T> $class |
152
|
|
|
*/ |
153
|
32 |
|
private static function CachePublicGettersAndSettersProperties(string $class) : void |
154
|
|
|
{ |
155
|
|
|
if ( |
156
|
32 |
|
is_a($class, AbstractDaftObject::class, true) && |
157
|
32 |
|
DefinitionAssistant::IsTypeUnregistered($class) |
158
|
|
|
) { |
159
|
|
|
/** |
160
|
|
|
* @psalm-var class-string<T> |
161
|
|
|
*/ |
162
|
31 |
|
$class = DefinitionAssistant::RegisterAbstractDaftObjectType($class); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
foreach ( |
166
|
32 |
|
DefinitionAssistant::ObtainExpectedProperties($class) as $prop |
167
|
|
|
) { |
168
|
32 |
|
static::CachePublicGettersAndSettersProperty($class, $prop); |
169
|
24 |
|
} |
170
|
8 |
|
} |
171
|
6 |
|
|
172
|
4 |
|
/** |
173
|
8 |
|
* @psalm-param class-string<DaftObject> $class |
174
|
8 |
|
*/ |
175
|
|
|
private static function CachePublicGettersAndSettersProperty( |
176
|
4 |
|
string $class, |
177
|
|
|
string $prop |
178
|
|
|
) : void { |
179
|
32 |
|
if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) { |
180
|
31 |
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC; |
181
|
|
|
} elseif (static::HasMethod( |
182
|
|
|
$class, |
183
|
32 |
|
$prop, |
184
|
|
|
self::BOOL_EXPECTING_GETTER, |
185
|
|
|
self::BOOL_EXPECTING_NON_PUBLIC_METHOD |
186
|
|
|
)) { |
187
|
|
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if (static::HasMethod($class, $prop, self::BOOL_METHOD_IS_PUBLIC)) { |
191
|
|
|
self::$publicSetters[$class][] = $prop; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|