1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
use ReflectionMethod; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base daft object. |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractDaftObject implements DaftObject |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* List of properties that can be defined on an implementation. |
20
|
|
|
* |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
const PROPERTIES = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* List of nullable properties that can be defined on an implementation. |
27
|
|
|
* |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
const NULLABLE_PROPERTIES = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* List of exportable properties that can be defined on an implementation. |
34
|
|
|
* |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
const EXPORTABLE_PROPERTIES = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Does some sanity checking. |
41
|
|
|
* |
42
|
|
|
* @see DefinesOwnIdPropertiesInterface |
43
|
|
|
* @see self::CheckTypeDefinesOwnIdProperties() |
44
|
|
|
*/ |
45
|
46 |
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
if ( |
48
|
46 |
|
($this instanceof DefinesOwnIdPropertiesInterface) |
49
|
|
|
) { |
50
|
33 |
|
self::CheckTypeDefinesOwnIdProperties($this); |
51
|
|
|
} |
52
|
44 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
29 |
|
public function __get(string $property) |
58
|
|
|
{ |
59
|
29 |
|
return $this->DoGetSet( |
60
|
29 |
|
$property, |
61
|
29 |
|
'Get' . ucfirst($property), |
62
|
29 |
|
PropertyNotReadableException::class, |
63
|
29 |
|
NotPublicGetterPropertyException::class |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
17 |
|
public function __set(string $property, $v) |
71
|
|
|
{ |
72
|
17 |
|
return $this->DoGetSet( |
73
|
17 |
|
$property, |
74
|
17 |
|
'Set' . ucfirst($property), |
75
|
17 |
|
PropertyNotWriteableException::class, |
76
|
17 |
|
NotPublicSetterPropertyException::class, |
77
|
17 |
|
$v |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
* |
84
|
|
|
* @see static::NudgePropertyValue() |
85
|
|
|
*/ |
86
|
8 |
|
public function __unset(string $property) : void |
87
|
|
|
{ |
88
|
8 |
|
$this->NudgePropertyValue($property, null); |
89
|
6 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
15 |
|
public function __debugInfo() : array |
95
|
|
|
{ |
96
|
15 |
|
$out = []; |
97
|
15 |
|
foreach (static::DaftObjectExportableProperties() as $prop) { |
98
|
15 |
|
$expectedMethod = 'Get' . ucfirst($prop); |
99
|
|
|
if ( |
100
|
15 |
|
$this->__isset($prop) && |
101
|
15 |
|
method_exists($this, $expectedMethod) && |
102
|
|
|
( |
103
|
12 |
|
new ReflectionMethod(static::class, $expectedMethod) |
104
|
12 |
|
)->isPublic() |
105
|
|
|
) { |
106
|
12 |
|
$out[$prop] = $this->$expectedMethod(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
15 |
|
return $out; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* List of properties that can be defined on an implementation. |
115
|
|
|
* |
116
|
|
|
* @return string[] |
117
|
|
|
*/ |
118
|
40 |
|
final public static function DaftObjectProperties() : array |
119
|
|
|
{ |
120
|
40 |
|
return static::PROPERTIES; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
9 |
|
final public static function DaftObjectNullableProperties() : array |
127
|
|
|
{ |
128
|
9 |
|
return static::NULLABLE_PROPERTIES; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
15 |
|
final public static function DaftObjectExportableProperties() : array |
135
|
|
|
{ |
136
|
15 |
|
return static::EXPORTABLE_PROPERTIES; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Nudge the state of a given property, marking it as dirty. |
141
|
|
|
* |
142
|
|
|
* @param string $property property being nudged |
143
|
|
|
* @param mixed $value value to nudge property with |
144
|
|
|
* |
145
|
|
|
* @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties() |
146
|
|
|
* @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties() |
147
|
|
|
* @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed |
148
|
|
|
*/ |
149
|
|
|
abstract protected function NudgePropertyValue( |
150
|
|
|
string $property, |
151
|
|
|
$value |
152
|
|
|
) : void; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Checks if a type correctly defines it's own id. |
156
|
|
|
* |
157
|
|
|
* @param DaftObject $object |
158
|
|
|
* |
159
|
|
|
* @throws ClassDoesNotImplementClassException if $object is not an implementation of DefinesOwnIdPropertiesInterface |
160
|
|
|
* @throws ClassMethodReturnHasZeroArrayCountException if $object::DaftObjectIdProperties() does not contain at least one property |
161
|
|
|
* @throws ClassMethodReturnIsNotArrayOfStringsException if $object::DaftObjectIdProperties() is not string[] |
162
|
|
|
* @throws UndefinedPropertyException if an id property is not in $object::DaftObjectIdProperties() |
163
|
|
|
*/ |
164
|
34 |
|
final protected static function CheckTypeDefinesOwnIdProperties( |
165
|
|
|
DaftObject $object |
166
|
|
|
) : void { |
167
|
34 |
|
$class = get_class($object); |
168
|
34 |
|
if (false === ($object instanceof DefinesOwnIdPropertiesInterface)) { |
169
|
1 |
|
throw new ClassDoesNotImplementClassException( |
170
|
1 |
|
$class, |
171
|
1 |
|
DefinesOwnIdPropertiesInterface::class |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @var DefinesOwnIdPropertiesInterface $object |
177
|
|
|
*/ |
178
|
33 |
|
$object = $object; |
179
|
|
|
|
180
|
33 |
|
$properties = $object::DaftObjectIdProperties(); |
181
|
|
|
|
182
|
33 |
|
if (count($properties) < 1) { |
183
|
1 |
|
throw new ClassMethodReturnHasZeroArrayCountException( |
184
|
1 |
|
$class, |
185
|
1 |
|
'DaftObjectIdProperties' |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
32 |
|
foreach ($properties as $property) { |
190
|
32 |
|
if (false === is_string($property)) { |
191
|
1 |
|
throw new ClassMethodReturnIsNotArrayOfStringsException( |
192
|
1 |
|
$class, |
193
|
1 |
|
'DaftObjectIdProperties' |
194
|
|
|
); |
195
|
|
|
} |
196
|
|
|
} |
197
|
31 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param mixed $v |
201
|
|
|
* |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
40 |
|
private function DoGetSet( |
205
|
|
|
string $property, |
206
|
|
|
string $expectedMethod, |
207
|
|
|
string $notExists, |
208
|
|
|
string $notPublic, |
209
|
|
|
$v = null |
210
|
|
|
) { |
211
|
|
|
if ( |
212
|
|
|
( |
213
|
40 |
|
'id' !== $property || |
214
|
9 |
|
false === ($this instanceof DefinesOwnIdPropertiesInterface) |
215
|
|
|
) && |
216
|
40 |
|
false === in_array($property, static::DaftObjectProperties(), true) |
217
|
|
|
) { |
218
|
3 |
|
throw new UndefinedPropertyException(static::class, $property); |
219
|
37 |
|
} elseif (false === method_exists($this, $expectedMethod)) { |
220
|
2 |
|
throw new $notExists( |
221
|
2 |
|
static::class, |
222
|
2 |
|
$property |
223
|
|
|
); |
224
|
|
|
} elseif ( |
225
|
|
|
false === ( |
226
|
35 |
|
new ReflectionMethod(static::class, $expectedMethod) |
227
|
35 |
|
)->isPublic() |
228
|
|
|
) { |
229
|
2 |
|
throw new $notPublic( |
230
|
2 |
|
static::class, |
231
|
2 |
|
$property |
232
|
|
|
); |
233
|
|
|
} |
234
|
|
|
|
235
|
33 |
|
return $this->$expectedMethod($v); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|