1 | <?php |
||
16 | class UAParser extends AbstractProvider |
||
17 | { |
||
18 | /** |
||
19 | * Name of the provider |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $name = 'UAParser'; |
||
24 | |||
25 | /** |
||
26 | * Homepage of the provider |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $homepage = 'https://github.com/ua-parser/uap-php'; |
||
31 | |||
32 | /** |
||
33 | * Composer package name |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $packageName = 'ua-parser/uap-php'; |
||
38 | |||
39 | protected $detectionCapabilities = [ |
||
40 | |||
41 | 'browser' => [ |
||
42 | 'name' => true, |
||
43 | 'version' => true, |
||
44 | ], |
||
45 | |||
46 | 'renderingEngine' => [ |
||
47 | 'name' => false, |
||
48 | 'version' => false, |
||
49 | ], |
||
50 | |||
51 | 'operatingSystem' => [ |
||
52 | 'name' => true, |
||
53 | 'version' => true, |
||
54 | ], |
||
55 | |||
56 | 'device' => [ |
||
57 | 'model' => true, |
||
58 | 'brand' => true, |
||
59 | 'type' => false, |
||
60 | 'isMobile' => false, |
||
61 | 'isTouch' => false, |
||
62 | ], |
||
63 | |||
64 | 'bot' => [ |
||
65 | 'isBot' => true, |
||
66 | 'name' => true, |
||
67 | 'type' => false, |
||
68 | ], |
||
69 | ]; |
||
70 | |||
71 | protected $defaultValues = [ |
||
72 | |||
73 | 'general' => [ |
||
74 | '/^Other$/i', |
||
75 | |||
76 | ], |
||
77 | |||
78 | 'device' => [ |
||
79 | |||
80 | 'brand' => [ |
||
81 | '/^Generic/i', |
||
82 | '/^unknown$/i', |
||
83 | ], |
||
84 | |||
85 | 'model' => [ |
||
86 | '/^generic$/i', |
||
87 | '/^Smartphone$/i', |
||
88 | '/^Feature Phone$/i', |
||
89 | '/^iOS-Device$/i', |
||
90 | '/^Tablet$/i', |
||
91 | '/^Touch$/i', |
||
92 | '/^Windows$/i', |
||
93 | '/^Windows Phone$/i', |
||
94 | '/^Android$/i', |
||
95 | ], |
||
96 | ], |
||
97 | |||
98 | 'bot' => [ |
||
99 | 'name' => [ |
||
100 | '/^Other$/i', |
||
101 | '/^crawler$/i', |
||
102 | '/^robot$/i', |
||
103 | '/^crawl$/i', |
||
104 | '/^Spider$/i', |
||
105 | ], |
||
106 | ], |
||
107 | ]; |
||
108 | |||
109 | private $parser; |
||
110 | |||
111 | /** |
||
112 | * |
||
113 | * @param Parser $parser |
||
114 | * @throws PackageNotLoadedException |
||
115 | */ |
||
116 | 18 | public function __construct(Parser $parser = null) |
|
117 | { |
||
118 | 18 | if ($parser === null) { |
|
119 | 8 | $this->checkIfInstalled(); |
|
120 | } |
||
121 | |||
122 | 18 | $this->parser = $parser; |
|
123 | 18 | } |
|
124 | |||
125 | /** |
||
126 | * |
||
127 | * @return Parser |
||
128 | */ |
||
129 | 11 | public function getParser() |
|
130 | { |
||
131 | 11 | if ($this->parser !== null) { |
|
132 | 11 | return $this->parser; |
|
133 | } |
||
134 | |||
135 | 1 | $this->parser = Parser::create(); |
|
136 | |||
137 | 1 | return $this->parser; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * |
||
142 | * @param \UAParser\Result\Client $resultRaw |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 10 | private function hasResult(\UAParser\Result\Client $resultRaw) |
|
147 | { |
||
148 | 10 | if ($this->isBot($resultRaw) === true) { |
|
149 | 3 | return true; |
|
150 | } |
||
151 | |||
152 | 7 | if ($this->isRealResult($resultRaw->ua->family)) { |
|
153 | 1 | return true; |
|
154 | } |
||
155 | |||
156 | 6 | if ($this->isRealResult($resultRaw->os->family)) { |
|
157 | 2 | return true; |
|
158 | } |
||
159 | |||
160 | 4 | if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) { |
|
161 | 1 | return true; |
|
162 | } |
||
163 | |||
164 | 3 | return false; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * |
||
169 | * @param \UAParser\Result\Client $resultRaw |
||
170 | * |
||
171 | * @return bool |
||
172 | */ |
||
173 | 10 | private function isBot(\UAParser\Result\Client $resultRaw) |
|
181 | |||
182 | /** |
||
183 | * |
||
184 | * @param Model\Bot $bot |
||
185 | * @param \UAParser\Result\Client $resultRaw |
||
186 | */ |
||
187 | 3 | private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw) |
|
192 | |||
193 | /** |
||
194 | * |
||
195 | * @param Model\Browser $browser |
||
196 | * @param \UAParser\Result\UserAgent $uaRaw |
||
197 | */ |
||
198 | 4 | private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw) |
|
206 | |||
207 | /** |
||
208 | * |
||
209 | * @param Model\OperatingSystem $os |
||
210 | * @param \UAParser\Result\OperatingSystem $osRaw |
||
211 | */ |
||
212 | 4 | private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw) |
|
220 | |||
221 | /** |
||
222 | * |
||
223 | * @param Model\UserAgent $device |
||
224 | * @param \UAParser\Result\Device $deviceRaw |
||
225 | */ |
||
226 | 4 | private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw) |
|
231 | |||
232 | 10 | public function parse($userAgent, array $headers = []) |
|
271 | } |
||
272 |