SignpostMarv /
Daft-Schema.org
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author SignpostMarv |
||
| 4 | */ |
||
| 5 | declare(strict_types=1); |
||
| 6 | |||
| 7 | namespace SignpostMarv\DaftObject\SchemaOrg; |
||
| 8 | |||
| 9 | use InvalidArgumentException; |
||
| 10 | use SignpostMarv\DaftObject\AbstractArrayBackedDaftObject; |
||
| 11 | use SignpostMarv\DaftObject\DaftJson; |
||
| 12 | use SignpostMarv\DaftObject\DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues; |
||
| 13 | use SignpostMarv\DaftObject\JsonTypeUtilities; |
||
| 14 | use SignpostMarv\DaftObject\SchemaOrg\CreativeWork\MediaObject\ImageObject; |
||
| 15 | use SignpostMarv\DaftObject\SchemaOrg\Intangible\StructuredValue\PropertyValue; |
||
| 16 | use SignpostMarv\DaftObject\SchemaOrgLookup\LookupInterface; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @property array<int, string> $additionalType |
||
| 20 | * @property array<int, string> $alternateName |
||
| 21 | * @property array<int, string> $description |
||
| 22 | * @property array<int, string> $disambiguatingDescription |
||
| 23 | * @property array<int, string|PropertyValue> $identifier |
||
| 24 | * @property array<int, string|ImageObject> $image |
||
| 25 | * @property array<int, string|CreativeWork> $mainEntityOfPage |
||
| 26 | * @property array<int, string> $name |
||
| 27 | * @property array<int, Action> $potentialAction |
||
| 28 | * @property array<int, string> $sameAs |
||
| 29 | * @property array<int, CreativeWork|Event> $subjectOf |
||
| 30 | * @property array<int, Action> $potentialAction |
||
| 31 | * @property array<int, string> $url |
||
| 32 | * |
||
| 33 | * @template-implements DaftJson<Thing> |
||
| 34 | */ |
||
| 35 | class Thing extends AbstractArrayBackedDaftObject implements |
||
| 36 | DaftJson, |
||
| 37 | DaftObjectHasPropertiesWithMultiTypedArraysOfUniqueValues |
||
| 38 | { |
||
| 39 | const BOOL_DEFAULT_AUTOTRIMSTRINGS = true; |
||
| 40 | |||
| 41 | const INT_FILL_FROM_START = 0; |
||
| 42 | |||
| 43 | const INT_COUNT_NOT_EMPTY = 0; |
||
| 44 | |||
| 45 | const SCHEMA_ORG_CONTEXT = 'http://schema.org'; |
||
| 46 | |||
| 47 | const SCHEMA_ORG_TYPE = 'Thing'; |
||
| 48 | |||
| 49 | const PROPERTIES = [ |
||
| 50 | '@context', |
||
| 51 | '@type', |
||
| 52 | 'additionalType', |
||
| 53 | 'alternateName', |
||
| 54 | 'description', |
||
| 55 | 'disambiguatingDescription', |
||
| 56 | 'identifier', |
||
| 57 | 'image', |
||
| 58 | 'mainEntityOfPage', |
||
| 59 | 'name', |
||
| 60 | 'potentialAction', |
||
| 61 | 'sameAs', |
||
| 62 | 'subjectOf', |
||
| 63 | 'url', |
||
| 64 | ]; |
||
| 65 | |||
| 66 | const PROPERTIES_WITH_MULTI_TYPED_ARRAYS = [ |
||
| 67 | 'additionalType' => [ |
||
| 68 | 'string', |
||
| 69 | ], |
||
| 70 | 'alternateName' => [ |
||
| 71 | 'string', |
||
| 72 | ], |
||
| 73 | 'description' => [ |
||
| 74 | 'string', |
||
| 75 | ], |
||
| 76 | 'disambiguatingDescription' => [ |
||
| 77 | 'string', |
||
| 78 | ], |
||
| 79 | 'identifier' => [ |
||
| 80 | 'string', |
||
| 81 | PropertyValue::class, |
||
| 82 | ], |
||
| 83 | 'image' => [ |
||
| 84 | 'string', |
||
| 85 | ImageObject::class, |
||
| 86 | ], |
||
| 87 | 'mainEntityOfPage' => [ |
||
| 88 | 'string', |
||
| 89 | CreativeWork::class, |
||
| 90 | ], |
||
| 91 | 'name' => [ |
||
| 92 | 'string', |
||
| 93 | ], |
||
| 94 | 'potentialAction' => [ |
||
| 95 | Action::class, |
||
| 96 | ], |
||
| 97 | 'sameAs' => [ |
||
| 98 | 'string', |
||
| 99 | ], |
||
| 100 | 'subjectOf' => [ |
||
| 101 | CreativeWork::class, |
||
| 102 | Event::class, |
||
| 103 | ], |
||
| 104 | 'url' => [ |
||
| 105 | 'string', |
||
| 106 | ], |
||
| 107 | ]; |
||
| 108 | |||
| 109 | 5143 | public function __construct(array $data = [], bool $writeAll = false) |
|
| 110 | { |
||
| 111 | 5143 | $missing = array_diff(static::DaftObjectProperties(), array_keys($data)); |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @var array<int, string> |
||
| 115 | */ |
||
| 116 | 5143 | $missing = array_combine( |
|
| 117 | 5143 | $missing, |
|
| 118 | 5143 | array_fill(self::INT_FILL_FROM_START, count($missing), []) |
|
| 119 | ); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array<string, scalar|array|object|null> |
||
| 123 | */ |
||
| 124 | 5143 | $data = array_merge( |
|
| 125 | 5143 | $data, |
|
| 126 | 5143 | $missing |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 127 | ); |
||
| 128 | |||
| 129 | 5143 | unset($data['@context'], $data['@type']); |
|
| 130 | |||
| 131 | 5143 | parent::__construct($data, $writeAll); |
|
| 132 | 5143 | } |
|
| 133 | |||
| 134 | 237 | public function ObtainContext() : string |
|
| 135 | { |
||
| 136 | 237 | return (string) static::SCHEMA_ORG_CONTEXT; |
|
| 137 | } |
||
| 138 | |||
| 139 | 237 | public function ObtainType() : string |
|
| 140 | { |
||
| 141 | 237 | return (string) static::SCHEMA_ORG_TYPE; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return array<int, string> |
||
| 146 | */ |
||
| 147 | 346 | public function GetAdditionalType() : array |
|
| 148 | { |
||
| 149 | /** |
||
| 150 | * @var array<int, string> |
||
| 151 | */ |
||
| 152 | 346 | $out = $this->ExpectRetrievedValueIsArray('additionalType'); |
|
| 153 | |||
| 154 | 346 | return $out; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param array<int, string> $value |
||
| 159 | */ |
||
| 160 | 110 | public function SetAdditionalType(array $value) : void |
|
| 161 | { |
||
| 162 | 110 | $this->NudgePropertyValue('additionalType', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 163 | 110 | } |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @return array<int, string> |
||
| 167 | */ |
||
| 168 | 346 | public function GetAlternateName() : array |
|
| 169 | { |
||
| 170 | /** |
||
| 171 | * @var array<int, string> |
||
| 172 | */ |
||
| 173 | 346 | $out = $this->ExpectRetrievedValueIsArray('alternateName'); |
|
| 174 | |||
| 175 | 346 | return $out; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array<int, string> $value |
||
| 180 | */ |
||
| 181 | 110 | public function SetAlternateName(array $value) : void |
|
| 182 | { |
||
| 183 | 110 | $this->NudgePropertyValue('alternateName', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 184 | 110 | } |
|
| 185 | |||
| 186 | /** |
||
| 187 | * @return array<int, string> |
||
| 188 | */ |
||
| 189 | 346 | public function GetDescription() : array |
|
| 190 | { |
||
| 191 | /** |
||
| 192 | * @var array<int, string> |
||
| 193 | */ |
||
| 194 | 346 | $out = $this->ExpectRetrievedValueIsArray('description'); |
|
| 195 | |||
| 196 | 346 | return $out; |
|
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param array<int, string> $value |
||
| 201 | */ |
||
| 202 | 110 | public function SetDescription(array $value) : void |
|
| 203 | { |
||
| 204 | 110 | $this->NudgePropertyValue('description', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 205 | 110 | } |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return array<int, string> |
||
| 209 | */ |
||
| 210 | 346 | public function GetDisambiguatingDescription() : array |
|
| 211 | { |
||
| 212 | /** |
||
| 213 | * @var array<int, string> |
||
| 214 | */ |
||
| 215 | 346 | $out = $this->ExpectRetrievedValueIsArray('disambiguatingDescription'); |
|
| 216 | |||
| 217 | 346 | return $out; |
|
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array<int, string> $value |
||
| 222 | */ |
||
| 223 | 110 | public function SetDisambiguatingDescription(array $value) : void |
|
| 224 | { |
||
| 225 | 110 | $this->NudgePropertyValue('disambiguatingDescription', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 226 | 110 | } |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @return array<int, string|PropertyValue> |
||
| 230 | */ |
||
| 231 | 346 | public function GetIdentifier() : array |
|
| 232 | { |
||
| 233 | /** |
||
| 234 | * @var array<int, string|PropertyValue> |
||
| 235 | */ |
||
| 236 | 346 | $out = $this->ExpectRetrievedValueIsArray('identifier'); |
|
| 237 | |||
| 238 | 346 | return $out; |
|
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param array<int, string|PropertyValue> $value |
||
| 243 | */ |
||
| 244 | 110 | public function SetIdentifier(array $value) : void |
|
| 245 | { |
||
| 246 | 110 | $this->NudgePropertyValue('identifier', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 247 | 110 | } |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @return array<int, string|ImageObject> |
||
| 251 | */ |
||
| 252 | 346 | public function GetImage() : array |
|
| 253 | { |
||
| 254 | /** |
||
| 255 | * @var array<int, string|ImageObject> |
||
| 256 | */ |
||
| 257 | 346 | $out = $this->ExpectRetrievedValueIsArray('image'); |
|
| 258 | |||
| 259 | 346 | return $out; |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param array<int, string|ImageObject> $value |
||
| 264 | */ |
||
| 265 | 110 | public function SetImage(array $value) : void |
|
| 266 | { |
||
| 267 | 110 | $this->NudgePropertyValue('image', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 268 | 110 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @return array<int, string|CreativeWork> |
||
| 272 | */ |
||
| 273 | 346 | public function GetMainEntityOfPage() : array |
|
| 274 | { |
||
| 275 | /** |
||
| 276 | * @var array<int, string|CreativeWork> |
||
| 277 | */ |
||
| 278 | 346 | $out = $this->ExpectRetrievedValueIsArray('mainEntityOfPage'); |
|
| 279 | |||
| 280 | 346 | return $out; |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * @param array<int, string|CreativeWork> $value |
||
| 285 | */ |
||
| 286 | 110 | public function SetMainEntityOfPage(array $value) : void |
|
| 287 | { |
||
| 288 | 110 | $this->NudgePropertyValue('mainEntityOfPage', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 289 | 110 | } |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @return array<int, string> |
||
| 293 | */ |
||
| 294 | 346 | public function GetName() : array |
|
| 295 | { |
||
| 296 | /** |
||
| 297 | * @var array<int, string> |
||
| 298 | */ |
||
| 299 | 346 | $out = $this->ExpectRetrievedValueIsArray('name'); |
|
| 300 | |||
| 301 | 346 | return $out; |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param array<int, string> $value |
||
| 306 | */ |
||
| 307 | 110 | public function SetName(array $value) : void |
|
| 308 | { |
||
| 309 | 110 | $this->NudgePropertyValue('name', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 310 | 110 | } |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @return array<int, Action> |
||
| 314 | */ |
||
| 315 | 346 | public function GetPotentialAction() : array |
|
| 316 | { |
||
| 317 | /** |
||
| 318 | * @var array<int, Action> |
||
| 319 | */ |
||
| 320 | 346 | $out = $this->ExpectRetrievedValueIsArray('potentialAction'); |
|
| 321 | |||
| 322 | 346 | return $out; |
|
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param array<int, Action> $value |
||
| 327 | */ |
||
| 328 | 110 | public function SetPotentialAction(array $value) : void |
|
| 329 | { |
||
| 330 | 110 | $this->NudgePropertyValue('potentialAction', $value); |
|
| 331 | 110 | } |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @return array<int, string> |
||
| 335 | */ |
||
| 336 | 346 | public function GetSameAs() : array |
|
| 337 | { |
||
| 338 | /** |
||
| 339 | * @var array<int, string> |
||
| 340 | */ |
||
| 341 | 346 | $out = $this->ExpectRetrievedValueIsArray('sameAs'); |
|
| 342 | |||
| 343 | 346 | return $out; |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param array<int, string> $value |
||
| 348 | */ |
||
| 349 | 110 | public function SetSameAs(array $value) : void |
|
| 350 | { |
||
| 351 | 110 | $this->NudgePropertyValue('sameAs', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 352 | 110 | } |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @return array<int, CreativeWork|Event> |
||
| 356 | */ |
||
| 357 | 346 | public function GetSubjectOf() : array |
|
| 358 | { |
||
| 359 | /** |
||
| 360 | * @var array<int, CreativeWork|Event> |
||
| 361 | */ |
||
| 362 | 346 | $out = $this->ExpectRetrievedValueIsArray('subjectOf'); |
|
| 363 | |||
| 364 | 346 | return $out; |
|
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param array<int, CreativeWork|Event> $value |
||
| 369 | */ |
||
| 370 | 110 | public function SetSubjectOf(array $value) : void |
|
| 371 | { |
||
| 372 | 110 | $this->NudgePropertyValue('subjectOf', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 373 | 110 | } |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @return array<int, string> |
||
| 377 | */ |
||
| 378 | 346 | public function GetUrl() : array |
|
| 379 | { |
||
| 380 | /** |
||
| 381 | * @var array<int, string> |
||
| 382 | */ |
||
| 383 | 346 | $out = $this->ExpectRetrievedValueIsArray('url'); |
|
| 384 | |||
| 385 | 346 | return $out; |
|
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @param array<int, string> $value |
||
| 390 | */ |
||
| 391 | 110 | public function SetUrl(array $value) : void |
|
| 392 | { |
||
| 393 | 110 | $this->NudgePropertyValue('url', $value, self::BOOL_DEFAULT_AUTOTRIMSTRINGS); |
|
| 394 | 110 | } |
|
| 395 | |||
| 396 | 237 | public function jsonSerialize() : array |
|
| 397 | { |
||
| 398 | 237 | return array_filter( |
|
| 399 | 237 | parent::jsonSerialize(), |
|
| 400 | /** |
||
| 401 | * @param scalar|array|object|null $val |
||
| 402 | */ |
||
| 403 | function ($val) : bool { |
||
| 404 | 237 | return ! is_array($val) || count($val) > self::INT_COUNT_NOT_EMPTY; |
|
| 405 | 237 | } |
|
| 406 | ); |
||
| 407 | } |
||
| 408 | |||
| 409 | 15553 | public static function DaftObjectProperties() : array |
|
| 410 | { |
||
| 411 | /** |
||
| 412 | * @var array<int, string> |
||
| 413 | */ |
||
| 414 | 15553 | $static = static::PROPERTIES; |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @var string |
||
| 418 | * |
||
| 419 | * @psalm-var class-string<Thing> |
||
| 420 | */ |
||
| 421 | 15553 | $static_parent = get_parent_class(static::class); |
|
| 422 | |||
| 423 | 15553 | $parent = $static_parent::DaftObjectProperties(); |
|
| 424 | |||
| 425 | 15553 | return array_unique(array_merge( |
|
| 426 | 15553 | $parent, |
|
| 427 | 15553 | $static |
|
| 428 | )); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return array<string, array<int, string>> |
||
| 433 | */ |
||
| 434 | 475 | public static function DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues() : array |
|
| 435 | { |
||
| 436 | /** |
||
| 437 | * @var array<string, array<int, string>> |
||
| 438 | */ |
||
| 439 | 475 | $static = static::PROPERTIES_WITH_MULTI_TYPED_ARRAYS; |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @var string |
||
| 443 | * |
||
| 444 | * @psalm-var class-string<Thing> |
||
| 445 | */ |
||
| 446 | 475 | $static_parent = get_parent_class(static::class); |
|
| 447 | |||
| 448 | 475 | if ($static_parent === get_parent_class(self::class)) { |
|
| 449 | 475 | return $static; |
|
| 450 | } |
||
| 451 | |||
| 452 | 472 | $parent = $static_parent::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues(); |
|
| 453 | |||
| 454 | 472 | return array_merge( |
|
| 455 | 472 | $parent, |
|
| 456 | 472 | $static |
|
| 457 | ); |
||
| 458 | } |
||
| 459 | |||
| 460 | 5252 | public static function DaftObjectNullableProperties() : array |
|
| 461 | { |
||
| 462 | /** |
||
| 463 | * @var array<int, string> |
||
| 464 | */ |
||
| 465 | 5252 | $static = static::NULLABLE_PROPERTIES; |
|
| 466 | |||
| 467 | /** |
||
| 468 | * @var string |
||
| 469 | * |
||
| 470 | * @psalm-var class-string<Thing> |
||
| 471 | */ |
||
| 472 | 5252 | $static_parent = get_parent_class(static::class); |
|
| 473 | |||
| 474 | 5252 | $parent = $static_parent::DaftObjectNullableProperties(); |
|
| 475 | |||
| 476 | 5252 | return array_unique(array_merge( |
|
| 477 | 5252 | $parent, |
|
| 478 | 5252 | $static |
|
| 479 | )); |
||
| 480 | } |
||
| 481 | |||
| 482 | 454 | public static function DaftObjectExportableProperties() : array |
|
| 483 | { |
||
| 484 | 454 | return static::DaftObjectProperties(); |
|
| 485 | } |
||
| 486 | |||
| 487 | 473 | public static function DaftObjectJsonProperties() : array |
|
| 488 | { |
||
| 489 | 473 | return static::DaftObjectProperties(); |
|
| 490 | } |
||
| 491 | |||
| 492 | 472 | public static function DaftObjectPublicGetters() : array |
|
| 493 | { |
||
| 494 | /** |
||
| 495 | * @var string |
||
| 496 | * |
||
| 497 | * @psalm-var class-string<Thing> |
||
| 498 | */ |
||
| 499 | 472 | $static_parent = get_parent_class(static::class); |
|
| 500 | |||
| 501 | 472 | $static = TypeUtilities::DaftObjectPublicGetters(static::class); |
|
| 502 | |||
| 503 | 472 | if ($static_parent === get_parent_class(self::class)) { |
|
| 504 | 53 | return $static; |
|
| 505 | } |
||
| 506 | |||
| 507 | 471 | return array_unique(array_merge( |
|
| 508 | 471 | TypeUtilities::DaftObjectPublicGetters($static_parent), |
|
| 509 | 471 | $static |
|
| 510 | )); |
||
| 511 | } |
||
| 512 | |||
| 513 | 1 | public static function DaftObjectPublicOrProtectedGetters() : array |
|
| 514 | { |
||
| 515 | /** |
||
| 516 | * @var string |
||
| 517 | * |
||
| 518 | * @psalm-var class-string<Thing> |
||
| 519 | */ |
||
| 520 | 1 | $static_parent = get_parent_class(static::class); |
|
| 521 | |||
| 522 | 1 | $static = TypeUtilities::DaftObjectPublicOrProtectedGetters(static::class); |
|
| 523 | |||
| 524 | 1 | if ($static_parent === get_parent_class(self::class)) { |
|
| 525 | 1 | return $static; |
|
| 526 | } |
||
| 527 | |||
| 528 | 1 | return array_unique(array_merge( |
|
| 529 | 1 | TypeUtilities::DaftObjectPublicOrProtectedGetters($static_parent), |
|
| 530 | 1 | $static |
|
| 531 | )); |
||
| 532 | } |
||
| 533 | |||
| 534 | 236 | public static function DaftObjectPublicSetters() : array |
|
| 535 | { |
||
| 536 | /** |
||
| 537 | * @var string |
||
| 538 | * |
||
| 539 | * @psalm-var class-string<Thing> |
||
| 540 | */ |
||
| 541 | 236 | $static_parent = get_parent_class(static::class); |
|
| 542 | |||
| 543 | 236 | $static = TypeUtilities::DaftObjectPublicSetters(static::class); |
|
| 544 | |||
| 545 | 236 | if ($static_parent === get_parent_class(self::class)) { |
|
| 546 | 2 | return $static; |
|
| 547 | } |
||
| 548 | |||
| 549 | 235 | return array_unique(array_merge( |
|
| 550 | 235 | TypeUtilities::DaftObjectPublicSetters($static_parent), |
|
| 551 | 235 | $static |
|
| 552 | )); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @return static |
||
| 557 | * |
||
| 558 | * @psalm-return Thing |
||
| 559 | */ |
||
| 560 | 237 | public static function DaftObjectFromJsonString( |
|
| 561 | string $string, |
||
| 562 | bool $writeAll = self::BOOL_DEFAULT_WRITEALL |
||
| 563 | ) : DaftJson { |
||
| 564 | /** |
||
| 565 | * @var array<string, scalar|(scalar|array|object|null)[]|object|null> |
||
| 566 | */ |
||
| 567 | 237 | $decoded = json_decode($string, true); |
|
| 568 | |||
| 569 | /** |
||
| 570 | * @var static |
||
| 571 | */ |
||
| 572 | 237 | $out = static::DaftObjectFromJsonArray($decoded, $writeAll); |
|
| 573 | |||
| 574 | 237 | return $out; |
|
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * @return static |
||
| 579 | * |
||
| 580 | * @psalm-return Thing |
||
| 581 | */ |
||
| 582 | 239 | public static function DaftObjectFromJsonArray( |
|
| 583 | array $array, |
||
| 584 | bool $writeAll = self::BOOL_DEFAULT_WRITEALL |
||
| 585 | ) : DaftJson { |
||
| 586 | 239 | $type = JsonTypeUtilities::ThrowIfNotDaftJson(static::class); |
|
| 587 | |||
| 588 | 239 | $multi_type = static::DaftObjectPropertiesWithMultiTypedArraysOfUniqueValues(); |
|
| 589 | |||
| 590 | 239 | $array_keys = array_keys($array); |
|
| 591 | |||
| 592 | 239 | foreach ($array_keys as $k) { |
|
| 593 | 239 | if ( ! is_string($k)) { |
|
| 594 | 1 | throw new InvalidArgumentException( |
|
| 595 | 'Argument 1 passed to ' . |
||
| 596 | __METHOD__ . |
||
| 597 | 239 | '() must have all-string indices!' |
|
| 598 | ); |
||
| 599 | } |
||
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * @var array<int, string> |
||
| 604 | */ |
||
| 605 | 238 | $array_keys = $array_keys; |
|
| 606 | |||
| 607 | 238 | $data = array_combine($array_keys, array_map( |
|
| 608 | /** |
||
| 609 | * @param string $k |
||
| 610 | * |
||
| 611 | * @return mixed |
||
| 612 | */ |
||
| 613 | function (string $k) use ($array, $multi_type) { |
||
| 614 | 238 | if (is_array($array[$k])) { |
|
| 615 | 237 | return static::DaftObjectFromJsonArrayFromArray($k, $multi_type, $array[$k]); |
|
| 616 | } |
||
| 617 | |||
| 618 | 237 | return $array[$k]; |
|
| 619 | 238 | }, |
|
| 620 | 238 | $array_keys |
|
| 621 | )); |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @psalm-var Thing |
||
| 625 | * |
||
| 626 | * @var Thing |
||
| 627 | */ |
||
| 628 | 237 | $out = new $type($data, $writeAll); |
|
| 629 | |||
| 630 | 237 | return $out; |
|
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param array<string, array<int, string>> $multi_type |
||
| 635 | */ |
||
| 636 | 237 | protected static function DaftObjectFromJsonArrayFromArray( |
|
| 637 | string $k, |
||
| 638 | array $multi_type, |
||
| 639 | array $arr |
||
| 640 | ) : array { |
||
| 641 | 237 | return array_map( |
|
| 642 | /** |
||
| 643 | * @param mixed $val |
||
| 644 | * |
||
| 645 | * @return mixed |
||
| 646 | */ |
||
| 647 | function ($val) use ($k, $multi_type) { |
||
| 648 | if ( |
||
| 649 | 237 | is_array($val) && |
|
| 650 | 237 | isset($val['@context'], $val['@type'], $multi_type[$k]) |
|
| 651 | ) { |
||
| 652 | /** |
||
| 653 | * @psalm-var array<string, array<array-key, array<array-key, mixed>|scalar|object|null>|scalar|object|null> |
||
| 654 | */ |
||
| 655 | 230 | $val = $val; |
|
| 656 | |||
| 657 | 230 | return static::DaftObjectFromJsonArrayFromArrayMapVal($val, $multi_type, $k); |
|
| 658 | } |
||
| 659 | |||
| 660 | 236 | return $val; |
|
| 661 | 237 | }, |
|
| 662 | 237 | $arr |
|
| 663 | ); |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param array<string, array<int, string>> $multi_type |
||
| 668 | * |
||
| 669 | * @psalm-param array<string, array<array-key, array<array-key, mixed>|scalar|object|null>|scalar|object|null> $val |
||
| 670 | */ |
||
| 671 | 230 | protected static function DaftObjectFromJsonArrayFromArrayMapVal( |
|
| 672 | array $val, |
||
| 673 | array $multi_type, |
||
| 674 | string $k |
||
| 675 | ) : Thing { |
||
| 676 | 230 | foreach ($multi_type[$k] as $maybe) { |
|
| 677 | $lookup_class = |
||
| 678 | 'SignpostMarv\\DaftObject\\SchemaOrgLookup\\Lookup_' . |
||
| 679 | 230 | hash('sha512', $maybe); |
|
| 680 | 230 | if (is_a($lookup_class, LookupInterface::class, true)) { |
|
| 681 | /** |
||
| 682 | * @psalm-var array<int, class-string<Thing>> |
||
| 683 | */ |
||
| 684 | 230 | $maybe_classes = $lookup_class::ObtainClasses(); |
|
| 685 | |||
| 686 | 230 | foreach ($maybe_classes as $maybe_class) { |
|
| 687 | if ( |
||
| 688 | 230 | $val['@context'] === $maybe_class::SCHEMA_ORG_CONTEXT && |
|
| 689 | 230 | $val['@type'] === $maybe_class::SCHEMA_ORG_TYPE |
|
| 690 | ) { |
||
| 691 | 230 | return $maybe_class::DaftObjectFromJsonArray($val); |
|
| 692 | } |
||
| 693 | } |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | 1 | throw new InvalidArgumentException( |
|
| 698 | 'Argument 3 (' . |
||
| 699 | 1 | $k . |
|
| 700 | 1 | ') passed to ' . |
|
| 701 | 1 | __METHOD__ . |
|
| 702 | 1 | '() did not correspond to an instance of ' . |
|
| 703 | 1 | Thing::class . |
|
| 704 | 1 | ' as defined in argument 2!' |
|
| 705 | ); |
||
| 706 | } |
||
| 707 | |||
| 708 | 389 | protected function ExpectRetrievedValueIsArray(string $property) : array |
|
| 709 | { |
||
| 710 | 389 | return TypeUtilities::ExpectRetrievedValueIsArray( |
|
| 711 | 389 | $property, |
|
| 712 | 389 | $this->RetrievePropertyValueFromData($property), |
|
| 713 | 389 | static::class |
|
| 714 | ); |
||
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @param array<int, bool> $value |
||
| 719 | */ |
||
| 720 | 37 | protected function NudgePropertyWithUniqueBooleans( |
|
| 721 | string $property, |
||
| 722 | array $value |
||
| 723 | ) : void { |
||
| 724 | 37 | $replace = []; |
|
| 725 | 37 | if (in_array(true, $value, true)) { |
|
| 726 | 35 | $replace[] = true; |
|
| 727 | } |
||
| 728 | 37 | if (in_array(false, $value, true)) { |
|
| 729 | 34 | $replace[] = false; |
|
| 730 | } |
||
| 731 | |||
| 732 | 37 | $this->NudgePropertyValue($property, $replace); |
|
| 733 | 37 | } |
|
| 734 | } |
||
| 735 |