Passed
Pull Request — master (#27)
by Anatoly
05:58 queued 01:43
created

ContentTypeHeader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
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 63
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getFieldValue() 0 8 2
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\InvalidHeaderValueException;
18
use Sunrise\Http\Message\Exception\InvalidHeaderValueParameterException;
19
use Sunrise\Http\Message\Header;
20
21
/**
22
 * Import functions
23
 */
24
use function sprintf;
25
26
/**
27
 * @link https://tools.ietf.org/html/rfc2616#section-14.17
28
 */
29
class ContentTypeHeader extends Header
30
{
31
32
    /**
33
     * Regular Expression for a content type validation
34
     *
35
     * @link https://tools.ietf.org/html/rfc6838#section-4.2
36
     *
37
     * @var string
38
     */
39
    public const RFC6838_CONTENT_TYPE = '/^[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*(?:\/[\dA-Za-z][\d\w\!#\$&\+\-\.\^]*)?$/';
40
41
    /**
42
     * @var string
43
     */
44
    private string $type;
45
46
    /**
47
     * @var array<string, string>
48
     */
49
    private array $parameters;
50
51
    /**
52
     * Constructor of the class
53
     *
54
     * @param string $type
55
     * @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...
56
     *
57
     * @throws InvalidHeaderValueException
58
     *         If the type isn't valid.
59
     *
60
     * @throws InvalidHeaderValueParameterException
61
     *         If the parameters aren't valid.
62
     */
63 16
    public function __construct(string $type, array $parameters = [])
64
    {
65 16
        $this->validateValueByRegex(self::RFC6838_CONTENT_TYPE, $type);
66
67 14
        $parameters = $this->validateParameters($parameters);
68
69 10
        $this->type = $type;
70 10
        $this->parameters = $parameters;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 9
    public function getFieldName(): string
77
    {
78 9
        return 'Content-Type';
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 8
    public function getFieldValue(): string
85
    {
86 8
        $v = $this->type;
87 8
        foreach ($this->parameters as $name => $value) {
88 7
            $v .= sprintf('; %s="%s"', $name, $value);
89
        }
90
91 8
        return $v;
92
    }
93
}
94