TypeParameter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 6
rs 10
c 1
b 0
f 1
ccs 5
cts 5
cp 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace FileEye\MimeMap;
4
5
/**
6
 * Class for working with MIME type parameters.
7
 */
8
class TypeParameter
9
{
10
    /**
11
     * Parameter name.
12
     *
13
     * @var string
14
     */
15
    protected $name;
16
17
    /**
18
     * Parameter value.
19
     *
20
     * @var string
21
     */
22
    protected $value;
23
24
    /**
25
     * Parameter comment.
26
     *
27
     * @var string
28
     */
29
    protected $comment;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param string $name    Parameter name.
35
     * @param string $value   Parameter value.
36
     * @param string $comment Comment for this parameter.
37
     */
38 27
    public function __construct(string $name, string $value, string $comment = null)
39
    {
40 27
        $this->name = $name;
41 27
        $this->value = $value;
42 27
        if ($comment !== null) {
43 13
            $this->comment = $comment;
44
        }
45
    }
46
47
    /**
48
     * Gets the parameter name.
49
     */
50 24
    public function getName(): string
51
    {
52 24
        return $this->name;
53
    }
54
55
    /**
56
     * Gets the parameter value.
57
     */
58 24
    public function getValue(): string
59
    {
60 24
        return $this->value;
61
    }
62
63
    /**
64
     * Does this parameter have a comment?
65
     */
66 26
    public function hasComment(): bool
67
    {
68 26
        return (bool) $this->comment;
69
    }
70
71
    /**
72
     * Gets the parameter comment.
73
     *
74
     * @throws UndefinedException
75
     */
76 13
    public function getComment(): string
77
    {
78 13
        if ($this->hasComment()) {
79 13
            return $this->comment;
80
        }
81
        throw new UndefinedException('Parameter comment is not defined');
82
    }
83
84
    /**
85
     * Gets a string representation of this parameter.
86
     *
87
     * @param int $format The format of the output string.
88
     */
89 24
    public function toString(int $format = Type::FULL_TEXT): string
90
    {
91 24
        $val = $this->name . '="' . str_replace('"', '\\"', $this->value) . '"';
92 24
        if ($format > Type::FULL_TEXT && $this->hasComment()) {
93 12
            $val .= ' (' . $this->getComment() . ')';
94
        }
95 24
        return $val;
96
    }
97
}
98