Passed
Push — master ( 73257f...4486b3 )
by SignpostMarv
13:10
created

Thing::SetDisambiguatingDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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