Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ActiveHttpHeader
93.33% covered (success)
93.33%
14 / 15
75.00% covered (warning)
75.00%
3 / 4
5.01
0.00% covered (danger)
0.00%
0 / 1
 requiresActive
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getActiveData
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2namespace Apie\IanaValueObjects\HttpHeader;
3
4use Apie\Core\Lists\StringSet;
5use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
6use Apie\Core\ValueObjects\Interfaces\LimitedOptionsInterface;
7use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
8use Apie\IanaValueObjects\HasActiveFilter;
9use Apie\IanaValueObjects\StaticDataValueObject;
10
11/**
12 * All message headers (HTTP fields) registered in the IANA HTTP Field Names Registry.
13 *
14 * @see https://www.iana.org/assignments/http-fields/http-fields.xhtml
15 *
16 * Only active HTTP headers can be used (e.g. permanent or provisional, not deprecated/obsoleted).
17 */
18final class ActiveHttpHeader implements StringValueObjectInterface, LimitedOptionsInterface
19{
20    use IsHttpHeader, StaticDataValueObject {
21        IsHttpHeader::convert insteadof StaticDataValueObject;
22        validate as private validLanguage;
23    }
24    use HasActiveFilter;
25
26    protected static function requiresActive(): bool
27    {
28        return true;
29    }
30
31    protected static function getActiveData(): array
32    {
33        return array_filter(static::getData(), function (array $data) {
34            $status = $data['Status'] ?? null;
35            return $status === 'permanent';
36        });
37    }
38
39    public static function getOptions(): StringSet
40    {
41        return new StringSet(array_keys(static::getActiveData()));
42    }
43
44    public static function validate(string $input): void
45    {
46        $inputLower = strtolower($input);
47        $data = static::getData()[$inputLower] ?? null;
48        $status = $data['Status'] ?? null;
49        if ($status !== 'permanent') {
50            throw new InvalidStringForValueObjectException(
51                $input,
52                (new \ReflectionClass(static::class))
53            );
54        }
55
56        static::validLanguage($input);
57    }
58}