1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Agent; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use JsonSerializable; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Jenssegers\Agent\Agent as BaseAgent; |
9
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
|
12
|
|
|
class Agent extends BaseAgent implements ArrayAccess, Arrayable, Jsonable, JsonSerializable |
13
|
|
|
{ |
14
|
|
|
use Concerns\HasAttributes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The additional properties. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected static $addedProperties = [ |
22
|
|
|
'WeChat' => 'MicroMessenger/[VER]', |
23
|
|
|
'QQ' => 'QQ/[VER]', |
24
|
|
|
'NetType' => 'NetType/[VER]', |
25
|
|
|
'Language' => 'Language/[VER]', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Check these properties as well in the `is` method. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected static $checkPropertyKeys = [ |
34
|
|
|
'MicroMessenger', |
35
|
|
|
'WeChat', 'QQ', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
32 |
|
public function __construct(array $headers = null, $userAgent = null) |
42
|
|
|
{ |
43
|
32 |
|
parent::__construct($headers, $userAgent); |
44
|
|
|
|
45
|
32 |
|
$this->addProperties(); |
46
|
32 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add the additional properties. |
50
|
|
|
*/ |
51
|
32 |
|
protected function addProperties() |
52
|
|
|
{ |
53
|
32 |
|
if (! empty(static::$addedProperties)) { |
54
|
4 |
|
parent::$properties = array_merge( |
55
|
4 |
|
parent::$properties, |
56
|
2 |
|
static::$addedProperties |
57
|
2 |
|
); |
58
|
|
|
|
59
|
4 |
|
static::$addedProperties = []; |
60
|
2 |
|
} |
61
|
32 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
4 |
|
public function is($key, $userAgent = null, $httpHeaders = null) |
67
|
|
|
{ |
68
|
4 |
|
if (parent::is($key, $userAgent, $httpHeaders)) { |
69
|
4 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ( |
73
|
4 |
|
is_null($userAgent) && is_null($httpHeaders) |
74
|
4 |
|
&& in_array($key, static::$checkPropertyKeys) |
75
|
2 |
|
) { |
76
|
4 |
|
return $this->version($key) !== false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the version number of the given property in the User-Agent. |
84
|
|
|
* |
85
|
|
|
* @param string $propertyName |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
4 |
|
public function versionNumber($propertyName) |
89
|
|
|
{ |
90
|
4 |
|
return $this->version($propertyName, self::VERSION_TYPE_FLOAT); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the operating system name. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
8 |
|
public function os() |
99
|
|
|
{ |
100
|
8 |
|
return $this->platform() ?: null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the operating system version. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
4 |
|
public function osVersion() |
109
|
|
|
{ |
110
|
4 |
|
if ($version = $this->version($this->os())) { |
111
|
4 |
|
return str_replace('_', '.', $version); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the accept language. |
117
|
|
|
* |
118
|
|
|
* @param string|null $preferLanguage |
119
|
|
|
* @return string|bool |
120
|
|
|
*/ |
121
|
4 |
|
public function language($preferLanguage = null) |
122
|
|
|
{ |
123
|
4 |
|
$languages = $this->languages(); |
124
|
|
|
|
125
|
4 |
|
if (is_null($preferLanguage)) { |
126
|
4 |
|
return reset($languages); |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
$preferLanguage = strtolower($preferLanguage); |
130
|
4 |
|
foreach ($languages as $lang) { |
131
|
4 |
|
if (Str::startsWith($lang, $preferLanguage)) { |
132
|
4 |
|
return true; |
133
|
|
|
} |
134
|
2 |
|
} |
135
|
|
|
|
136
|
4 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
4 |
|
public function __call($name, $arguments) |
143
|
|
|
{ |
144
|
4 |
|
if (Str::startsWith($name, 'is')) { |
145
|
4 |
|
return $this->is(substr($name, 2)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return parent::__call($name, $arguments); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|