aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/bakame/http-structured-fields/src/Ietf.php
blob: a98e16e20a1cfc87159741d3ce1e7824bc6cc0fe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php

declare(strict_types=1);

namespace Bakame\Http\StructuredFields;

use DateTimeImmutable;
use DateTimeZone;

enum Ietf
{
    case Rfc8941;
    case Rfc9651;

    public function uri(): string
    {
        return match ($this) {
            self::Rfc9651 => 'https://www.rfc-editor.org/rfc/rfc9651.html',
            self::Rfc8941 => 'https://www.rfc-editor.org/rfc/rfc8941.html',
        };
    }

    public function publishedAt(): DateTimeImmutable
    {
        return new DateTimeImmutable(match ($this) {
            self::Rfc9651 => '2024-09-01',
            self::Rfc8941 => '2021-02-01',
        }, new DateTimeZone('UTC'));
    }

    public function isActive(): bool
    {
        return self::Rfc9651 === $this;
    }

    public function isObsolete(): bool
    {
        return !$this->isActive();
    }

    public function supports(mixed $value): bool
    {
        if ($value instanceof StructuredFieldProvider) {
            $value = $value->toStructuredField();
        }

        if ($value instanceof OuterList ||
            $value instanceof InnerList ||
            $value instanceof Dictionary ||
            $value instanceof Parameters ||
            $value instanceof Item
        ) {
            try {
                $value->toHttpValue($this);

                return true;
            } catch (MissingFeature) {
                return false;
            }
        }

        if (!$value instanceof Type) {
            $value = Type::tryFromVariable($value);
        }

        return match ($value) {
            null => false,
            Type::DisplayString,
            Type::Date => self::Rfc8941 !== $this,
            default => true,
        };
    }
}