Test Failed
Push — master ( d87250...ffa34c )
by SignpostMarv
12:19
created

Thing::GetDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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