1 | <?php |
||
23 | class UserAgentParser |
||
24 | { |
||
25 | /** |
||
26 | * RFC 7231 - Section 5.5.3 - User-agent |
||
27 | */ |
||
28 | const RFC_README = 'https://tools.ietf.org/html/rfc7231#section-5.5.3'; |
||
29 | |||
30 | /** |
||
31 | * PREG pattern for valid characters |
||
32 | */ |
||
33 | const PREG_PATTERN = '/[^\x21-\x7E]/'; |
||
34 | |||
35 | /** |
||
36 | * Origin product |
||
37 | * @var string |
||
38 | */ |
||
39 | private $originProduct; |
||
40 | |||
41 | /** |
||
42 | * Product |
||
43 | * @var string |
||
44 | */ |
||
45 | private $product; |
||
46 | |||
47 | /** |
||
48 | * Version |
||
49 | * @var float|int|string|null |
||
50 | */ |
||
51 | private $version; |
||
52 | |||
53 | /** |
||
54 | * UserAgentParser constructor |
||
55 | * |
||
56 | * @param string $product |
||
57 | * @param float|int|string|null $version |
||
58 | */ |
||
59 | public function __construct($product, $version = null) |
||
69 | |||
70 | /** |
||
71 | * Split Product and Version |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | private function split() |
||
83 | |||
84 | /** |
||
85 | * Validate the Product format |
||
86 | * @link https://tools.ietf.org/html/rfc7231#section-5.5.3 |
||
87 | * @link https://tools.ietf.org/html/rfc7230#section-3.2.4 |
||
88 | * |
||
89 | * @return bool |
||
90 | * @throws ProductException |
||
91 | */ |
||
92 | private function validateProduct() |
||
104 | |||
105 | /** |
||
106 | * Check for blacklisted strings or characters |
||
107 | * |
||
108 | * @param float|int|string|null $input |
||
109 | * @return bool |
||
110 | * @throws FormatException |
||
111 | */ |
||
112 | private function blacklistCheck($input) |
||
126 | |||
127 | /** |
||
128 | * Validate the Version and it's format |
||
129 | * @link https://tools.ietf.org/html/rfc7231#section-5.5.3 |
||
130 | * |
||
131 | * @return bool |
||
132 | * @throws VersionException |
||
133 | */ |
||
134 | private function validateVersion() |
||
148 | |||
149 | /** |
||
150 | * Get User-agent |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getUserAgent() |
||
160 | |||
161 | /** |
||
162 | * Get product |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getProduct() |
||
170 | |||
171 | /** |
||
172 | * Get version |
||
173 | * |
||
174 | * @return float|int|string|null |
||
175 | */ |
||
176 | public function getVersion() |
||
180 | |||
181 | /** |
||
182 | * Find the best matching User-agent |
||
183 | * |
||
184 | * @param string[] $userAgents |
||
185 | * @return string|false |
||
186 | */ |
||
187 | public function getMostSpecific(array $userAgents) |
||
202 | |||
203 | /** |
||
204 | * Get an array of all possible User-agent combinations |
||
205 | * |
||
206 | * @return string[] |
||
207 | */ |
||
208 | public function getUserAgents() |
||
215 | |||
216 | /** |
||
217 | * Get versions |
||
218 | * |
||
219 | * @return float[]|int[]|string[] |
||
220 | */ |
||
221 | public function getVersions() |
||
241 | |||
242 | /** |
||
243 | * Explode |
||
244 | * |
||
245 | * @param string $string |
||
246 | * @param string $delimiter |
||
247 | * @return string[] |
||
248 | */ |
||
249 | private function explode($string, $delimiter) |
||
257 | |||
258 | /** |
||
259 | * Filter duplicates from an array |
||
260 | * |
||
261 | * @param string[] $array |
||
262 | * @return string[] |
||
263 | */ |
||
264 | private function filterDuplicates($array) |
||
274 | |||
275 | /** |
||
276 | * Get products |
||
277 | * |
||
278 | * @return string[] |
||
279 | */ |
||
280 | public function getProducts() |
||
290 | } |
||
291 |