1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Serhii Nekhaienko <[email protected]> |
4
|
|
|
* @license GPL |
5
|
|
|
* @copyright Serhii Nekhaienko © 2020 |
6
|
|
|
* @version 2.0.0 |
7
|
|
|
* @project endorphin-studio/browser-detector-detection |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector\Detection; |
11
|
|
|
|
12
|
|
|
use EndorphinStudio\Detector\Data\AbstractData; |
13
|
|
|
use EndorphinStudio\Detector\Detector; |
14
|
|
|
use EndorphinStudio\Detector\Tools; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractDetection |
18
|
|
|
* Abstract detector class |
19
|
|
|
* @package EndorphinStudio\Detector\Detection |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractDetection implements DetectionInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array Additional Info |
25
|
|
|
*/ |
26
|
|
|
protected $additionalInfo = []; |
27
|
|
|
/** |
28
|
|
|
* @var string Key in config (os, device, etc.) |
29
|
|
|
*/ |
30
|
|
|
protected $configKey = 'none'; |
31
|
|
|
|
32
|
|
|
/** @var array Data for detection (patterns, etc.) */ |
33
|
|
|
protected $config; |
34
|
|
|
/** @var Detector Detector class */ |
35
|
|
|
protected $detector; |
36
|
|
|
|
37
|
|
|
/** @var AbstractData Result object */ |
38
|
|
|
protected $resultObject; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Detector $detector Init detection class with detector |
42
|
|
|
*/ |
43
|
|
|
public function init(Detector $detector) |
44
|
|
|
{ |
45
|
|
|
$this->detector = $detector; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Result object initialisation |
50
|
|
|
* @return null |
51
|
|
|
*/ |
52
|
|
|
protected function initResultObject() |
53
|
|
|
{ |
54
|
|
|
if(!array_key_exists('default', $this->config)) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
// init default value from data |
58
|
|
|
foreach ($this->config['default'] as $defaultKey => $defaultValue) { |
59
|
|
|
Tools::runSetter($this->resultObject, $defaultKey, $defaultValue); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Detect method |
65
|
|
|
* @param array $additional Additional info |
66
|
|
|
*/ |
67
|
|
|
public function detect(array $additional = []) |
68
|
|
|
{ |
69
|
|
|
if ($this->configKey !== 'none') { |
70
|
|
|
$this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), $this->configKey); |
71
|
|
|
$resultObject = $this->detector->getResultObject(); |
72
|
|
|
$this->resultObject = Tools::runGetter($resultObject, $this->configKey); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
$this->initResultObject(); |
75
|
|
|
$this->setupResultObject(); |
76
|
|
|
$this->afterDetection(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Setup Result Object |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
|
|
protected abstract function setupResultObject(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Detect by pattern |
87
|
|
|
* @param array $patternList list of patterns |
88
|
|
|
* @return array|null |
89
|
|
|
*/ |
90
|
|
|
protected function detectByPattern(array $patternList) |
91
|
|
|
{ |
92
|
|
|
foreach ($patternList as $patternId => $patternData) { |
93
|
|
|
$pattern = $this->getPattern($patternId, $patternData); |
94
|
|
|
|
95
|
|
|
if (preg_match($pattern['pattern'], $this->detector->getUserAgent())) { |
96
|
|
|
return ['name' => $patternId, 'version' => Tools::getVersion($pattern['version'], $this->detector->getUserAgent()), 'originalInfo' => $patternData]; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get pattern form array |
104
|
|
|
* @param string $patternId ID in config |
105
|
|
|
* @param array $patternData array from yaml file |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
private function getPattern(string $patternId, array $patternData): array |
109
|
|
|
{ |
110
|
|
|
if (array_key_exists('default', $patternData) && $patternData['default'] === true) { |
111
|
|
|
return ['pattern' => sprintf('/%s/', $patternId), 'version' => $patternId]; |
112
|
|
|
} |
113
|
|
|
return ['pattern' => sprintf('/%s/', $patternData['pattern']), 'version' => array_key_exists('version', $patternData) ? $patternData['version'] : $patternId]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set attributes of result object |
118
|
|
|
* @param array $info Array |
119
|
|
|
*/ |
120
|
|
|
protected function setAttributes(array $info) |
121
|
|
|
{ |
122
|
|
|
$result = $this->detector->getResultObject(); |
123
|
|
|
if (array_key_exists('attributes', $info)) { |
124
|
|
|
foreach ($info['attributes'] as $attributeKey => $attributeValue) { |
125
|
|
|
Tools::runSetter($result, $attributeKey, $attributeValue); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Detect by type |
132
|
|
|
* @param string $key |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function detectByType($key = 'none'): array |
136
|
|
|
{ |
137
|
|
|
$container = $key === 'none' ? $this->config : $this->config[$key]; |
138
|
|
|
foreach ($container as $type => $patternList) { |
139
|
|
|
if ($type === 'default') { |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
$browser = $this->detectByPattern($patternList); |
143
|
|
|
if (!empty($browser)) { |
144
|
|
|
return array_merge($browser, ['type' => $type]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return []; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Detect by Key |
152
|
|
|
* @param string $keyName |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
protected function detectByKey($keyName = 'family'): array |
156
|
|
|
{ |
157
|
|
|
foreach ($this->config as $key => $data) { |
158
|
|
|
if ($key === 'default') { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
$detectedData = $this->detectByType($key); |
162
|
|
|
if (!empty($detectedData)) { |
163
|
|
|
return array_merge($detectedData, [$keyName => $key]); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return []; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function afterDetection() { |
170
|
|
|
/** Not required implementation */ |
171
|
|
|
} |
172
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.