Range   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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