SignpostMarv /
daft-object
| 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 | * Array-backed daft objects. |
||
| 13 | */ |
||
| 14 | abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray |
||
| 15 | { |
||
| 16 | const BOOL_DEFAULT_WRITEALL = false; |
||
| 17 | |||
| 18 | const BOOL_DEFAULT_AUTOTRIMSTRINGS = false; |
||
| 19 | |||
| 20 | const BOOL_DEFAULT_THROWIFNOTUNIQUE = false; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * data for this instance. |
||
| 24 | * |
||
| 25 | * @var array<string, scalar|array|object|null> |
||
| 26 | */ |
||
| 27 | private $data = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * List of changed properties. |
||
| 31 | * |
||
| 32 | * @var array<string, bool> |
||
| 33 | */ |
||
| 34 | private $changedProperties = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List of changed properties, for write-once read-many. |
||
| 38 | * |
||
| 39 | * @var array<string, bool> |
||
| 40 | */ |
||
| 41 | private $wormProperties = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array<string, scalar|array|object|null> $data |
||
| 45 | */ |
||
| 46 | 980 | public function __construct(array $data = [], bool $writeAll = false) |
|
| 47 | { |
||
| 48 | 980 | if (true === $writeAll) { |
|
| 49 | 192 | foreach ($data as $k => $v) { |
|
| 50 | 192 | $this->__set($k, $v); |
|
| 51 | } |
||
| 52 | } else { |
||
| 53 | 788 | $this->data = $data; |
|
| 54 | } |
||
| 55 | 980 | } |
|
| 56 | |||
| 57 | 24 | public function __isset(string $property) : bool |
|
| 58 | { |
||
| 59 | return |
||
| 60 | 24 | in_array( |
|
| 61 | 18 | $property, |
|
| 62 | 24 | static::DaftObjectProperties(), |
|
| 63 | 24 | DefinitionAssistant::IN_ARRAY_STRICT_MODE |
|
| 64 | ) && |
||
| 65 | 24 | isset($this->data, $this->data[$property]); |
|
| 66 | } |
||
| 67 | |||
| 68 | 88 | public function ChangedProperties() : array |
|
| 69 | { |
||
| 70 | 88 | return array_keys($this->changedProperties); |
|
| 71 | } |
||
| 72 | |||
| 73 | 88 | public function MakePropertiesUnchanged(string ...$properties) : void |
|
| 74 | { |
||
| 75 | 88 | foreach ($properties as $property) { |
|
| 76 | 88 | unset($this->changedProperties[$property]); |
|
| 77 | } |
||
| 78 | 88 | } |
|
| 79 | |||
| 80 | 280 | public function HasPropertyChanged(string $property) : bool |
|
| 81 | { |
||
| 82 | 280 | return $this->changedProperties[$property] ?? false; |
|
| 83 | } |
||
| 84 | |||
| 85 | 124 | public function jsonSerialize() : array |
|
| 86 | { |
||
| 87 | /** |
||
| 88 | * @var array<int, string> |
||
| 89 | */ |
||
| 90 | 124 | $properties = static::DaftObjectJsonPropertyNames(); |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @var array<string, string> |
||
| 94 | */ |
||
| 95 | 52 | $properties = array_combine($properties, $properties); |
|
| 96 | |||
| 97 | 52 | return array_filter( |
|
| 98 | 39 | array_map( |
|
| 99 | /** |
||
| 100 | * @return scalar|array|object|null |
||
| 101 | */ |
||
| 102 | function (string $property) { |
||
| 103 | 52 | return $this->DoGetSet($property, false); |
|
| 104 | 52 | }, |
|
| 105 | 26 | $properties |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 106 | ), |
||
| 107 | /** |
||
| 108 | * @param scalar|array|object|null $maybe |
||
| 109 | */ |
||
| 110 | function ($maybe) : bool { |
||
| 111 | 52 | return ! is_null($maybe); |
|
| 112 | 52 | } |
|
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param array<int|string, scalar|(scalar|array|object|null)[]|object|null> $array |
||
| 118 | */ |
||
| 119 | 124 | public static function DaftObjectFromJsonArray( |
|
| 120 | array $array, |
||
| 121 | bool $writeAll = self::BOOL_DEFAULT_WRITEALL |
||
| 122 | ) : DaftJson { |
||
| 123 | 124 | $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class); |
|
| 124 | |||
| 125 | 52 | $array = JsonTypeUtilities::ThrowIfJsonDefNotValid($type, $array); |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @var array<int, string> |
||
| 129 | */ |
||
| 130 | 52 | $props = array_keys($array); |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @var array<int, scalar|object|array|null> |
||
| 134 | */ |
||
| 135 | 52 | $vals = array_map( |
|
| 136 | 52 | JsonTypeUtilities::DaftJsonClosure( |
|
| 137 | 39 | $array, |
|
| 138 | 26 | $writeAll, |
|
| 139 | 26 | $type |
|
| 140 | ), |
||
| 141 | 26 | $props |
|
| 142 | ); |
||
| 143 | |||
| 144 | 52 | return new $type(array_combine($props, $vals), $writeAll); |
|
| 145 | } |
||
| 146 | |||
| 147 | 196 | public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
| 148 | { |
||
| 149 | /** |
||
| 150 | * @var scalar|array<int|string, scalar|(scalar|array|object|null)[]|object|null>|object|null |
||
| 151 | */ |
||
| 152 | 196 | $decoded = json_decode($string, true); |
|
| 153 | |||
| 154 | 196 | return JsonTypeUtilities::ThrowIfNotDaftJson(static::class)::DaftObjectFromJsonArray( |
|
| 155 | 52 | is_array($decoded) ? $decoded : [$decoded] |
|
| 156 | ); |
||
| 157 | } |
||
| 158 | |||
| 159 | 316 | public function DaftObjectWormPropertyWritten(string $property) : bool |
|
| 160 | { |
||
| 161 | 316 | $wormProperties = $this->wormProperties; |
|
| 162 | |||
| 163 | return |
||
| 164 | 316 | ($this instanceof DaftObjectWorm) && |
|
| 165 | ( |
||
| 166 | 192 | $this->HasPropertyChanged($property) || |
|
| 167 | 316 | isset($wormProperties[$property]) |
|
| 168 | ); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Retrieve a property from data. |
||
| 173 | * |
||
| 174 | * @param string $property the property being retrieved |
||
| 175 | * |
||
| 176 | * @throws Exceptions\PropertyNotNullableException if value is not set and $property is not listed as nullabe |
||
| 177 | * |
||
| 178 | * @return scalar|array|object|null the property value |
||
| 179 | */ |
||
| 180 | 184 | protected function RetrievePropertyValueFromData(string $property) |
|
| 181 | { |
||
| 182 | 184 | $isNullable = in_array( |
|
| 183 | 138 | $property, |
|
| 184 | 184 | static::DaftObjectNullableProperties(), |
|
| 185 | 184 | DefinitionAssistant::IN_ARRAY_STRICT_MODE |
|
| 186 | ); |
||
| 187 | |||
| 188 | 184 | if ( ! array_key_exists($property, $this->data) && ! $isNullable) { |
|
| 189 | 4 | throw Exceptions\Factory::PropertyNotNullableException(static::class, $property); |
|
| 190 | 180 | } elseif ($isNullable) { |
|
| 191 | 128 | return $this->data[$property] ?? null; |
|
| 192 | } |
||
| 193 | |||
| 194 | 120 | return $this->data[$property]; |
|
| 195 | } |
||
| 196 | |||
| 197 | 4 | protected function RetrievePropertyValueFromDataExpectStringOrNull(string $property) : ? string |
|
| 198 | { |
||
| 199 | 4 | $value = $this->RetrievePropertyValueFromData($property); |
|
| 200 | |||
| 201 | 4 | if (is_null($value)) { |
|
| 202 | 4 | return null; |
|
| 203 | } |
||
| 204 | |||
| 205 | 4 | return TypeUtilities::ExpectRetrievedValueIsString($property, $value, static::class); |
|
| 206 | } |
||
| 207 | |||
| 208 | 4 | protected function RetrievePropertyValueFromDataExpectArrayOrNull(string $property) : ? array |
|
| 209 | { |
||
| 210 | 4 | $value = $this->RetrievePropertyValueFromData($property); |
|
| 211 | |||
| 212 | 4 | if (is_null($value)) { |
|
| 213 | 4 | return null; |
|
| 214 | } |
||
| 215 | |||
| 216 | 4 | return TypeUtilities::ExpectRetrievedValueIsArray($property, $value, static::class); |
|
| 217 | } |
||
| 218 | |||
| 219 | 12 | protected function RetrievePropertyValueFromDataExpectIntishOrNull(string $property) : ? int |
|
| 220 | { |
||
| 221 | 12 | $value = $this->RetrievePropertyValueFromData($property); |
|
| 222 | |||
| 223 | 12 | if (is_null($value)) { |
|
| 224 | 12 | return null; |
|
| 225 | } |
||
| 226 | |||
| 227 | 12 | return TypeUtilities::ExpectRetrievedValueIsIntish($property, $value, static::class); |
|
| 228 | } |
||
| 229 | |||
| 230 | 12 | protected function RetrievePropertyValueFromDataExpectFloatishOrNull(string $property) : ? float |
|
| 231 | { |
||
| 232 | 12 | $value = $this->RetrievePropertyValueFromData($property); |
|
| 233 | |||
| 234 | 12 | if (is_null($value)) { |
|
| 235 | 12 | return null; |
|
| 236 | } |
||
| 237 | |||
| 238 | 12 | return TypeUtilities::ExpectRetrievedValueIsFloatish($property, $value, static::class); |
|
| 239 | } |
||
| 240 | |||
| 241 | 28 | protected function RetrievePropertyValueFromDataExpectBoolishOrNull(string $property) : ? bool |
|
| 242 | { |
||
| 243 | 28 | $value = $this->RetrievePropertyValueFromData($property); |
|
| 244 | |||
| 245 | 28 | if (is_null($value)) { |
|
| 246 | 28 | return null; |
|
| 247 | } |
||
| 248 | |||
| 249 | 28 | return TypeUtilities::ExpectRetrievedValueIsBoolish($property, $value, static::class); |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param scalar|array|object|null $value |
||
| 254 | */ |
||
| 255 | 528 | protected function NudgePropertyValue( |
|
| 256 | string $property, |
||
| 257 | $value, |
||
| 258 | bool $autoTrimStrings = self::BOOL_DEFAULT_AUTOTRIMSTRINGS, |
||
| 259 | bool $throwIfNotUnique = self::BOOL_DEFAULT_THROWIFNOTUNIQUE |
||
| 260 | ) : void { |
||
| 261 | 528 | TypeUtilities::MaybeThrowOnNudge(static::class, $property, $value); |
|
| 262 | |||
| 263 | 316 | if ($this->DaftObjectWormPropertyWritten($property)) { |
|
| 264 | 192 | throw Exceptions\Factory::PropertyNotRewriteableException(static::class, $property); |
|
| 265 | } |
||
| 266 | |||
| 267 | 316 | $value = $this->MaybeModifyValueBeforeNudge( |
|
| 268 | 237 | $property, |
|
| 269 | 158 | $value, |
|
| 270 | 158 | $autoTrimStrings, |
|
| 271 | 158 | $throwIfNotUnique |
|
| 272 | ); |
||
| 273 | |||
| 274 | $isChanged = ( |
||
| 275 | 300 | ! array_key_exists($property, $this->data) || |
|
| 276 | 300 | $this->data[$property] !== $value |
|
| 277 | ); |
||
| 278 | |||
| 279 | 300 | $this->data[$property] = $value; |
|
| 280 | |||
| 281 | 300 | if ($isChanged && true !== isset($this->changedProperties[$property])) { |
|
| 282 | 300 | $this->changedProperties[$property] = $this->wormProperties[$property] = true; |
|
| 283 | } |
||
| 284 | 300 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param scalar|array|object|null $value |
||
| 288 | * |
||
| 289 | * @return scalar|array|object|null |
||
| 290 | */ |
||
| 291 | 316 | private function MaybeModifyValueBeforeNudge( |
|
| 292 | string $property, |
||
| 293 | $value, |
||
| 294 | bool $autoTrimStrings = self::BOOL_DEFAULT_AUTOTRIMSTRINGS, |
||
| 295 | bool $throwIfNotUnique = self::BOOL_DEFAULT_THROWIFNOTUNIQUE |
||
| 296 | ) { |
||
| 297 | 316 | $spec = null; |
|
| 298 | |||
| 299 | if ( |
||
| 300 | 316 | is_a( |
|
| 301 | 316 | static::class, |
|
| 302 | 316 | DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues::class, |
|
| 303 | 316 | true |
|
| 304 | ) |
||
| 305 | ) { |
||
| 306 | $spec = ( |
||
| 307 | 24 | static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues()[$property] ?? null |
|
| 308 | ); |
||
| 309 | } |
||
| 310 | |||
| 311 | 316 | if (is_array($spec)) { |
|
| 312 | 20 | $value = DefinitionAssistant::MaybeThrowIfValueDoesNotMatchMultiTypedArray( |
|
| 313 | 15 | $autoTrimStrings, |
|
| 314 | 10 | $throwIfNotUnique, |
|
| 315 | 10 | $value, |
|
| 316 | 8 | ...$spec |
|
| 317 | ); |
||
| 318 | } |
||
| 319 | |||
| 320 | 300 | if (is_string($value) && $autoTrimStrings) { |
|
| 321 | 4 | $value = trim($value); |
|
| 322 | } |
||
| 323 | |||
| 324 | 300 | return $value; |
|
| 325 | } |
||
| 326 | } |
||
| 327 |