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
|
|
|
/** |
15
|
|
|
* @var array<string, array<string, bool>> |
16
|
|
|
*/ |
17
|
|
|
private static $Getters = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array<string, array<int, string>> |
21
|
|
|
*/ |
22
|
|
|
private static $publicSetters = []; |
23
|
|
|
|
24
|
110 |
|
public static function DaftObjectPublicGetters(string $class) : array |
25
|
|
|
{ |
26
|
110 |
|
static::CachePublicGettersAndSetters($class); |
27
|
|
|
|
28
|
110 |
|
return array_keys(array_filter(self::$Getters[$class])); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function DaftObjectPublicOrProtectedGetters(string $class) : array |
32
|
|
|
{ |
33
|
|
|
static::CachePublicGettersAndSetters($class); |
34
|
|
|
|
35
|
|
|
return array_keys(self::$Getters); |
36
|
|
|
} |
37
|
|
|
|
38
|
160 |
|
public static function DaftObjectPublicSetters(string $class) : array |
39
|
|
|
{ |
40
|
160 |
|
static::CachePublicGettersAndSetters($class); |
41
|
|
|
|
42
|
160 |
|
return self::$publicSetters[$class]; |
43
|
|
|
} |
44
|
|
|
|
45
|
26 |
|
public static function HasMethod(string $class, string $property, bool $SetNotGet, bool $public = true) : bool |
46
|
|
|
{ |
47
|
26 |
|
$method = TypeUtilities::MethodNameFromProperty($property, $SetNotGet); |
48
|
|
|
|
49
|
|
|
try { |
50
|
26 |
|
$mRef = new ReflectionMethod($class, $method); |
51
|
|
|
|
52
|
26 |
|
return ($public ? $mRef->isPublic() : $mRef->isProtected()) && false === $mRef->isStatic(); |
53
|
8 |
|
} catch (ReflectionException $e) { |
54
|
8 |
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
208 |
|
public static function MethodNameFromProperty(string $prop, bool $SetNotGet = false) : string |
59
|
|
|
{ |
60
|
208 |
|
return ($SetNotGet ? 'Set' : 'Get') . ucfirst($prop); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks if a type correctly defines it's own id. |
65
|
|
|
* |
66
|
|
|
* @throws ClassDoesNotImplementClassException if $class is not an implementation of DefinesOwnIdPropertiesInterface |
67
|
|
|
* @throws ClassMethodReturnHasZeroArrayCountException if $class::DaftObjectIdProperties() does not contain at least one property |
68
|
|
|
* @throws ClassMethodReturnIsNotArrayOfStringsException if $class::DaftObjectIdProperties() is not string[] |
69
|
|
|
* @throws UndefinedPropertyException if an id property is not in $class::DaftObjectIdProperties() |
70
|
|
|
*/ |
71
|
430 |
|
public static function CheckTypeDefinesOwnIdProperties( |
72
|
|
|
string $class, |
73
|
|
|
bool $throwIfNotImplementation = false |
74
|
|
|
) : void { |
75
|
430 |
|
if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) { |
76
|
262 |
|
self::CheckTypeDefinesOwnIdPropertiesIsImplementation($class); |
77
|
174 |
|
} elseif ($throwIfNotImplementation) { |
78
|
2 |
|
throw new ClassDoesNotImplementClassException( |
79
|
2 |
|
$class, |
80
|
2 |
|
DefinesOwnIdPropertiesInterface::class |
81
|
|
|
); |
82
|
|
|
} |
83
|
426 |
|
} |
84
|
|
|
|
85
|
218 |
|
final protected static function CachePublicGettersAndSetters(string $class) : void |
86
|
|
|
{ |
87
|
218 |
|
if (false === isset(self::$Getters[$class])) { |
88
|
26 |
|
self::$Getters[$class] = []; |
89
|
26 |
|
self::$publicSetters[$class] = []; |
90
|
|
|
|
91
|
26 |
|
if (is_a($class, DefinesOwnIdPropertiesInterface::class, true)) { |
92
|
14 |
|
self::$Getters[$class]['id'] = true; |
93
|
|
|
} |
94
|
|
|
|
95
|
26 |
|
self::CachePublicGettersAndSettersProperties($class); |
96
|
|
|
} |
97
|
218 |
|
} |
98
|
|
|
|
99
|
26 |
|
final protected static function CachePublicGettersAndSettersProperties(string $class) : void |
100
|
|
|
{ |
101
|
|
|
/** |
102
|
|
|
* @var string[] $props |
103
|
|
|
*/ |
104
|
26 |
|
$props = $class::DaftObjectProperties(); |
105
|
|
|
|
106
|
26 |
|
foreach ($props as $prop) { |
107
|
26 |
|
if (TypeUtilities::HasMethod($class, $prop, false)) { |
108
|
22 |
|
self::$Getters[$class][$prop] = true; |
109
|
6 |
|
} elseif (TypeUtilities::HasMethod($class, $prop, false, false)) { |
110
|
2 |
|
self::$Getters[$class][$prop] = false; |
111
|
|
|
} |
112
|
|
|
|
113
|
26 |
|
if (TypeUtilities::HasMethod($class, $prop, true)) { |
114
|
26 |
|
self::$publicSetters[$class][] = $prop; |
115
|
|
|
} |
116
|
|
|
} |
117
|
26 |
|
} |
118
|
|
|
|
119
|
262 |
|
final protected static function CheckTypeDefinesOwnIdPropertiesIsImplementation( |
120
|
|
|
string $class |
121
|
|
|
) : void { |
122
|
262 |
|
$properties = (array) $class::DaftObjectIdProperties(); |
123
|
|
|
|
124
|
262 |
|
if (count($properties) < 1) { |
125
|
2 |
|
throw new ClassMethodReturnHasZeroArrayCountException( |
126
|
2 |
|
$class, |
127
|
2 |
|
'DaftObjectIdProperties' |
128
|
|
|
); |
129
|
260 |
|
} elseif (count($properties) !== count(array_filter($properties, 'is_string'))) { |
130
|
2 |
|
throw new ClassMethodReturnIsNotArrayOfStringsException( |
131
|
2 |
|
$class, |
132
|
2 |
|
'DaftObjectIdProperties' |
133
|
|
|
); |
134
|
|
|
} |
135
|
258 |
|
} |
136
|
|
|
} |
137
|
|
|
|