Test Failed
Push — master ( fadfeb...0af13c )
by SignpostMarv
04:18
created

Thing::GetAlternateName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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