44
55class hints {
66
7- public static function parse (array $ hints ) : \stdClass {
7+ public static function parse (string & $ ua , array $ hints ) : \stdClass {
88 $ map = [
99 'sec-ch-ua-mobile ' => fn (\stdClass $ obj , string $ value ) : string => $ obj ->category = $ value === '?1 ' ? 'mobile ' : 'desktop ' ,
1010 'sec-ch-ua-platform ' => fn (\stdClass $ obj , string $ value ) : ?string => $ obj ->platform = \trim ($ value , '" ' ) ?: null ,
11- 'sec-ch-ua-platform-version ' => fn (\stdClass $ obj , string $ value ) : ?string => $ obj ->platformversion = \trim ($ value , '" ' ) ?: null ,
11+ 'sec-ch-ua-platform-version ' => function (\stdClass $ obj , string $ value ) : void {
12+ $ value = \trim ($ value , '" ' );
13+ if ($ obj ->platform === 'Windows ' ) {
14+ $ map = [
15+ '8 ' ,
16+ '10.1507 ' ,
17+ '10.1511 ' ,
18+ '10.1607 ' ,
19+ '10.1703 ' ,
20+ '10.1709 ' ,
21+ '10.1803 ' ,
22+ '10.1809 ' ,
23+ '10.1903 ' ,
24+ '10.1909 ' ,
25+ '10.2004 ' ,
26+ '10.20H2 ' ,
27+ '10.21H1 ' ,
28+ '10.21H2 '
29+ ];
30+ $ major = \intval ($ value );
31+ if (isset ($ map [$ major ])) {
32+ $ value = $ map [$ major ] ?? '11 ' ;
33+ }
34+ }
35+ $ obj ->platformversion = $ value ?: null ;
36+ },
1237 'sec-ch-ua-model ' => fn (\stdClass $ obj , string $ value ) : ?string => $ obj ->model = \trim ($ value , '" ' ) ?: null ,
13- 'sec-ch-ua-full-version-list ' => function (\stdClass $ obj , string $ value ) : void {
14- $ brands = self ::parseBrands ($ value );
15- $ item = \array_key_first ($ brands );
16- $ obj ->browserversion = $ brands [$ item ];
38+ 'sec-ch-ua-full-version-list ' => function (\stdClass $ obj , string $ value , string &$ ua ) : void {
39+ $ brands = [];
40+
41+ // process brands string
42+ foreach (\explode (', ' , $ value ) AS $ item ) {
43+ $ parts = \explode ('";v=" ' , \trim ($ item , ' " ' ));
44+
45+ // remove GREASE brands
46+ if (\strcspn ($ parts [0 ], '()-./:;=?_ ' ) === \strlen ($ parts [0 ])) {
47+ $ brands [$ parts [0 ]] = $ parts [1 ];
48+ }
49+ }
50+
51+ // remove Chromium if Google Chrome present
52+ if (isset ($ brands ['Chromium ' ], $ brands ['Google Chrome ' ])) {
53+ unset($ brands ['Chromium ' ]);
54+ }
55+
56+ // sort the values in importance order
57+ $ sort = ['Chromium ' , 'Google Chrome ' ];
58+ $ count = \count ($ sort );
59+ \uksort ($ brands , function (string $ a , string $ b ) use ($ sort , $ count ) : int {
60+ $ aval = $ bval = $ count ;
61+ foreach ($ sort AS $ key => $ item ) {
62+ if ($ a === $ item ) {
63+ $ aval = $ key ;
64+ }
65+ if ($ b === $ item ) {
66+ $ bval = $ key ;
67+ }
68+ }
69+ return $ aval - $ bval ;
70+ });
71+
72+ // add to UA string
73+ foreach ($ brands AS $ key => $ item ) {
74+ $ ua = $ key .'/ ' .$ item .' ' .$ ua ;
75+ }
1776 },
1877 'device-memory ' => fn (\stdClass $ obj , string $ value ) : int => $ obj ->ram = \intval (\floatval ($ value ) * 1024 ),
1978 'width ' => fn (\stdClass $ obj , string $ value ) : int => $ obj ->width = \intval ($ value ),
@@ -23,37 +82,9 @@ public static function parse(array $hints) : \stdClass {
2382 foreach ($ hints AS $ key => $ item ) {
2483 $ key = \strtolower ($ key );
2584 if (isset ($ map [$ key ])) {
26- $ map [$ key ]($ obj , $ item );
85+ $ map [$ key ]($ obj , $ item, $ ua );
2786 }
2887 }
2988 return $ obj ;
3089 }
31-
32- protected static function parseBrands (string $ brand ) : array {
33-
34- // parse the brands in the string
35- $ items = [];
36- foreach (\explode (', ' , $ brand ) AS $ item ) {
37- if (\mb_stripos ($ item , 'brand ' ) === false && ($ pos = \mb_strrpos ($ item , '; ' )) !== false ) {
38- $ items [\trim (\mb_substr ($ item , 0 , $ pos ), '"; ' )] = \trim (\mb_substr ($ item , $ pos + 1 ), 'v="; ' );
39- }
40- }
41-
42- // sort the values in importance order
43- $ sort = ['Chromium ' , 'Chrome ' ];
44- $ count = \count ($ sort );
45- \uksort ($ items , function (string $ a , string $ b ) use ($ sort , $ count ) : int {
46- $ aval = $ bval = $ count ;
47- foreach ($ sort AS $ key => $ item ) {
48- if (\mb_stripos ($ a , $ item )) {
49- $ aval = $ key ;
50- }
51- if (\mb_stripos ($ b , $ item )) {
52- $ bval = $ key ;
53- }
54- }
55- return $ aval - $ bval ;
56- });
57- return $ items ;
58- }
5990}
0 commit comments