1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine User Agent |
5
|
|
|
* |
6
|
|
|
* Platine User Agent is a lightweight library for detecting |
7
|
|
|
* user browser, device, OS, CPU |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine User Agent |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file UserAgent.php |
34
|
|
|
* |
35
|
|
|
* The Platine User Agent main class |
36
|
|
|
* |
37
|
|
|
* @package Platine\UserAgent |
38
|
|
|
* @author Platine Developers Team |
39
|
|
|
* @copyright Copyright (c) 2020 |
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
41
|
|
|
* @link https://www.platine-php.com |
42
|
|
|
* @version 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
declare(strict_types=1); |
47
|
|
|
|
48
|
|
|
namespace Platine\UserAgent; |
49
|
|
|
|
50
|
|
|
use Platine\UserAgent\Detector\BrowserDetector; |
51
|
|
|
use Platine\UserAgent\Detector\CpuDetector; |
52
|
|
|
use Platine\UserAgent\Detector\DeviceDetector; |
53
|
|
|
use Platine\UserAgent\Detector\EngineDetector; |
54
|
|
|
use Platine\UserAgent\Detector\OsDetector; |
55
|
|
|
use Platine\UserAgent\Entity\Browser; |
56
|
|
|
use Platine\UserAgent\Entity\Cpu; |
57
|
|
|
use Platine\UserAgent\Entity\Device; |
58
|
|
|
use Platine\UserAgent\Entity\Engine; |
59
|
|
|
use Platine\UserAgent\Entity\Os; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @class UserAgent |
63
|
|
|
* @package Platine\UserAgent |
64
|
|
|
*/ |
65
|
|
|
class UserAgent |
66
|
|
|
{ |
67
|
|
|
/** |
68
|
|
|
* Operating System detector |
69
|
|
|
* @var OsDetector |
70
|
|
|
*/ |
71
|
|
|
protected OsDetector $osDetector; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Device detector |
75
|
|
|
* @var DeviceDetector |
76
|
|
|
*/ |
77
|
|
|
protected DeviceDetector $deviceDetector; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Browser detector |
81
|
|
|
* @var BrowserDetector |
82
|
|
|
*/ |
83
|
|
|
protected BrowserDetector $browserDetector; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Engine detector |
87
|
|
|
* @var EngineDetector |
88
|
|
|
*/ |
89
|
|
|
protected EngineDetector $engineDetector; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* CPU detector |
93
|
|
|
* @var CpuDetector |
94
|
|
|
*/ |
95
|
|
|
protected CpuDetector $cpuDetector; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Operating System entity |
99
|
|
|
* @var Os |
100
|
|
|
*/ |
101
|
|
|
protected Os $os; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Device entity |
105
|
|
|
* @var Device |
106
|
|
|
*/ |
107
|
|
|
protected Device $device; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Browser entity |
111
|
|
|
* @var Browser |
112
|
|
|
*/ |
113
|
|
|
protected Browser $browser; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Engine entity |
117
|
|
|
* @var Engine |
118
|
|
|
*/ |
119
|
|
|
protected Engine $engine; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* CPU entity |
123
|
|
|
* @var Cpu |
124
|
|
|
*/ |
125
|
|
|
protected Cpu $cpu; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The User agent string |
129
|
|
|
* @var string |
130
|
|
|
*/ |
131
|
|
|
protected string $userAgent; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create new instance |
135
|
|
|
* @param string|null $userAgent |
136
|
|
|
*/ |
137
|
|
|
public function __construct(?string $userAgent = null) |
138
|
|
|
{ |
139
|
|
|
$this->browserDetector = new BrowserDetector(); |
140
|
|
|
$this->cpuDetector = new CpuDetector(); |
141
|
|
|
$this->engineDetector = new EngineDetector(); |
142
|
|
|
$this->deviceDetector = new DeviceDetector(); |
143
|
|
|
$this->osDetector = new OsDetector(); |
144
|
|
|
|
145
|
|
|
if ($userAgent !== null) { |
146
|
|
|
$this->parse($userAgent); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Parse the user agent for the given value |
152
|
|
|
* @param string $userAgent |
153
|
|
|
* @return $this |
154
|
|
|
*/ |
155
|
|
|
public function parse(string $userAgent): self |
156
|
|
|
{ |
157
|
|
|
$this->userAgent = $userAgent; |
158
|
|
|
|
159
|
|
|
$this->browserDetector->detect($this->userAgent); |
160
|
|
|
$this->cpuDetector->detect($this->userAgent); |
161
|
|
|
$this->engineDetector->detect($this->userAgent); |
162
|
|
|
$this->deviceDetector->detect($this->userAgent); |
163
|
|
|
$this->osDetector->detect($this->userAgent); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* The Operating System entity |
170
|
|
|
* @return Os |
171
|
|
|
*/ |
172
|
|
|
public function os(): Os |
173
|
|
|
{ |
174
|
|
|
/** @var Os $os */ |
175
|
|
|
$os = $this->osDetector->entity(); |
176
|
|
|
|
177
|
|
|
return $this->os = $os; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* The device entity |
182
|
|
|
* @return Device |
183
|
|
|
*/ |
184
|
|
|
public function device(): Device |
185
|
|
|
{ |
186
|
|
|
/** @var Device $device */ |
187
|
|
|
$device = $this->deviceDetector->entity(); |
188
|
|
|
|
189
|
|
|
return $this->device = $device; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* The browser entity |
194
|
|
|
* @return Browser |
195
|
|
|
*/ |
196
|
|
|
public function browser(): Browser |
197
|
|
|
{ |
198
|
|
|
/** @var Browser $browser */ |
199
|
|
|
$browser = $this->browserDetector->entity(); |
200
|
|
|
|
201
|
|
|
return $this->browser = $browser; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The engine entity |
206
|
|
|
* @return Engine |
207
|
|
|
*/ |
208
|
|
|
public function engine(): Engine |
209
|
|
|
{ |
210
|
|
|
/** @var Engine $engine */ |
211
|
|
|
$engine = $this->engineDetector->entity(); |
212
|
|
|
|
213
|
|
|
return $this->engine = $engine; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* The CPU entity |
218
|
|
|
* @return Cpu |
219
|
|
|
*/ |
220
|
|
|
public function cpu(): Cpu |
221
|
|
|
{ |
222
|
|
|
/** @var Cpu $cpu */ |
223
|
|
|
$cpu = $this->cpuDetector->entity(); |
224
|
|
|
|
225
|
|
|
return $this->cpu = $cpu; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|