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

ContentSecurityPolicyHeader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
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://www.w3.org/TR/CSP3/#csp-header
28
 */
29
class ContentSecurityPolicyHeader extends Header
30
{
31
32
    /**
33
     * Regular Expression for a directive name validation
34
     *
35
     * @link https://www.w3.org/TR/CSP3/#framework-directives
36
     *
37
     * @var string
38
     */
39
    public const VALID_DIRECTIVE_NAME = '/^[0-9A-Za-z\-]+$/';
40
41
    /**
42
     * Regular Expression for a directive value validation
43
     *
44
     * @link https://www.w3.org/TR/CSP3/#framework-directives
45
     *
46
     * @var string
47
     */
48
    public const VALID_DIRECTIVE_VALUE = '/^[\x09\x20-\x2B\x2D-\x3A\x3C-\x7E]*$/';
49
50
    /**
51
     * @var array<string, string>
52
     */
53
    private array $parameters;
54
55
    /**
56
     * Constructor of the class
57
     *
58
     * @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...
59
     *
60
     * @throws InvalidHeaderValueParameterException
61
     *         If the parameters aren't valid.
62
     */
63 26
    public function __construct(array $parameters = [])
64
    {
65 26
        $parameters = $this->validateParametersByRegex(
66 26
            $parameters,
67 26
            self::VALID_DIRECTIVE_NAME,
68 26
            self::VALID_DIRECTIVE_VALUE
69 26
        );
70
71 18
        $this->parameters = $parameters;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 7
    public function getFieldName(): string
78
    {
79 7
        return 'Content-Security-Policy';
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 14
    public function getFieldValue(): string
86
    {
87 14
        $directives = [];
88 14
        foreach ($this->parameters as $directive => $value) {
89
            // the directive can be without value...
90
            // e.g. sandbox, upgrade-insecure-requests, etc.
91 12
            if ($value === '') {
92 4
                $directives[] = $directive;
93 4
                continue;
94
            }
95
96 10
            $directives[] = sprintf('%s %s', $directive, $value);
97
        }
98
99 14
        return implode('; ', $directives);
100
    }
101
}
102