Passed
Push — master ( 68008b...85a044 )
by SignpostMarv
12:46
created

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