|
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 SUPPORTED_INVALID_LEADING_CHARACTERS = [ |
|
29
|
|
|
'@', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array<string, array<string, bool>> |
|
34
|
|
|
*/ |
|
35
|
|
|
private static $Getters = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array<string, array<int, string>> |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $publicSetters = []; |
|
41
|
|
|
|
|
42
|
126 |
|
public static function DaftObjectPublicGetters(string $class) : array |
|
43
|
|
|
{ |
|
44
|
126 |
|
static::CachePublicGettersAndSetters($class); |
|
45
|
|
|
|
|
46
|
126 |
|
return array_keys(array_filter(self::$Getters[$class])); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public static function DaftObjectPublicOrProtectedGetters(string $class) : array |
|
50
|
|
|
{ |
|
51
|
2 |
|
static::CachePublicGettersAndSetters($class); |
|
52
|
|
|
|
|
53
|
2 |
|
return array_keys(self::$Getters[$class]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
188 |
|
public static function DaftObjectPublicSetters(string $class) : array |
|
57
|
|
|
{ |
|
58
|
188 |
|
static::CachePublicGettersAndSetters($class); |
|
59
|
|
|
|
|
60
|
188 |
|
return self::$publicSetters[$class]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
266 |
|
public static function MethodNameFromProperty( |
|
64
|
|
|
string $prop, |
|
65
|
|
|
bool $SetNotGet = self::BOOL_DEFAULT_SET_NOT_GET |
|
66
|
|
|
) : string { |
|
67
|
266 |
|
if (TypeParanoia::MaybeInArray(mb_substr($prop, 0, 1), self::SUPPORTED_INVALID_LEADING_CHARACTERS)) { |
|
68
|
4 |
|
return ($SetNotGet ? 'Alter' : 'Obtain') . ucfirst(mb_substr($prop, 1)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
262 |
|
return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Checks if a type correctly defines it's own id. |
|
76
|
|
|
* |
|
77
|
|
|
* @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface |
|
78
|
|
|
* @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property |
|
79
|
|
|
* @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[] |
|
80
|
|
|
* @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties() |
|
81
|
|
|
*/ |
|
82
|
536 |
|
public static function CheckTypeDefinesOwnIdProperties( |
|
83
|
|
|
string $class, |
|
84
|
|
|
bool $throwIfNotImplementation = self::BOOL_DEFAULT_THROWIFNOTIMPLEMENTATION |
|
85
|
|
|
) : void { |
|
86
|
536 |
|
if (TypeParanoia::IsThingStrings($class, DefinesOwnIdPropertiesInterface::class)) { |
|
87
|
314 |
|
static::CheckTypeDefinesOwnIdPropertiesIsImplementation($class); |
|
88
|
228 |
|
} elseif ($throwIfNotImplementation) { |
|
89
|
2 |
|
throw new ClassDoesNotImplementClassException( |
|
90
|
2 |
|
$class, |
|
91
|
2 |
|
DefinesOwnIdPropertiesInterface::class |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
530 |
|
} |
|
95
|
|
|
|
|
96
|
36 |
|
/** |
|
97
|
|
|
* @return array<int, string> |
|
98
|
|
|
*/ |
|
99
|
|
|
protected static function ObtainExpectedOrDefaultPropertiesWithAutoRegister( |
|
100
|
|
|
string $class |
|
101
|
|
|
) : array { |
|
102
|
36 |
|
return DefinitionAssistant::ObtainExpectedOrDefaultPropertiesWithAutoRegister($class); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
36 |
|
protected static function HasMethod( |
|
106
|
|
|
string $class, |
|
107
|
36 |
|
string $property, |
|
108
|
20 |
|
bool $SetNotGet, |
|
109
|
20 |
|
bool $pub = self::BOOL_DEFAULT_EXPECTING_NON_PUBLIC_METHOD |
|
110
|
|
|
) : bool { |
|
111
|
|
|
$method = static::MethodNameFromProperty($property, $SetNotGet); |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$ref = new ReflectionMethod($class, $method); |
|
115
|
|
|
|
|
116
|
312 |
|
return ($pub ? $ref->isPublic() : $ref->isProtected()) && false === $ref->isStatic(); |
|
117
|
|
|
} catch (ReflectionException $e) { |
|
118
|
312 |
|
return false; |
|
119
|
312 |
|
} |
|
120
|
312 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param mixed $maybe |
|
124
|
|
|
*/ |
|
125
|
|
|
protected static function FilterMaybeArray($maybe, callable $filter) : array |
|
126
|
|
|
{ |
|
127
|
314 |
|
return array_filter( |
|
128
|
|
|
TypeParanoia::EnsureArgumentIsArray($maybe, TypeParanoia::INDEX_FIRST_ARG, __METHOD__), |
|
129
|
314 |
|
$filter |
|
130
|
314 |
|
); |
|
131
|
314 |
|
} |
|
132
|
314 |
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param mixed $maybe |
|
135
|
|
|
*/ |
|
136
|
244 |
|
protected static function CountMaybeArray($maybe) : int |
|
137
|
|
|
{ |
|
138
|
244 |
|
return count(TypeParanoia::EnsureArgumentIsArray( |
|
139
|
36 |
|
$maybe, |
|
140
|
36 |
|
TypeParanoia::INDEX_FIRST_ARG, |
|
141
|
|
|
__METHOD__ |
|
142
|
36 |
|
)); |
|
143
|
16 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
protected static function CachePublicGettersAndSetters(string $class) : void |
|
146
|
36 |
|
{ |
|
147
|
|
|
if (false === isset(self::$Getters[$class])) { |
|
148
|
244 |
|
self::$Getters[$class] = []; |
|
149
|
|
|
self::$publicSetters[$class] = []; |
|
150
|
36 |
|
|
|
151
|
|
|
if (TypeParanoia::IsThingStrings($class, DefinesOwnIdPropertiesInterface::class)) { |
|
152
|
|
|
self::$Getters[$class]['id'] = self::BOOL_METHOD_IS_PUBLIC; |
|
153
|
36 |
|
} |
|
154
|
|
|
|
|
155
|
36 |
|
static::CachePublicGettersAndSettersProperties($class); |
|
156
|
32 |
|
} |
|
157
|
6 |
|
} |
|
158
|
6 |
|
|
|
159
|
6 |
|
protected static function CachePublicGettersAndSettersProperties(string $class) : void |
|
160
|
6 |
|
{ |
|
161
|
6 |
|
foreach ( |
|
162
|
|
|
static::ObtainExpectedOrDefaultPropertiesWithAutoRegister($class) as $prop |
|
163
|
2 |
|
) { |
|
164
|
|
|
if (static::HasMethod($class, $prop, self::BOOL_EXPECTING_GETTER)) { |
|
165
|
|
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_PUBLIC; |
|
166
|
36 |
|
} elseif (static::HasMethod( |
|
167
|
36 |
|
$class, |
|
168
|
|
|
$prop, |
|
169
|
|
|
self::BOOL_EXPECTING_GETTER, |
|
170
|
36 |
|
self::BOOL_EXPECTING_NON_PUBLIC_METHOD |
|
171
|
|
|
)) { |
|
172
|
314 |
|
self::$Getters[$class][$prop] = self::BOOL_METHOD_IS_NON_PUBLIC; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (static::HasMethod($class, $prop, true)) { |
|
176
|
|
|
self::$publicSetters[$class][] = $prop; |
|
177
|
|
|
} |
|
178
|
314 |
|
} |
|
179
|
|
|
} |
|
180
|
314 |
|
|
|
181
|
2 |
|
protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation( |
|
182
|
2 |
|
string $class |
|
183
|
2 |
|
) : void { |
|
184
|
|
|
/** |
|
185
|
|
|
* @var scalar|array|object|null |
|
186
|
312 |
|
*/ |
|
187
|
312 |
|
$properties = $class::DaftObjectIdProperties(); |
|
188
|
|
|
|
|
189
|
2 |
|
if (self::CountMaybeArray($properties) < 1) { |
|
190
|
2 |
|
throw new ClassMethodReturnHasZeroArrayCountException( |
|
191
|
2 |
|
$class, |
|
192
|
|
|
'DaftObjectIdProperties' |
|
193
|
|
|
); |
|
194
|
310 |
|
} elseif ( |
|
195
|
|
|
self::CountMaybeArray($properties) !== |
|
196
|
|
|
count(self::FilterMaybeArray($properties, 'is_string')) |
|
197
|
|
|
) { |
|
198
|
|
|
throw new ClassMethodReturnIsNotArrayOfStringsException( |
|
199
|
|
|
$class, |
|
200
|
|
|
'DaftObjectIdProperties' |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|