Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
StaticDataValueObject
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 getData
n/a
0 / 0
n/a
0 / 0
0
 getActiveData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 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        
41        $active = $data[$input]['Active'] ?? false;
42        $deprecated = !empty($data[$input]['Deprecated'] ?? null);
43        if (((!$active || $deprecated) && self::requiresActive())) {
44            throw new InvalidStringForValueObjectException(
45                $input,
46                (new \ReflectionClass(static::class))
47            );
48        }
49    }
50}