Directive   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 58
ccs 15
cts 15
cp 1
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A fromString() 0 21 6
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\Rfc7234;
9
10
/**
11
 * Class for handling cache control directives.
12
 *
13
 * Specification: https://tools.ietf.org/html/rfc7234#section-5.2.2
14
 */
15
abstract class Directive
16
{
17
    /**
18
     * @var string Name.
19
     */
20
    protected $name;
21
22
    /**
23
     * Construct directive from string.
24
     *
25
     * @param string $directive Directive string.
26
     * @return self Directive generated based on the string.
27
     */
28 5
    public static function fromString(string $directive): self
29
    {
30 5
        $regEx = '{^' . Rfc7234::CACHE_DIRECTIVE_CAPTURE . '$}';
31 5
        $plainDirective = mb_convert_encoding($directive, 'ISO-8859-1', 'UTF-8');
32 5
        if ($plainDirective !== $directive || preg_match($regEx, $directive, $matches) !== 1) {
33 1
            throw new InvalidArgumentException('Invalid directive: ' . $directive);
34
        }
35
36 4
        $name = strtolower($matches['NAME']);
37
38 4
        if (in_array($name, FieldListDirective::RESERVED_NAMES, /*strict*/true)) {
39 1
            return FieldListDirective::fromString($directive);
40
        }
41 3
        if (in_array($name, IntegerDirective::RESERVED_NAMES, /*strict*/true)) {
42 1
            return IntegerDirective::fromString($directive);
43
        }
44 2
        if (in_array($name, ValuelessDirective::RESERVED_NAMES, /*strict*/true)) {
45 1
            return ValuelessDirective::fromString($directive);
46
        }
47
48 1
        return GeneralDirective::fromString($directive);
49
    }
50
51
    /**
52
     * @return string String representation of the directive.
53
     */
54
    abstract public function __toString(): string;
55
56
    /**
57
     * @return string Name.
58
     */
59 4
    public function getName(): string
60
    {
61 4
        return $this->name;
62
    }
63
64
    /**
65
     * @param string $name Name.
66
     */
67
    abstract public function setName(string $name): void;
68
69
    /**
70
     * @return string|null Value.
71
     */
72
    abstract public function getValue(): ?string;
73
}
74