1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EndorphinStudio\Detector\Detection; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use EndorphinStudio\Detector\Data\AbstractData; |
8
|
|
|
use EndorphinStudio\Detector\Data\Os; |
9
|
|
|
use EndorphinStudio\Detector\Tools; |
10
|
|
|
|
11
|
|
|
class OsDetector extends AbstractDetection |
12
|
|
|
{ |
13
|
|
|
public function detect(string $ua) |
14
|
|
|
{ |
15
|
|
|
$this->config = $this->detector->getPatternList($this->detector->getDataProvider()->getConfig(), 'os'); |
16
|
|
|
$this->initResultObject(); |
17
|
|
|
$this->setupResultObject(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function initResultObject() |
21
|
|
|
{ |
22
|
|
|
$result = $this->detector->getResultObject()->getOs(); |
23
|
|
|
|
24
|
|
|
// init default value from data |
25
|
|
|
foreach ($this->config['default'] as $defaultKey => $defaultValue) { |
26
|
|
|
Tools::runSetter($result, $defaultKey, $defaultValue); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function setupResultObject() |
31
|
|
|
{ |
32
|
|
|
$result = $this->detector->getResultObject()->getOs(); |
33
|
|
|
$osData = $this->detectByFamily(); |
34
|
|
|
foreach ($osData as $key => $value) { |
35
|
|
|
if ($key === 'originalInfo') { |
36
|
|
|
$this->setAttributes($value); |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
Tools::runSetter($result, $key, $value); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function setAttributes($info) |
44
|
|
|
{ |
45
|
|
|
$result = $this->detector->getResultObject(); |
46
|
|
|
if (array_key_exists('attributes', $info)) { |
47
|
|
|
foreach ($info['attributes'] as $attributeKey => $attributeValue) { |
48
|
|
|
Tools::runSetter($result, $attributeKey, $attributeValue); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function detectByFamily(): array |
54
|
|
|
{ |
55
|
|
|
foreach ($this->config as $family => $data) { |
56
|
|
|
if ($family === 'default') { |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
$os = $this->detectByDeviceType($family); |
60
|
|
|
if ($os) { |
|
|
|
|
61
|
|
|
return array_merge($os, ['family' => $family]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
return []; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function detectByDeviceType($family): array |
68
|
|
|
{ |
69
|
|
|
foreach ($this->config[$family] as $deviceType => $patternList) { |
70
|
|
|
$os = $this->detectByPattern($patternList); |
71
|
|
|
if ($os) { |
72
|
|
|
return array_merge($os, ['type' => $deviceType]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
return []; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function detectByPattern(array $deviceList) |
79
|
|
|
{ |
80
|
|
|
foreach ($deviceList as $patternId => $patternData) { |
81
|
|
|
$useDefault = false; |
82
|
|
|
$pattern = '/%s/'; |
83
|
|
|
$version = '%s'; |
84
|
|
|
if (array_key_exists('default', $patternData) && $patternData['default'] === true) { |
85
|
|
|
$useDefault = true; |
86
|
|
|
$pattern = sprintf($pattern, $patternId); |
87
|
|
|
$version = sprintf($version, $patternId); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$useDefault) { |
91
|
|
|
$pattern = sprintf($pattern, $patternData['pattern']); |
92
|
|
|
if (array_key_exists('version', $patternData)) { |
93
|
|
|
$version = sprintf($version, $patternData['version']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (preg_match($pattern, $this->detector->getUserAgent())) { |
98
|
|
|
$version = Tools::getVersion($version, $this->detector->getUserAgent()); |
99
|
|
|
return ['name' => $patternId, 'version' => $version, 'originalInfo' => $patternData]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.