Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
15 / 20
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StaticDataValueObject
75.00% covered (warning)
75.00%
15 / 20
66.67% covered (warning)
66.67%
2 / 3
9.00
0.00% covered (danger)
0.00%
0 / 1
 getData
n/a
0 / 0
n/a
0 / 0
0
 getActiveData
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 requiresActive
n/a
0 / 0
n/a
0 / 0
0
 getFieldValue
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2namespace Apie\IanaValueObjects;
3
4use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
5use Apie\Core\ValueObjects\IsStringValueObject;
6
7trait StaticDataValueObject
8{
9    use IsStringValueObject;
10
11    abstract protected static function getData(): array;
12
13    protected static function getActiveData(): array
14    {
15        return array_filter(static::getData(), function (array $data) {
16            $active = $data['Active'] ?? false;
17            $deprecated = !empty($data['Deprecated'] ?? null);
18
19            return $active && !$deprecated;
20        });
21    }
22
23    abstract protected static function requiresActive(): bool;
24
25    protected function getFieldValue(string $fieldName): mixed
26    {
27        return static::getData()[strtolower($this->internal)][$fieldName] ?? null;
28    }
29
30    public static function validate(string $input): void
31    {
32        $input = strtolower($input);
33        $data = static::getData();
34        if (!isset($data[$input])) {
35            throw new InvalidStringForValueObjectException(
36                $input,
37                (new \ReflectionClass(static::class))
38            );
39        };
40        $active = $data[$input]['Active'] ?? false;
41        $deprecated = !empty($data[$input]['Deprecated'] ?? null);
42        if (((!$active || $deprecated) && self::requiresActive())) {
43            throw new InvalidStringForValueObjectException(
44                $input,
45                (new \ReflectionClass(static::class))
46            );
47        }
48    }
49}