Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\HttpFoundation;
13
 
14
/**
15
 * Represents an Accept-* header item.
16
 *
17
 * @author Jean-François Simon <contact@jfsimon.fr>
18
 */
19
class AcceptHeaderItem
20
{
21
    private string $value;
22
    private float $quality = 1.0;
23
    private int $index = 0;
24
    private array $attributes = [];
25
 
26
    public function __construct(string $value, array $attributes = [])
27
    {
28
        $this->value = $value;
29
        foreach ($attributes as $name => $value) {
30
            $this->setAttribute($name, $value);
31
        }
32
    }
33
 
34
    /**
35
     * Builds an AcceptHeaderInstance instance from a string.
36
     */
37
    public static function fromString(?string $itemValue): self
38
    {
39
        $parts = HeaderUtils::split($itemValue ?? '', ';=');
40
 
41
        $part = array_shift($parts);
42
        $attributes = HeaderUtils::combine($parts);
43
 
44
        return new self($part[0], $attributes);
45
    }
46
 
47
    /**
48
     * Returns header value's string representation.
49
     */
50
    public function __toString(): string
51
    {
52
        $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
53
        if (\count($this->attributes) > 0) {
54
            $string .= '; '.HeaderUtils::toString($this->attributes, ';');
55
        }
56
 
57
        return $string;
58
    }
59
 
60
    /**
61
     * Set the item value.
62
     *
63
     * @return $this
64
     */
65
    public function setValue(string $value): static
66
    {
67
        $this->value = $value;
68
 
69
        return $this;
70
    }
71
 
72
    /**
73
     * Returns the item value.
74
     */
75
    public function getValue(): string
76
    {
77
        return $this->value;
78
    }
79
 
80
    /**
81
     * Set the item quality.
82
     *
83
     * @return $this
84
     */
85
    public function setQuality(float $quality): static
86
    {
87
        $this->quality = $quality;
88
 
89
        return $this;
90
    }
91
 
92
    /**
93
     * Returns the item quality.
94
     */
95
    public function getQuality(): float
96
    {
97
        return $this->quality;
98
    }
99
 
100
    /**
101
     * Set the item index.
102
     *
103
     * @return $this
104
     */
105
    public function setIndex(int $index): static
106
    {
107
        $this->index = $index;
108
 
109
        return $this;
110
    }
111
 
112
    /**
113
     * Returns the item index.
114
     */
115
    public function getIndex(): int
116
    {
117
        return $this->index;
118
    }
119
 
120
    /**
121
     * Tests if an attribute exists.
122
     */
123
    public function hasAttribute(string $name): bool
124
    {
125
        return isset($this->attributes[$name]);
126
    }
127
 
128
    /**
129
     * Returns an attribute by its name.
130
     */
131
    public function getAttribute(string $name, mixed $default = null): mixed
132
    {
133
        return $this->attributes[$name] ?? $default;
134
    }
135
 
136
    /**
137
     * Returns all attributes.
138
     */
139
    public function getAttributes(): array
140
    {
141
        return $this->attributes;
142
    }
143
 
144
    /**
145
     * Set an attribute.
146
     *
147
     * @return $this
148
     */
149
    public function setAttribute(string $name, string $value): static
150
    {
151
        if ('q' === $name) {
152
            $this->quality = (float) $value;
153
        } else {
154
            $this->attributes[$name] = $value;
155
        }
156
 
157
        return $this;
158
    }
159
}