1 | <?php |
||
16 | class UserAgentParser |
||
17 | { |
||
18 | /** |
||
19 | * RFC 7231 - Section 5.5.3 - User-agent |
||
20 | */ |
||
21 | const RFC_README = 'https://tools.ietf.org/html/rfc7231#section-5.5.3'; |
||
22 | |||
23 | /** |
||
24 | * PREG pattern for valid characters |
||
25 | */ |
||
26 | const PREG_PATTERN = '/[^\x21-\x7E]/'; |
||
27 | |||
28 | /** |
||
29 | * Product |
||
30 | * @var string |
||
31 | */ |
||
32 | private $product; |
||
33 | |||
34 | /** |
||
35 | * Version |
||
36 | * @var int|string|null |
||
37 | */ |
||
38 | private $version; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @param string $product |
||
44 | * @param int|string|null $version |
||
45 | */ |
||
46 | public function __construct($product, $version = null) |
||
56 | |||
57 | /** |
||
58 | * Split Product and Version |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | private function split() |
||
70 | |||
71 | /** |
||
72 | * Validate the Product format |
||
73 | * @link https://tools.ietf.org/html/rfc7230#section-3.2.4 |
||
74 | * |
||
75 | * @return bool |
||
76 | * @throws ProductException |
||
77 | */ |
||
78 | private function validateProduct() |
||
90 | |||
91 | /** |
||
92 | * Check for blacklisted strings or characters |
||
93 | * |
||
94 | * @param int|string|null $input |
||
95 | * @throws FormatException |
||
96 | */ |
||
97 | private function blacklistCheck($input) |
||
113 | |||
114 | /** |
||
115 | * Validate the Version and it's format |
||
116 | * |
||
117 | * @return bool |
||
118 | * @throws VersionException |
||
119 | */ |
||
120 | private function validateVersion() |
||
135 | |||
136 | /** |
||
137 | * Get User-agent |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getUserAgent() |
||
145 | |||
146 | /** |
||
147 | * Get product |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getProduct() |
||
155 | |||
156 | /** |
||
157 | * Get version |
||
158 | * |
||
159 | * @return string|null |
||
160 | */ |
||
161 | public function getVersion() |
||
165 | |||
166 | /** |
||
167 | * Find the best matching User-agent |
||
168 | * |
||
169 | * @param string[] $userAgents |
||
170 | * @return string|false |
||
171 | */ |
||
172 | public function getMostSpecific(array $userAgents) |
||
187 | |||
188 | /** |
||
189 | * Get an array of all possible User-agent combinations |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | public function getUserAgents() |
||
200 | |||
201 | /** |
||
202 | * Get versions |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | public function getVersions() |
||
225 | |||
226 | /** |
||
227 | * Explode |
||
228 | * |
||
229 | * @param string $string |
||
230 | * @param string $delimiter |
||
231 | * @return array |
||
232 | */ |
||
233 | private function explode($string, $delimiter) |
||
241 | |||
242 | /** |
||
243 | * Filter duplicates from an array |
||
244 | * |
||
245 | * @param string[] $array |
||
246 | * @return array |
||
247 | */ |
||
248 | private function filterDuplicates($array) |
||
258 | |||
259 | /** |
||
260 | * Get products |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | public function getProducts() |
||
275 | } |
||
276 |