1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace hexydec\agentzero; |
4
|
|
|
|
5
|
|
|
class hints { |
6
|
|
|
|
7
|
|
|
public static function parse(array $hints) : \stdClass { |
8
|
|
|
$map = [ |
9
|
|
|
'sec-ch-ua-mobile' => fn (\stdClass $obj, string $value) : string => $obj->category = $value === '?1' ? 'mobile' : 'desktop', |
10
|
|
|
'sec-ch-ua-platform' => fn (\stdClass $obj, string $value) : string => $obj->platform = $value, |
11
|
|
|
// 'Sec-CH-UA' => fn (\stdClass $obj, string $value) : string => $obj->type = $value === '?1' ? 'mobile' : 'desktop', |
12
|
|
|
'sec-ch-ua-platform-version' => fn (\stdClass $obj, string $value) : string => $obj->platformversion = $value, |
13
|
|
|
'sec-ch-ua-model' => fn (\stdClass $obj, string $value) : string => $obj->model = $value ?: null, |
14
|
|
|
// 'Sec-CH-UA-Full-Version-List' => fn (\stdClass $obj, string $value) : string => $obj->type = $value === '?1' ? 'mobile' : 'desktop', |
15
|
|
|
'device-memory' => fn (\stdClass $obj, string $value) : int => $obj->ram = \intval(\floatval($value) * 1024), |
16
|
|
|
'width' => fn (\stdClass $obj, string $value) : int => $obj->width = \intval($value), |
17
|
|
|
// 'RTT' => fn (\stdClass $obj, string $value) : string => $obj->type = $value === '?1' ? 'mobile' : 'desktop', |
18
|
|
|
// 'Downlink' => fn (\stdClass $obj, string $value) : string => $obj->type = $value === '?1' ? 'mobile' : 'desktop', |
19
|
|
|
'ect' => fn (\stdClass $obj, string $value) : string => $obj->nettype = $value |
20
|
|
|
]; |
21
|
|
|
$obj = new \stdClass(); |
22
|
|
|
foreach ($hints AS $key => $item) { |
23
|
|
|
$key = \strtolower($key); |
24
|
|
|
if (isset($map[$key])) { |
25
|
|
|
$map[$key]($obj, $item); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
return $obj; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function parseBrands(string $brand) : array { |
32
|
|
|
|
33
|
|
|
// parse the brands in the string |
34
|
|
|
$items = []; |
35
|
|
|
foreach (\explode(',', $brand) AS $item) { |
36
|
|
|
if (\mb_stripos($item, 'brand') === false && ($pos = \mb_strrpos($item, ';')) !== false) { |
37
|
|
|
$items[\trim(\mb_substr($item, 0, $pos), '"; ')] = \trim(\mb_substr($item, $pos + 1), 'v="; '); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// sort the values in importance order |
42
|
|
|
$sort = ['Chromium', 'Chrome']; |
43
|
|
|
$count = \count($sort); |
44
|
|
|
\uksort($items, function (string $a, string $b) use ($sort, $count) : int { |
45
|
|
|
$aval = $bval = $count; |
46
|
|
|
foreach ($sort AS $key => $item) { |
47
|
|
|
if (\mb_stripos($a, $item)) { |
48
|
|
|
$aval = $key; |
49
|
|
|
} |
50
|
|
|
if (\mb_stripos($b, $item)) { |
51
|
|
|
$bval = $key; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return $aval - $bval; |
55
|
|
|
}); |
56
|
|
|
return $items; |
57
|
|
|
} |
58
|
|
|
} |