1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\Agent\Detectors; |
6
|
|
|
|
7
|
|
|
use Arcanedev\Agent\Contracts\Detector; |
8
|
|
|
use DeviceDetector\DeviceDetector as BaseDetector; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class DeviceDetector |
13
|
|
|
* |
14
|
|
|
* @author ARCANEDEV <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @mixin \DeviceDetector\DeviceDetector |
17
|
|
|
*/ |
18
|
|
|
class DeviceDetector implements Detector |
19
|
|
|
{ |
20
|
|
|
/* ----------------------------------------------------------------- |
21
|
|
|
| Properties |
22
|
|
|
| ----------------------------------------------------------------- |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** @var \DeviceDetector\DeviceDetector */ |
26
|
|
|
protected $detector; |
27
|
|
|
|
28
|
|
|
/* ----------------------------------------------------------------- |
29
|
|
|
| Main Methods |
30
|
|
|
| ----------------------------------------------------------------- |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Handle the given request. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Http\Request $request |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
372 |
|
public function handle(Request $request): Detector |
41
|
|
|
{ |
42
|
372 |
|
return $this->fromUserAgent( |
43
|
372 |
|
$request->server('HTTP_USER_AGENT') |
|
|
|
|
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Handle from the given user agent. |
49
|
|
|
* |
50
|
|
|
* @param string $userAgent |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
372 |
|
public function fromUserAgent(string $userAgent): Detector |
55
|
|
|
{ |
56
|
372 |
|
$this->detector = tap(new BaseDetector($userAgent), function (BaseDetector $detector) { |
57
|
372 |
|
$detector->parse(); |
58
|
372 |
|
}); |
59
|
|
|
|
60
|
372 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/* ----------------------------------------------------------------- |
64
|
|
|
| Accessors |
65
|
|
|
| ----------------------------------------------------------------- |
66
|
|
|
*/ |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the OS's name. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
42 |
|
public function osName(): string |
74
|
|
|
{ |
75
|
42 |
|
return $this->getOs('name'); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the OS's short name. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
42 |
|
public function osShortName(): string |
84
|
|
|
{ |
85
|
42 |
|
return $this->getOs('short_name'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the OS's version. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
36 |
|
public function osVersion(): string |
94
|
|
|
{ |
95
|
36 |
|
return $this->getOs('version'); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the client name. |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
72 |
|
public function clientName() |
104
|
|
|
{ |
105
|
72 |
|
return $this->getClient('name'); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the client name. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
72 |
|
public function clientShortName() |
114
|
|
|
{ |
115
|
72 |
|
return $this->getClient('short_name'); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the client's version. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
66 |
|
public function clientVersion(): string |
124
|
|
|
{ |
125
|
66 |
|
return $this->getClient('version'); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the bot's name. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
30 |
|
public function botName(): string |
134
|
|
|
{ |
135
|
30 |
|
return $this->getBot()['name'] ?? BaseDetector::UNKNOWN; |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check if it's a visitor. |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
30 |
|
public function isVisitor(): bool |
144
|
|
|
{ |
145
|
30 |
|
return ! $this->isBot(); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/* ----------------------------------------------------------------- |
149
|
|
|
| Check Methods |
150
|
|
|
| ----------------------------------------------------------------- |
151
|
|
|
*/ |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check the given name matches client's name. |
155
|
|
|
* |
156
|
|
|
* @param string $name |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
72 |
|
public function isClientName(string $name): bool |
161
|
|
|
{ |
162
|
72 |
|
return in_array($name, [ |
163
|
72 |
|
$this->clientName(), |
164
|
72 |
|
$this->clientShortName(), |
165
|
|
|
]); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Check the given name matches Operating system's name. |
170
|
|
|
* |
171
|
|
|
* @param string $name |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
42 |
|
public function isOsName(string $name): bool |
176
|
|
|
{ |
177
|
42 |
|
return in_array($name, [ |
178
|
42 |
|
$this->osName(), |
179
|
42 |
|
$this->osShortName(), |
180
|
|
|
]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* ----------------------------------------------------------------- |
184
|
|
|
| Other Methods |
185
|
|
|
| ----------------------------------------------------------------- |
186
|
|
|
*/ |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param string $name |
190
|
|
|
* @param array $params |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
354 |
|
public function __call(string $name, array $params) |
195
|
|
|
{ |
196
|
354 |
|
return call_user_func_array([$this->detector, $name], $params); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.