|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Arcanedev\Agent; |
|
6
|
|
|
|
|
7
|
|
|
use Arcanedev\Agent\Contracts\Agent as AgentContract; |
|
8
|
|
|
use Arcanedev\Agent\Contracts\Detector; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Agent |
|
14
|
|
|
* |
|
15
|
|
|
* @package Arcanedev\Agent |
|
16
|
|
|
* @author ARCANEDEV <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Agent implements AgentContract |
|
19
|
|
|
{ |
|
20
|
|
|
/* ----------------------------------------------------------------- |
|
21
|
|
|
| Traits |
|
22
|
|
|
| ----------------------------------------------------------------- |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
use ForwardsCalls; |
|
26
|
|
|
|
|
27
|
|
|
/* ----------------------------------------------------------------- |
|
28
|
|
|
| Properties |
|
29
|
|
|
| ----------------------------------------------------------------- |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var \Illuminate\Http\Request |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $request; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The detectors. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $detectors = [ |
|
43
|
|
|
Detectors\DeviceDetector::class, |
|
44
|
|
|
Detectors\LanguageDetector::class, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Parsed request. |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $parsed; |
|
53
|
|
|
|
|
54
|
|
|
/* ----------------------------------------------------------------- |
|
55
|
|
|
| Getters & Setters |
|
56
|
|
|
| ----------------------------------------------------------------- |
|
57
|
|
|
*/ |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set the request instance. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \Illuminate\Http\Request $request |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
372 |
|
public function setRequest(Request $request) |
|
67
|
|
|
{ |
|
68
|
372 |
|
$this->request = $request; |
|
69
|
|
|
|
|
70
|
372 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the request instance. |
|
75
|
|
|
* |
|
76
|
|
|
* @return \Illuminate\Http\Request |
|
77
|
|
|
*/ |
|
78
|
372 |
|
public function getRequest(): Request |
|
79
|
|
|
{ |
|
80
|
372 |
|
return $this->request; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the device detector. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Arcanedev\Agent\Detectors\DeviceDetector |
|
87
|
|
|
*/ |
|
88
|
354 |
|
public function device() |
|
89
|
|
|
{ |
|
90
|
354 |
|
return $this->getParsed(Detectors\DeviceDetector::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the language detector. |
|
95
|
|
|
* |
|
96
|
|
|
* @return \Arcanedev\Agent\Detectors\LanguageDetector |
|
97
|
|
|
*/ |
|
98
|
18 |
|
public function language() |
|
99
|
|
|
{ |
|
100
|
18 |
|
return $this->getParsed(Detectors\LanguageDetector::class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $key |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Arcanedev\Agent\Contracts\Detector|mixed |
|
107
|
|
|
*/ |
|
108
|
372 |
|
protected function getParsed(string $key): Detector |
|
109
|
|
|
{ |
|
110
|
372 |
|
return $this->parsed[$key]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/* ----------------------------------------------------------------- |
|
114
|
|
|
| Main Methods |
|
115
|
|
|
| ----------------------------------------------------------------- |
|
116
|
|
|
*/ |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Parse the given request. |
|
120
|
|
|
* |
|
121
|
|
|
* @param \Illuminate\Http\Request|null $request |
|
122
|
|
|
* |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
372 |
|
public function parse(Request $request = null): AgentContract |
|
126
|
|
|
{ |
|
127
|
372 |
|
if ( ! is_null($request)) |
|
128
|
366 |
|
$this->setRequest($request); |
|
129
|
|
|
|
|
130
|
372 |
|
foreach ($this->detectors as $detector) { |
|
131
|
372 |
|
$this->parsed[$detector] = static::callDetector($detector, $this->getRequest()); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
372 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/* ----------------------------------------------------------------- |
|
138
|
|
|
| Other Methods |
|
139
|
|
|
| ----------------------------------------------------------------- |
|
140
|
|
|
*/ |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Call the detector. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $detector |
|
146
|
|
|
* @param \Illuminate\Http\Request $request |
|
147
|
|
|
* |
|
148
|
|
|
* @return mixed |
|
149
|
|
|
*/ |
|
150
|
372 |
|
protected static function callDetector(string $detector, $request): Detector |
|
151
|
|
|
{ |
|
152
|
372 |
|
return call_user_func_array([app($detector), 'handle'], [$request]); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|