Passed
Push — master ( 9013bb...125cf1 )
by Kirill
03:56
created

AcceptHeaderItem::setParams()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 9.2222
cc 6
nc 4
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Pavel Z
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Http\Header;
13
14
/**
15
 * Represents "Accept" header single item.
16
 *
17
 * Can be used for comparing each item weight or constructing the "Accept" headers.
18
 */
19
final class AcceptHeaderItem
20
{
21
    /** @var string|null */
22
    private $value;
23
24
    /** @var float */
25
    private $quality;
26
27
    /** @var array */
28
    private $params = [];
29
30
    /**
31
     * AcceptHeaderItem constructor.
32
     * @param string $mime
33
     * @param float  $quality
34
     * @param array  $params
35
     */
36
    public function __construct(string $mime, float $quality = 1.0, array $params = [])
37
    {
38
        $this->setValue($mime);
39
        $this->setQuality($quality);
40
        $this->setParams($params);
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function __toString(): string
47
    {
48
        if ($this->value === '') {
49
            return '';
50
        }
51
52
        $parts = [$this->value];
53
54
        if ($this->quality < 1) {
55
            $parts[] = "q=$this->quality";
56
        }
57
58
        foreach ($this->getParams() as $name => $value) {
59
            $parts[] = "$name=$value";
60
        }
61
62
        return implode('; ', $parts);
63
    }
64
65
    /**
66
     * Parse accept header string.
67
     *
68
     * @param string $string
69
     * @return static|null
70
     */
71
    public static function fromString(string $string): self
72
    {
73
        $elements = explode(';', $string);
74
75
        $mime = trim(array_shift($elements));
76
        $quality = 1.0;
77
        $params = [];
78
79
        foreach ($elements as $element) {
80
            $parsed = explode('=', trim($element), 2);
81
82
            // Wrong params must be ignored
83
            if (count($parsed) !== 2) {
84
                continue;
85
            }
86
87
            $name = trim($parsed[0]);
88
            $value = trim($parsed[1]);
89
90
            if (strcasecmp($name, 'q') === 0) {
91
                $quality = (float)$value;
92
            } else {
93
                $params[$name] = $value;
94
            }
95
        }
96
97
        return new static($mime, $quality, $params);
98
    }
99
100
    /**
101
     * @param string $value
102
     * @return $this
103
     */
104
    public function withValue(string $value): self
105
    {
106
        $item = clone $this;
107
        $item->setValue($value);
108
109
        return $item;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getValue(): string
116
    {
117
        return $this->value;
118
    }
119
120
    /**
121
     * @param float $quality
122
     * @return $this
123
     */
124
    public function withQuality(float $quality): self
125
    {
126
        $item = clone $this;
127
        $item->setQuality($quality);
128
129
        return $item;
130
    }
131
132
    /**
133
     * @return float
134
     */
135
    public function getQuality(): float
136
    {
137
        return $this->quality;
138
    }
139
140
    /**
141
     * @param array $params
142
     * @return $this
143
     */
144
    public function withParams(array $params): self
145
    {
146
        $item = clone $this;
147
        $item->setParams($params);
148
149
        return $item;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    public function getParams(): array
156
    {
157
        return $this->params;
158
    }
159
160
    /**
161
     * @param string $value
162
     */
163
    private function setValue(string $value): void
164
    {
165
        $this->value = trim($value);
166
    }
167
168
    /**
169
     * @param float $quality
170
     */
171
    private function setQuality(float $quality): void
172
    {
173
        $this->quality = min(max($quality, 0), 1);
174
    }
175
176
    /**
177
     * @param array $params
178
     */
179
    private function setParams(array $params): void
180
    {
181
        foreach ($params as $name => $value) {
182
            if (is_numeric($name) || !is_scalar($value)) {
183
                continue;
184
            }
185
186
            $name = trim($name);
187
            $value = trim($value);
188
189
            if ($name === '' || $value === '') {
190
                continue;
191
            }
192
193
            $this->params[$name] = $value;
194
        }
195
    }
196
}
197