1 | <?php |
||
5 | class Accept extends Common |
||
6 | { |
||
7 | |||
8 | protected $headerName = 'Accept'; |
||
9 | |||
10 | |||
11 | /** |
||
12 | * @param string $headerValue |
||
13 | * @return array[] |
||
14 | */ |
||
15 | 10 | protected function extractData($headerValue) |
|
22 | |||
23 | |||
24 | /** |
||
25 | * @param array $elements |
||
26 | */ |
||
27 | 10 | private function obtainGroupedElements($elements) |
|
44 | |||
45 | |||
46 | /** |
||
47 | * @param string $item |
||
48 | * @return array |
||
49 | */ |
||
50 | 11 | private function obtainAssessedItem($item) |
|
65 | |||
66 | |||
67 | /** |
||
68 | * @param array[] $elements |
||
69 | * @return array[] |
||
70 | */ |
||
71 | 10 | private function obtainSortedQualityList($elements) |
|
82 | |||
83 | |||
84 | /** |
||
85 | * @param array[] $elements |
||
86 | * @param array $keys |
||
87 | * @return array[] |
||
88 | */ |
||
89 | 10 | private function obtainSortedElements($elements, $keys) |
|
103 | |||
104 | |||
105 | private function sortBySpecificity($list) |
||
106 | { |
||
107 | |||
108 | foreach ($list as $key => $item) { |
||
109 | $list[$key][' spec '] = $this->computeSpecificity($item); |
||
110 | } |
||
111 | |||
112 | uksort($list, function($a, $b) use ($list) { |
||
113 | if ($list[$a][' spec '] === $list[$b][' spec ']) { |
||
114 | return $a > $b ? 1 : -1; |
||
115 | } |
||
116 | |||
117 | return $list[$a][' spec '] > $list[$b][' spec '] ? -1 : 1; |
||
118 | }); |
||
119 | |||
120 | return $list; |
||
121 | } |
||
122 | |||
123 | |||
124 | private function computeSpecificity($entry) |
||
125 | { |
||
126 | list($type, $subtype) = explode('/', $entry['value'] . '/'); |
||
127 | $specificity = count($entry) - 2; |
||
128 | |||
129 | if ($type !== '*') { |
||
130 | $specificity += 1000; |
||
131 | } |
||
132 | |||
133 | if ($subtype !== '*') { |
||
134 | $specificity += 100; |
||
135 | } |
||
136 | |||
137 | return $specificity; |
||
138 | } |
||
139 | |||
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * @return bool |
||
144 | */ |
||
145 | 2 | public function contains($type) |
|
156 | |||
157 | |||
158 | /** |
||
159 | * @param array $data |
||
160 | * @param array $expected |
||
161 | * @return bool |
||
162 | */ |
||
163 | 1 | private function matchFound($data, $expected) |
|
164 | { |
||
165 | 1 | foreach ($data as $item) { |
|
166 | 1 | if ($this->isMatch($expected, $item)) { |
|
167 | 1 | return true; |
|
168 | } |
||
169 | 1 | } |
|
170 | |||
171 | 1 | return false; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $options |
||
176 | * @return null|string |
||
177 | */ |
||
178 | 13 | public function getPreferred($options) |
|
188 | |||
189 | |||
190 | /** |
||
191 | * @param array $data |
||
192 | * @param array $options |
||
193 | * @return null|string |
||
194 | */ |
||
195 | 12 | private function findFormatedEntry($data, $options) |
|
196 | { |
||
197 | 12 | foreach ($data as $item) { |
|
198 | 12 | $entry = $this->obtainEntryFromList($item, $options); |
|
199 | |||
200 | 12 | if ($entry !== null) { |
|
201 | 10 | return $this->getFormatedEntry($entry); |
|
202 | } |
||
203 | 5 | } |
|
204 | |||
205 | 2 | return null; |
|
206 | } |
||
207 | |||
208 | |||
209 | /** |
||
210 | * @param array $entry |
||
211 | * @return string |
||
212 | */ |
||
213 | 12 | public function getFormatedEntry($entry) |
|
227 | |||
228 | |||
229 | /** |
||
230 | * @param array $needle |
||
231 | * @param array[] $haystack |
||
232 | * @return null|array |
||
233 | */ |
||
234 | 12 | private function obtainEntryFromList(array $needle, $haystack) |
|
235 | { |
||
236 | 12 | foreach ($haystack as $item) { |
|
237 | 12 | if ($this->isMatch($item, $needle)) { |
|
238 | 10 | return $item; |
|
239 | } |
||
240 | 8 | } |
|
241 | |||
242 | 5 | return null; |
|
243 | } |
||
244 | |||
245 | |||
246 | /** |
||
247 | * @param string $left |
||
248 | * @param string $right |
||
249 | * @return bool |
||
250 | */ |
||
251 | 1 | private function isMatch(array $left, array $right) |
|
263 | |||
264 | |||
265 | /** |
||
266 | * @param string $target |
||
267 | * @param string pattern |
||
268 | * @return string |
||
269 | */ |
||
270 | 10 | private function replaceStars($target, $pattern) |
|
285 | } |
||
286 |