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
// Help opcache.preload discover always-needed symbols
15
class_exists(AcceptHeaderItem::class);
16
 
17
/**
18
 * Represents an Accept-* header.
19
 *
20
 * An accept header is compound with a list of items,
21
 * sorted by descending quality.
22
 *
23
 * @author Jean-François Simon <contact@jfsimon.fr>
24
 */
25
class AcceptHeader
26
{
27
    /**
28
     * @var AcceptHeaderItem[]
29
     */
30
    private array $items = [];
31
 
32
    private bool $sorted = true;
33
 
34
    /**
35
     * @param AcceptHeaderItem[] $items
36
     */
37
    public function __construct(array $items)
38
    {
39
        foreach ($items as $item) {
40
            $this->add($item);
41
        }
42
    }
43
 
44
    /**
45
     * Builds an AcceptHeader instance from a string.
46
     */
47
    public static function fromString(?string $headerValue): self
48
    {
49
        $index = 0;
50
 
51
        $parts = HeaderUtils::split($headerValue ?? '', ',;=');
52
 
53
        return new self(array_map(function ($subParts) use (&$index) {
54
            $part = array_shift($subParts);
55
            $attributes = HeaderUtils::combine($subParts);
56
 
57
            $item = new AcceptHeaderItem($part[0], $attributes);
58
            $item->setIndex($index++);
59
 
60
            return $item;
61
        }, $parts));
62
    }
63
 
64
    /**
65
     * Returns header value's string representation.
66
     */
67
    public function __toString(): string
68
    {
69
        return implode(',', $this->items);
70
    }
71
 
72
    /**
73
     * Tests if header has given value.
74
     */
75
    public function has(string $value): bool
76
    {
77
        return isset($this->items[$value]);
78
    }
79
 
80
    /**
81
     * Returns given value's item, if exists.
82
     */
83
    public function get(string $value): ?AcceptHeaderItem
84
    {
85
        return $this->items[$value] ?? $this->items[explode('/', $value)[0].'/*'] ?? $this->items['*/*'] ?? $this->items['*'] ?? null;
86
    }
87
 
88
    /**
89
     * Adds an item.
90
     *
91
     * @return $this
92
     */
93
    public function add(AcceptHeaderItem $item): static
94
    {
95
        $this->items[$item->getValue()] = $item;
96
        $this->sorted = false;
97
 
98
        return $this;
99
    }
100
 
101
    /**
102
     * Returns all items.
103
     *
104
     * @return AcceptHeaderItem[]
105
     */
106
    public function all(): array
107
    {
108
        $this->sort();
109
 
110
        return $this->items;
111
    }
112
 
113
    /**
114
     * Filters items on their value using given regex.
115
     */
116
    public function filter(string $pattern): self
117
    {
118
        return new self(array_filter($this->items, function (AcceptHeaderItem $item) use ($pattern) {
119
            return preg_match($pattern, $item->getValue());
120
        }));
121
    }
122
 
123
    /**
124
     * Returns first item.
125
     */
126
    public function first(): ?AcceptHeaderItem
127
    {
128
        $this->sort();
129
 
130
        return $this->items ? reset($this->items) : null;
131
    }
132
 
133
    /**
134
     * Sorts items by descending quality.
135
     */
136
    private function sort(): void
137
    {
138
        if (!$this->sorted) {
139
            uasort($this->items, function (AcceptHeaderItem $a, AcceptHeaderItem $b) {
140
                $qA = $a->getQuality();
141
                $qB = $b->getQuality();
142
 
143
                if ($qA === $qB) {
144
                    return $a->getIndex() > $b->getIndex() ? 1 : -1;
145
                }
146
 
147
                return $qA > $qB ? -1 : 1;
148
            });
149
 
150
            $this->sorted = true;
151
        }
152
    }
153
}