Passed
Push — master ( 6d0f47...81601f )
by SignpostMarv
12:06
created

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

741
        /** @scrutinizer ignore-unused */ string ...$implementation

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
742
    ) : void {
743
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
744
    }
745
746
    /**
747
    * @param array<int, string|Thing|DataTypes\DataType> $value
748
    *
749
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$implementation
750
    */
751 211
    protected function NudgePropertyWithUniqueTrimmedStringsOrThings(
752
        string $property,
753
        string $method,
754
        array $value,
755
        string ...$implementation
0 ignored issues
show
Unused Code introduced by
The parameter $implementation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

755
        /** @scrutinizer ignore-unused */ string ...$implementation

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
756
    ) : void {
757 211
        $initialCount = count($value);
758
759
        /**
760
        * @var array<int, string|Thing|DataTypes\DataType>
761
        */
762 211
        $value = array_values(array_filter(
763 211
            array_map(
764
                /**
765
                * @param string|Thing|DataTypes\DataType $val
766
                *
767
                * @return string|Thing|DataTypes\DataType
768
                */
769
                function ($val) {
770 210
                    return is_string($val) ? trim($val) : $val;
771 211
                },
772 211
                $value
773
            ),
774
            /**
775
            * @param string|Thing|DataTypes\DataType $maybe
776
            */
777
            function ($maybe) : bool {
778 210
                return '' !== $maybe;
779 211
            }
780
        ));
781
782 211
        if (count($value) !== $initialCount) {
783
            throw new InvalidArgumentException(
784
                'Arguments passed to ' .
785
                __METHOD__ .
786
                ' must be strings with no trailing whitespace or instances of ' .
787
                Thing::class
788
            );
789
        }
790
791 211
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
792 211
    }
793
794
    /**
795
    * @param array<int, int|float|string|Thing|DataTypes\DataType> $value
796
    *
797
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$implementation
798
    */
799 3
    protected function NudgePropertyWithUniqueNumericsOrThings(
800
        string $property,
801
        string $method,
802
        array $value,
803
        string ...$implementation
0 ignored issues
show
Unused Code introduced by
The parameter $implementation is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

803
        /** @scrutinizer ignore-unused */ string ...$implementation

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
804
    ) : void {
805 3
        $initialCount = count($value);
806
807
        /**
808
        * @var array<int, string|Thing|DataTypes\DataType>
809
        */
810 3
        $value = array_values(
811 3
            array_map(
812
                /**
813
                * @param string|int|float|Thing|DataTypes\DataType $val
814
                *
815
                * @return int|float|Thing|DataTypes\DataType
816
                */
817
                function ($val) {
818
                    return
819 3
                        is_string($val)
820
                            ? (
821
                                ctype_digit($val)
822
                                    ? (int) $val
823
                                    : (float) $val
824
                            )
825 3
                            : $val;
826 3
                },
827 3
                $value
828
            )
829
        );
830
831 3
        if (count($value) !== $initialCount) {
832
            throw new InvalidArgumentException(
833
                'Arguments passed to ' .
834
                __METHOD__ .
835
                ' must be numerics or instances of ' .
836
                Thing::class
837
            );
838
        }
839
840 3
        $this->NudgePropertyWithUniqueValues($property, $method, $value, SORT_NUMERIC);
841 3
    }
842
843
    /**
844
    * @param array<int, string> $value
845
    */
846 211
    protected function NudgePropertyWithUniqueTrimmedStringsMightNotBeString(
847
        string $property,
848
        string $method,
849
        array $value
850
    ) : void {
851 211
        $initialCount = count($value);
852
853
        $value = array_filter(array_map('trim', $value), function (string $maybe) : bool {
854 210
            return '' !== $maybe;
855 211
        });
856
857 211
        if ($initialCount !== count($value)) {
858
            throw new InvalidArgumentException(
859
                'Arguments passed to ' .
860
                $method .
861
                ' must not have trailing whitespace!'
862
            );
863
        }
864
865 211
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
866 211
    }
867
868
    /**
869
    * @param array<int, int|float|string> $value
870
    */
871 2
    protected function NudgePropertyWithUniqueTrimmedStringsNumericsMightNotBeStringsOrNumerics(
872
        string $property,
873
        string $method,
874
        array $value
875
    ) : void {
876 2
        $initialCount = count($value);
877
878
        /**
879
        * @var array<int, string|float|int>
880
        */
881 2
        $value = array_filter(
882 2
            $value,
883
            /**
884
            * @param mixed $maybe
885
            */
886
            function ($maybe) : bool {
887 2
                return is_string($maybe) || is_numeric($maybe);
888 2
            }
889
        );
890
891 2
        if (count($value) !== $initialCount) {
892
            throw new InvalidArgumentException(
893
                'Argument 1 passed to ' .
894
                $method .
895
                ' must be an array of numerics!'
896
            );
897
        }
898
899
        /**
900
        * @var array<int, string|int|float>
901
        */
902 2
        $value = array_unique(
903 2
            array_map(
904
                /**
905
                * @param string|float|int $val
906
                *
907
                * @return string|float|int
908
                */
909
                function ($val) {
910
                    return
911 2
                        is_string($val)
912
                            ? (
913 2
                                ctype_digit($val)
914
                                    ? (int) $val
915
                                    : (
916 2
                                        is_numeric($val)
917
                                            ? (float) $val
918 2
                                            : trim($val)
919
                                    )
920
                            )
921 2
                            : $val;
922 2
                },
923 2
                $value
924
            ),
925 2
            SORT_NUMERIC
926
        );
927
928 2
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
929 2
    }
930
931
    /**
932
    * @param array<int, int> $value
933
    */
934 70
    protected function NudgePropertyWithUniqueIntegers(
935
        string $property,
936
        string $method,
937
        array $value
938
    ) : void {
939 70
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
940 70
    }
941
942
    /**
943
    * @param array<int, int|float|string> $value
944
    */
945 5
    protected function NudgePropertyWithUniqueIntegersOrFloats(
946
        string $property,
947
        string $method,
948
        array $value
949
    ) : void {
950 5
        $initialCount = count($value);
951
952
        /**
953
        * @var array<int, int|float>
954
        */
955 5
        $value = array_map(
956
            /**
957
            * @param int|float|string $val
958
            *
959
            * @return int|float
960
            */
961
            function ($val) {
962
                return
963 5
                    (is_int($val) || is_float($val))
964 5
                        ? $val
965
                        : (
966
                            ctype_digit($val)
967
                                ? (int) $val
968 5
                                : (float) $val
969
                        );
970 5
            },
971 5
            array_filter($value, 'is_numeric')
972
        );
973
974 5
        if (count($value) !== $initialCount) {
975
            throw new InvalidArgumentException(
976
                'Argument 1 passed to ' .
977
                $method .
978
                ' must be a numeric list!'
979
            );
980
        }
981
982 5
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
983 5
    }
984
985
    /**
986
    * @param array<int, bool> $value
987
    */
988 69
    protected function NudgePropertyWithUniqueBooleans(
989
        string $property,
990
        array $value
991
    ) : void {
992 69
        $replace = [];
993 69
        if (in_array(true, $value, true)) {
994 69
            $replace[] = true;
995
        }
996 69
        if (in_array(false, $value, true)) {
997
            $replace[] = false;
998
        }
999
1000 69
        $this->NudgePropertyValue($property, $replace);
1001 69
    }
1002
1003
    /**
1004
    * @param array<int, int|string> $value
1005
    */
1006 1
    protected function NudgePropertWithUniqueIntegersOrTrimmedStrings(
1007
        string $property,
1008
        string $method,
1009
        array $value
1010
    ) : void {
1011 1
        $initialCount = count($value);
1012
1013 1
        $value = array_map(
1014
            /**
1015
            * @param int|string $val
1016
            *
1017
            * @return int|string
1018
            */
1019
            function ($val) {
1020 1
                return is_string($val) ? trim($val) : $val;
1021 1
            },
1022 1
            array_filter(
1023 1
                $value,
1024
                /**
1025
                * @param mixed $maybe
1026
                */
1027
                function ($maybe) : bool {
1028 1
                    return is_string($maybe) || is_int($maybe);
1029 1
                }
1030
            )
1031
        );
1032
1033 1
        if (count($value) !== $initialCount) {
1034
            throw new InvalidArgumentException(
1035
                'Argument 1 passed to ' .
1036
                $method .
1037
                ' must be a list of integers & strings!'
1038
            );
1039
        }
1040
1041 1
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1042 1
    }
1043
1044
    /**
1045
    * @param array<int, scalar|array|object|null> $value
1046
    */
1047 211
    protected function NudgePropertyWithUniqueValues(
1048
        string $property,
1049
        string $method,
1050
        array $value,
1051
        int $sort_flags = SORT_REGULAR
0 ignored issues
show
Unused Code introduced by
The parameter $sort_flags is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1051
        /** @scrutinizer ignore-unused */ int $sort_flags = SORT_REGULAR

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1052
    ) : void {
1053 211
        $initialCount = count($value);
1054
1055 211
        $out = [];
1056
1057 211
        foreach ($value as $val) {
1058 211
            if ( ! in_array($val, $out, true)) {
1059 211
                $out[] = $val;
1060
            } else {
1061
                throw new InvalidArgumentException(
1062
                    'Arguments passed to ' .
1063
                    $method .
1064
                    ' must be unique, ' .
1065
                    var_export($val, true) .
1066 211
                    ' already found!'
1067
                );
1068
            }
1069
        }
1070
1071 211
        $value = $out;
1072
1073 211
        if ($initialCount !== count($value)) {
1074
            throw new InvalidArgumentException(
1075
                'Arguments passed to ' .
1076
                $method .
1077
                ' must be unique!'
1078
            );
1079
        }
1080
1081 211
        $this->NudgePropertyValue($property, $value);
1082 211
    }
1083
1084
    /**
1085
    * @param array<int, Date> $value
1086
    */
1087 3
    protected function NudgePropertyWithUniqueDates(
1088
        string $property,
1089
        string $method,
1090
        array $value
1091
    ) : void {
1092 3
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, Date::class);
1093 3
    }
1094
1095
    /**
1096
    * @param array<int, DateTime> $value
1097
    */
1098
    protected function NudgePropertyWithUniqueDateTimes(
1099
        string $property,
1100
        string $method,
1101
        array $value
1102
    ) : void {
1103
        $this->NudgePropertyWithUniqueValuesOfThings($property, $method, $value, DateTime::class);
1104
    }
1105
1106
    /**
1107
    * @param array<int, Date|DateTime> $value
1108
    */
1109 3
    protected function NudgePropertyWithUniqueDatesOrDateTimes(
1110
        string $property,
1111
        string $method,
1112
        array $value
1113
    ) : void {
1114 3
        $this->NudgePropertyWithUniqueValuesOfThings(
1115 3
            $property,
1116 3
            $method,
1117 3
            $value,
1118 3
            Date::class,
1119 3
            DateTime::class
1120
        );
1121 3
    }
1122
1123
    /**
1124
    * @param array<int, Person> $value
1125
    */
1126 4
    protected function NudgePropertyWithUniquePersons(
1127
        string $property,
1128
        string $method,
1129
        array $value
1130
    ) : void {
1131 4
        $this->NudgePropertyWithUniqueValuesOfThings(
1132 4
            $property,
1133 4
            $method,
1134 4
            $value,
1135 4
            Person::class
1136
        );
1137 4
    }
1138
1139
    /**
1140
    * @param array<int, Organization|Person> $value
1141
    */
1142 82
    protected function NudgePropertyWithUniqueOrganizationsOrPersons(
1143
        string $property,
1144
        string $method,
1145
        array $value
1146
    ) : void {
1147 82
        $this->NudgePropertyWithUniqueValuesOfThings(
1148 82
            $property,
1149 82
            $method,
1150 82
            $value,
1151 82
            Organization::class,
1152 82
            Person::class
1153
        );
1154 82
    }
1155
1156
    /**
1157
    * @param array<int, Organization> $value
1158
    */
1159
    protected function NudgePropertyWithUniqueOrganizations(
1160
        string $property,
1161
        string $method,
1162
        array $value
1163
    ) : void {
1164
        $this->NudgePropertyWithUniqueValuesOfThings(
1165
            $property,
1166
            $method,
1167
            $value,
1168
            Organization::class
1169
        );
1170
    }
1171
1172
    /**
1173
    * @param array<int, Event> $value
1174
    */
1175 3
    protected function NudgePropertyWithUniqueEvents(
1176
        string $property,
1177
        string $method,
1178
        array $value
1179
    ) : void {
1180 3
        $this->NudgePropertyWithUniqueValuesOfThings(
1181 3
            $property,
1182 3
            $method,
1183 3
            $value,
1184 3
            Event::class
1185
        );
1186 3
    }
1187
1188
    /**
1189
    * @param array<int, Place> $value
1190
    */
1191 5
    protected function NudgePropertyValueWithUniquePlaces(
1192
        string $property,
1193
        string $method,
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1193
        /** @scrutinizer ignore-unused */ string $method,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1194
        array $value
1195
    ) : void {
1196 5
        $this->NudgePropertyWithUniqueValuesOfThings(
1197 5
            $property,
1198 5
            __METHOD__,
1199 5
            $value,
1200 5
            Place::class
1201
        );
1202 5
    }
1203
1204
    /**
1205
    * @param array<int, ContactPoint|Place> $value
1206
    */
1207
    protected function NudgePropertyValueWithUniqueContactPointsOrPlaces(
1208
        string $property,
1209
        string $method,
0 ignored issues
show
Unused Code introduced by
The parameter $method is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1209
        /** @scrutinizer ignore-unused */ string $method,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1210
        array $value
1211
    ) : void {
1212
        $this->NudgePropertyWithUniqueValuesOfThings(
1213
            $property,
1214
            __METHOD__,
1215
            $value,
1216
            ContactPoint::class,
1217
            Place::class
1218
        );
1219
    }
1220
1221
    /**
1222
    * @param array<int, Thing|DataTypes\DataType> $value
1223
    *
1224
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> $validThing
1225
    * @psalm-param class-string<Thing>|class-string<DataTypes\DataType> ...$validThings
1226
    */
1227 211
    protected function NudgePropertyWithUniqueValuesOfThings(
1228
        string $property,
1229
        string $method,
1230
        array $value,
1231
        string $validThing,
1232
        string ...$validThings
1233
    ) : void {
1234 211
        array_unshift($validThings, $validThing);
1235
1236 211
        $initialCount = count($validThings);
1237
1238 211
        if (count($validThings) !== $initialCount) {
1239
            throw new InvalidArgumentException(
1240
                'Arguments 4+ passed to ' .
1241
                __METHOD__ .
1242
                ' must be implementations of ' .
1243
                Thing::class .
1244
                '!'
1245
            );
1246
        }
1247
1248 211
        $initialCount = count($value);
1249
1250
        /**
1251
        * @var array<int, Thing|DataTypes\DataType>
1252
        */
1253 211
        $value = array_filter(
1254 211
            $value,
1255
            /**
1256
            * @param Thing|DataTypes\DataType $maybe
1257
            */
1258
            function ($maybe) use ($validThings) : bool {
1259 210
                foreach ($validThings as $thing) {
1260 210
                    if (is_a($maybe, $thing, true)) {
1261 210
                        return true;
1262
                    }
1263
                }
1264
1265
                return false;
1266 211
            }
1267
        );
1268
1269 211
        if (count($value) !== $initialCount) {
1270
            throw new InvalidArgumentException(
1271
                'Argument 1 passed to ' .
1272
                $method .
1273
                ' must be an array containing any combination of ' .
1274
                implode(', ', $validThings)
1275
            );
1276
        }
1277
1278 211
        $this->NudgePropertyWithUniqueValues($property, $method, $value);
1279 211
    }
1280
}
1281