IntegerDirective   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 101
ccs 33
cts 33
cp 1
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 3 1
B fromString() 0 19 7
A getValue() 0 3 1
A setName() 0 15 5
A setValue() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\Http\Header\Value\CacheControl;
6
7
use InvalidArgumentException;
8
use Stadly\Http\Utilities\Rfc7230;
9
use Stadly\Http\Utilities\Rfc7234;
10
11
/**
12
 * Class for handling cache control directives with integer value.
13
 *
14
 * Specification: https://tools.ietf.org/html/rfc7234#section-5.2.2
15
 */
16
final class IntegerDirective extends Directive
17
{
18
    public const RESERVED_NAMES = [
19
        'max-age',
20
        's-maxage',
21
    ];
22
23
    /**
24
     * @var string Value.
25
     */
26
    private $value;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param string $name Name.
32
     * @param string $value Value.
33
     */
34 7
    public function __construct(string $name, string $value)
35
    {
36 7
        $this->setName($name);
37 3
        $this->setValue($value);
38
    }
39
40
    /**
41
     * Construct directive from string.
42
     *
43
     * @param string $directive Directive string.
44
     * @return self Directive generated based on the string.
45
     */
46 7
    public static function fromString(string $directive): self
47
    {
48 7
        $regEx = '{^' . Rfc7234::CACHE_DIRECTIVE_CAPTURE . '$}';
49 7
        $plainDirective = mb_convert_encoding($directive, 'ISO-8859-1', 'UTF-8');
50 7
        if ($plainDirective !== $directive || preg_match($regEx, $directive, $matches) !== 1) {
51 3
            throw new InvalidArgumentException('Invalid directive: ' . $directive);
52
        }
53
54 4
        if (!isset($matches['VALUE'])) {
55 1
            throw new InvalidArgumentException('Directive must have value');
56
        }
57
58 3
        $value = $matches['VALUE'];
59
        // Strip slashes if value is quoted.
60 3
        if (mb_strlen($value) >= 2 && mb_substr($value, 0, 1) === '"' && mb_substr($value, -1) === '"') {
61 2
            $value = stripslashes(mb_substr($value, 1, -1));
62
        }
63
64 3
        return new self($matches['NAME'], $value);
65
    }
66
67
    /**
68
     * @inheritDoc
69
     */
70 1
    public function __toString(): string
71
    {
72 1
        return $this->name . '=' . $this->value;
73
    }
74
75
    /**
76
     * @param string $name Name.
77
     */
78 5
    public function setName(string $name): void
79
    {
80 5
        $plainName = mb_convert_encoding($name, 'ISO-8859-1', 'UTF-8');
81 5
        if ($plainName !== $name || preg_match('{^' . Rfc7230::TOKEN . '$}', $name) !== 1) {
82 2
            throw new InvalidArgumentException('Invalid name: ' . $name);
83
        }
84
85 5
        if (in_array(strtolower($name), FieldListDirective::RESERVED_NAMES, /*strict*/true)) {
86 1
            throw new InvalidArgumentException('Name reserved for field list directive: ' . $name);
87
        }
88 5
        if (in_array(strtolower($name), ValuelessDirective::RESERVED_NAMES, /*strict*/true)) {
89 1
            throw new InvalidArgumentException('Name reserved for valueless directive: ' . $name);
90
        }
91
92 5
        $this->name = $name;
93
    }
94
95
    /**
96
     * @return string Value.
97
     */
98 1
    public function getValue(): string
99
    {
100 1
        return $this->value;
101
    }
102
103
    /**
104
     * Set the value. It must be a non-negative integer string.
105
     *
106
     * @param string $value Value.
107
     */
108 3
    public function setValue(string $value): void
109
    {
110 3
        $regEx = '{^' . Rfc7234::DELTA_SECONDS . '$}';
111 3
        $plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8');
112 3
        if ($plainValue !== $value || preg_match($regEx, $value) !== 1) {
113 2
            throw new InvalidArgumentException('Invalid value: ' . $value);
114
        }
115
116 3
        $this->value = $value;
117
    }
118
}
119