Passed
Push — master ( f10fbb...da69d2 )
by SignpostMarv
12:22
created

Thing::ExpectRetrievedValueIsArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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