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() |
||
150 | |||
151 | /** |
||
152 | * Get User-agent |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getUserAgent() |
||
162 | |||
163 | /** |
||
164 | * Get product |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getProduct() |
||
172 | |||
173 | /** |
||
174 | * Get version |
||
175 | * |
||
176 | * @return float|int|string|null |
||
177 | */ |
||
178 | public function getVersion() |
||
182 | |||
183 | /** |
||
184 | * Find the best matching User-agent |
||
185 | * |
||
186 | * @param string[] $userAgents |
||
187 | * @return string|false |
||
188 | */ |
||
189 | public function getMostSpecific(array $userAgents) |
||
204 | |||
205 | /** |
||
206 | * Get an array of all possible User-agent combinations |
||
207 | * |
||
208 | * @return string[] |
||
209 | */ |
||
210 | public function getUserAgents() |
||
217 | |||
218 | /** |
||
219 | * Get versions |
||
220 | * |
||
221 | * @return float[]|int[]|string[] |
||
222 | */ |
||
223 | public function getVersions() |
||
243 | |||
244 | /** |
||
245 | * Explode |
||
246 | * |
||
247 | * @param string $string |
||
248 | * @param string $delimiter |
||
249 | * @return string[] |
||
250 | */ |
||
251 | private function explode($string, $delimiter) |
||
259 | |||
260 | /** |
||
261 | * Filter duplicates from an array |
||
262 | * |
||
263 | * @param string[] $array |
||
264 | * @return string[] |
||
265 | */ |
||
266 | private function filterDuplicates($array) |
||
276 | |||
277 | /** |
||
278 | * Get products |
||
279 | * |
||
280 | * @return string[] |
||
281 | */ |
||
282 | public function getProducts() |
||
292 | } |
||
293 |