Passed
Pull Request — master (#27)
by Anatoly
04:10
created

CacheControlHeader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 50
ccs 14
cts 14
cp 1
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFieldValue() 0 16 4
A getFieldName() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Header;
13
14
/**
15
 * Import classes
16
 */
17
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException;
18
use Sunrise\Http\Message\Header;
19
20
/**
21
 * Import functions
22
 */
23
use function implode;
24
use function sprintf;
25
26
/**
27
 * @link https://tools.ietf.org/html/rfc2616#section-14.9
28
 */
29
class CacheControlHeader extends Header
30
{
31
32
    /**
33
     * @var array<string, string>
34
     */
35
    private array $parameters;
36
37
    /**
38
     * Constructor of the class
39
     *
40
     * @param array<array-key, mixed> $parameters
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
41
     *
42
     * @throws InvalidHeaderValueParameterException
43
     *         If the parameters aren't valid.
44
     */
45 14
    public function __construct(array $parameters)
46
    {
47 14
        $parameters = $this->validateParameters($parameters);
48
49 10
        $this->parameters = $parameters;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 7
    public function getFieldName(): string
56
    {
57 7
        return 'Cache-Control';
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 8
    public function getFieldValue(): string
64
    {
65 8
        $segments = [];
66 8
        foreach ($this->parameters as $name => $value) {
67
            // the construction <foo=> isn't valid...
68 7
            if ($value === '') {
69 2
                $segments[] = $name;
70 2
                continue;
71
            }
72
73 6
            $format = $this->isToken($value) ? '%s=%s' : '%s="%s"';
74
75 6
            $segments[] = sprintf($format, $name, $value);
76
        }
77
78 8
        return implode(', ', $segments);
79
    }
80
}
81