1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Base daft objects. |
4
|
|
|
* |
5
|
|
|
* @author SignpostMarv |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SignpostMarv\DaftObject; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Base daft object. |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractDaftObject implements DaftObject |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* List of properties that can be defined on an implementation. |
18
|
|
|
* |
19
|
|
|
* @var array<int, string> |
20
|
|
|
*/ |
21
|
|
|
const PROPERTIES = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* List of nullable properties that can be defined on an implementation. |
25
|
|
|
* |
26
|
|
|
* @var array<int, string> |
27
|
|
|
*/ |
28
|
|
|
const NULLABLE_PROPERTIES = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* List of exportable properties that can be defined on an implementation. |
32
|
|
|
* |
33
|
|
|
* @var array<int, string> |
34
|
|
|
*/ |
35
|
|
|
const EXPORTABLE_PROPERTIES = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* import/export definition for DaftJson. |
39
|
|
|
* |
40
|
|
|
* @var array<int, string> |
41
|
|
|
*/ |
42
|
|
|
const JSON_PROPERTIES = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Does some sanity checking. |
46
|
|
|
* |
47
|
|
|
* @see DefinesOwnIdPropertiesInterface |
48
|
|
|
* @see TypeUtilities::CheckTypeDefinesOwnIdProperties() |
49
|
|
|
*/ |
50
|
376 |
|
public function __construct() |
51
|
|
|
{ |
52
|
376 |
|
TypeUtilities::CheckTypeDefinesOwnIdProperties( |
53
|
376 |
|
static::class, |
54
|
376 |
|
($this instanceof DefinesOwnIdPropertiesInterface) |
55
|
|
|
); |
56
|
372 |
|
} |
57
|
|
|
|
58
|
82 |
|
public function __get(string $property) |
59
|
|
|
{ |
60
|
82 |
|
return $this->DoGetSet($property, false); |
61
|
|
|
} |
62
|
|
|
|
63
|
158 |
|
public function __set(string $property, $v) |
64
|
|
|
{ |
65
|
158 |
|
return $this->DoGetSet($property, true, $v); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @see static::NudgePropertyValue() |
70
|
|
|
*/ |
71
|
40 |
|
public function __unset(string $property) : void |
72
|
|
|
{ |
73
|
40 |
|
$this->NudgePropertyValue($property, null); |
74
|
36 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array<string, mixed> |
78
|
|
|
*/ |
79
|
62 |
|
public function __debugInfo() : array |
80
|
|
|
{ |
81
|
62 |
|
$getters = static::DaftObjectPublicGetters(); |
82
|
62 |
|
$exportables = static::DaftObjectExportableProperties(); |
83
|
|
|
/** |
84
|
|
|
* @var array<int, string> $properties |
85
|
|
|
*/ |
86
|
|
|
$properties = array_filter($exportables, function (string $prop) use ($getters) : bool { |
87
|
60 |
|
return $this->__isset($prop) && in_array($prop, $getters, true); |
88
|
62 |
|
}); |
89
|
|
|
|
90
|
62 |
|
return array_combine($properties, array_map( |
91
|
|
|
/** |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
function (string $prop) { |
95
|
48 |
|
$expectedMethod = 'Get' . ucfirst($prop); |
96
|
|
|
|
97
|
48 |
|
return $this->$expectedMethod(); |
98
|
62 |
|
}, |
99
|
62 |
|
$properties |
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* List of properties that can be defined on an implementation. |
105
|
|
|
* |
106
|
|
|
* @return array<int, string> |
107
|
|
|
*/ |
108
|
266 |
|
final public static function DaftObjectProperties() : array |
109
|
|
|
{ |
110
|
266 |
|
return static::PROPERTIES; |
111
|
|
|
} |
112
|
|
|
|
113
|
92 |
|
final public static function DaftObjectNullableProperties() : array |
114
|
|
|
{ |
115
|
92 |
|
return static::NULLABLE_PROPERTIES; |
116
|
|
|
} |
117
|
|
|
|
118
|
124 |
|
final public static function DaftObjectExportableProperties() : array |
119
|
|
|
{ |
120
|
124 |
|
return static::EXPORTABLE_PROPERTIES; |
121
|
|
|
} |
122
|
|
|
|
123
|
108 |
|
final public static function DaftObjectPublicGetters() : array |
124
|
|
|
{ |
125
|
108 |
|
return TypeUtilities::DaftObjectPublicGetters(static::class); |
126
|
|
|
} |
127
|
|
|
|
128
|
158 |
|
final public static function DaftObjectPublicSetters() : array |
129
|
|
|
{ |
130
|
158 |
|
return TypeUtilities::DaftObjectPublicSetters(static::class); |
131
|
|
|
} |
132
|
|
|
|
133
|
68 |
|
final public static function DaftObjectJsonProperties() : array |
134
|
|
|
{ |
135
|
68 |
|
TypeUtilities::ThrowIfNotDaftJson(static::class); |
136
|
|
|
|
137
|
36 |
|
return static::JSON_PROPERTIES; |
138
|
|
|
} |
139
|
|
|
|
140
|
36 |
|
final public static function DaftObjectJsonPropertyNames() : array |
141
|
|
|
{ |
142
|
36 |
|
$out = []; |
143
|
|
|
|
144
|
36 |
|
foreach (static::DaftObjectJsonProperties() as $k => $prop) { |
145
|
36 |
|
if (is_string($k)) { |
146
|
24 |
|
$prop = $k; |
147
|
|
|
} |
148
|
|
|
|
149
|
36 |
|
$out[] = $prop; |
150
|
|
|
} |
151
|
|
|
|
152
|
36 |
|
return $out; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Nudge the state of a given property, marking it as dirty. |
157
|
|
|
* |
158
|
|
|
* @param string $property property being nudged |
159
|
|
|
* @param mixed $value value to nudge property with |
160
|
|
|
* |
161
|
|
|
* @throws UndefinedPropertyException if $property is not in static::DaftObjectProperties() |
162
|
|
|
* @throws PropertyNotNullableException if $property is not in static::DaftObjectNullableProperties() |
163
|
|
|
* @throws PropertyNotRewriteableException if class is write-once read-many and $property was already changed |
164
|
|
|
*/ |
165
|
|
|
abstract protected function NudgePropertyValue(string $property, $value) : void; |
166
|
|
|
|
167
|
216 |
|
protected function MaybeThrowOnDoGetSet(string $property, bool $setter, array $props) : void |
168
|
|
|
{ |
169
|
216 |
|
if (false === in_array($property, $props, true)) { |
170
|
10 |
|
if (false === in_array($property, static::DaftObjectProperties(), true)) { |
171
|
6 |
|
throw new UndefinedPropertyException(static::class, $property); |
172
|
4 |
|
} elseif ($setter) { |
173
|
2 |
|
throw new NotPublicSetterPropertyException(static::class, $property); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
throw new NotPublicGetterPropertyException(static::class, $property); |
177
|
|
|
} |
178
|
206 |
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param mixed $v |
182
|
|
|
* |
183
|
|
|
* @return mixed |
184
|
|
|
*/ |
185
|
216 |
|
protected function DoGetSet(string $property, bool $setter, $v = null) |
186
|
|
|
{ |
187
|
216 |
|
$props = $setter ? static::DaftObjectPublicSetters() : static::DaftObjectPublicGetters(); |
188
|
|
|
|
189
|
216 |
|
$this->MaybeThrowOnDoGetSet($property, $setter, $props); |
190
|
|
|
|
191
|
206 |
|
return $this->{TypeUtilities::MethodNameFromProperty($property, $setter)}($v); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|