1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EndorphinStudio\Detector\Detection; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use EndorphinStudio\Detector\Detector; |
8
|
|
|
use EndorphinStudio\Detector\Tools; |
9
|
|
|
|
10
|
|
|
abstract class AbstractDetection implements DetectionInterface |
11
|
|
|
{ |
12
|
|
|
/** @var array */ |
13
|
|
|
protected $config; |
14
|
|
|
/** @var Detector */ |
15
|
|
|
protected $detector; |
16
|
|
|
|
17
|
|
|
public function init(Detector $detector) |
18
|
|
|
{ |
19
|
|
|
$this->detector = $detector; |
20
|
|
|
echo static::class.PHP_EOL; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public abstract function detect(string $ua); |
|
|
|
|
24
|
|
|
|
25
|
|
|
protected function detectByPattern(array $patternList) |
26
|
|
|
{ |
27
|
|
|
foreach ($patternList as $patternId => $patternData) { |
28
|
|
|
$useDefault = false; |
29
|
|
|
$pattern = '/%s/'; |
30
|
|
|
$version = '%s'; |
31
|
|
|
if (array_key_exists('default', $patternData) && $patternData['default'] === true) { |
32
|
|
|
$useDefault = true; |
33
|
|
|
$pattern = sprintf($pattern, $patternId); |
34
|
|
|
$version = sprintf($version, $patternId); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!$useDefault) { |
38
|
|
|
$pattern = sprintf($pattern, $patternData['pattern']); |
39
|
|
|
if (array_key_exists('version', $patternData)) { |
40
|
|
|
$version = sprintf($version, $patternData['version']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (preg_match($pattern, $this->detector->getUserAgent())) { |
45
|
|
|
$version = Tools::getVersion($version, $this->detector->getUserAgent()); |
46
|
|
|
return ['name' => $patternId, 'version' => $version, 'originalInfo' => $patternData]; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setAttributes($info) |
53
|
|
|
{ |
54
|
|
|
$result = $this->detector->getResultObject(); |
55
|
|
|
if (array_key_exists('attributes', $info)) { |
56
|
|
|
foreach ($info['attributes'] as $attributeKey => $attributeValue) { |
57
|
|
|
Tools::runSetter($result, $attributeKey, $attributeValue); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |