ETag   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 85
ccs 20
cts 20
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setEntityTag() 0 3 1
A __toString() 0 3 1
A getEntityTag() 0 3 1
A fromValue() 0 9 3
A getValue() 0 3 1
A isValid() 0 3 1
A getName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Response;
6
7
use Stadly\Http\Exception\InvalidHeader;
8
use Stadly\Http\Header\Value\EntityTag\EntityTag;
9
use Stadly\Http\Utilities\Rfc7232;
10
11
/**
12
 * Class for handling the HTTP header field ETag.
13
 *
14
 * Specification: https://tools.ietf.org/html/rfc7232#section-2.3
15
 */
16
final class ETag implements Header
17
{
18
    /**
19
     * @var EntityTag Entity tag.
20
     */
21
    private $entityTag;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param EntityTag $entityTag Entity tag.
27
     */
28 1
    public function __construct(EntityTag $entityTag)
29
    {
30 1
        $this->setEntityTag($entityTag);
31
    }
32
33
    /**
34
     * Construct header from value.
35
     *
36
     * @param string $value Header value.
37
     * @return self Header generated based on the value.
38
     * @throws InvalidHeader If the header value is invalid.
39
     */
40 2
    public static function fromValue(string $value): self
41
    {
42 2
        $regEx = '{^' . Rfc7232::ETAG . '$}';
43 2
        $plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
44 2
        if ($plainValue !== $value || preg_match($regEx, $value) !== 1) {
45 1
            throw new InvalidHeader('Invalid header value: ' . $value);
46
        }
47
48 1
        return new self(EntityTag::fromString($value));
49
    }
50
51
    /**
52
     * @inheritDoc
53
     * @throws void The header is always valid.
54
     */
55 2
    public function __toString(): string
56
    {
57 2
        return $this->getName() . ': ' . $this->getValue();
58
    }
59
60
    /**
61
     * @return true The header is always valid.
62
     */
63 1
    public function isValid(): bool
64
    {
65 1
        return true;
66
    }
67
68
    /**
69
     * @inheritDoc
70
     */
71 1
    public function getName(): string
72
    {
73 1
        return 'ETag';
74
    }
75
76
    /**
77
     * @inheritDoc
78
     * @throws void The header is always valid.
79
     */
80 2
    public function getValue(): string
81
    {
82 2
        return (string)$this->entityTag;
83
    }
84
85
    /**
86
     * @return EntityTag Entity tag.
87
     */
88 1
    public function getEntityTag(): EntityTag
89
    {
90 1
        return $this->entityTag;
91
    }
92
93
    /**
94
     * Set entity tag.
95
     *
96
     * @param EntityTag $entityTag Entity tag.
97
     */
98 1
    public function setEntityTag(EntityTag $entityTag): void
99
    {
100 1
        $this->entityTag = $entityTag;
101
    }
102
}
103