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) |
||
57 | |||
58 | /** |
||
59 | * Split Product and Version |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | private function split() |
||
71 | |||
72 | /** |
||
73 | * @throws ProductException |
||
74 | */ |
||
75 | private function blacklistCheck() |
||
91 | |||
92 | /** |
||
93 | * Validate the Product format |
||
94 | * @link https://tools.ietf.org/html/rfc7230#section-3.2.4 |
||
95 | * |
||
96 | * @return bool |
||
97 | * @throws ProductException |
||
98 | */ |
||
99 | private function validateProduct() |
||
110 | |||
111 | /** |
||
112 | * Validate the Version and it's format |
||
113 | * |
||
114 | * @return bool |
||
115 | * @throws VersionException |
||
116 | */ |
||
117 | private function validateVersion() |
||
132 | |||
133 | /** |
||
134 | * Get User-agent |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getUserAgent() |
||
142 | |||
143 | /** |
||
144 | * Get product |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getProduct() |
||
152 | |||
153 | /** |
||
154 | * Get version |
||
155 | * |
||
156 | * @return string|null |
||
157 | */ |
||
158 | public function getVersion() |
||
162 | |||
163 | /** |
||
164 | * Find the best matching User-agent |
||
165 | * |
||
166 | * @param string[] $userAgents |
||
167 | * @return string|false |
||
168 | */ |
||
169 | public function getMostSpecific(array $userAgents) |
||
184 | |||
185 | /** |
||
186 | * Get an array of all possible User-agent combinations |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getUserAgents() |
||
197 | |||
198 | /** |
||
199 | * Get versions |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | public function getVersions() |
||
221 | |||
222 | /** |
||
223 | * Explode |
||
224 | * |
||
225 | * @param string $string |
||
226 | * @param string $delimiter |
||
227 | * @return array |
||
228 | */ |
||
229 | private function explode($string, $delimiter) |
||
237 | |||
238 | /** |
||
239 | * Filter duplicates from an array |
||
240 | * |
||
241 | * @param string[] $array |
||
242 | * @return array |
||
243 | */ |
||
244 | private function filterDuplicates($array) |
||
254 | |||
255 | /** |
||
256 | * Get products |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | public function getProducts() |
||
271 | } |
||
272 |