1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace EndorphinStudio\Detector\Detection; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use EndorphinStudio\Detector\Data\AbstractData; |
8
|
|
|
use EndorphinStudio\Detector\Detector; |
9
|
|
|
use EndorphinStudio\Detector\Tools; |
10
|
|
|
|
11
|
|
|
abstract class AbstractDetection implements DetectionInterface |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
protected $config; |
15
|
|
|
/** @var Detector */ |
16
|
|
|
protected $detector; |
17
|
|
|
|
18
|
|
|
/** @var AbstractData */ |
19
|
|
|
protected $resultObject; |
20
|
|
|
|
21
|
|
|
public function init(Detector $detector) |
22
|
|
|
{ |
23
|
|
|
$this->detector = $detector; |
24
|
|
|
echo static::class . PHP_EOL; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function initResultObject() |
28
|
|
|
{ |
29
|
|
|
// init default value from data |
30
|
|
|
foreach ($this->config['default'] as $defaultKey => $defaultValue) { |
31
|
|
|
Tools::runSetter($this->resultObject, $defaultKey, $defaultValue); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public abstract function detect(string $ua); |
36
|
|
|
|
37
|
|
|
protected function detectByPattern(array $patternList) |
38
|
|
|
{ |
39
|
|
|
foreach ($patternList as $patternId => $patternData) { |
40
|
|
|
$pattern = $this->getPattern($patternId, $patternData); |
41
|
|
|
|
42
|
|
|
if (preg_match($pattern['pattern'], $this->detector->getUserAgent())) { |
43
|
|
|
return ['name' => $patternId, 'version' => Tools::getVersion($pattern['version'], $this->detector->getUserAgent()), 'originalInfo' => $patternData]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function getPattern(string $patternId, array $patternData): array |
50
|
|
|
{ |
51
|
|
|
if (array_key_exists('default', $patternData) && $patternData['default'] === true) { |
52
|
|
|
return ['pattern' => sprintf('/%s/', $patternId), 'version' => $patternId]; |
53
|
|
|
} |
54
|
|
|
return ['pattern' => sprintf('/%s/', $patternData['pattern']), 'version' => array_key_exists('version', $patternData) ? $patternData['version'] : $patternId]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function setAttributes($info) |
58
|
|
|
{ |
59
|
|
|
$result = $this->detector->getResultObject(); |
60
|
|
|
if (array_key_exists('attributes', $info)) { |
61
|
|
|
foreach ($info['attributes'] as $attributeKey => $attributeValue) { |
62
|
|
|
Tools::runSetter($result, $attributeKey, $attributeValue); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
protected function detectByType(): array |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
foreach ($this->config as $type => $patternList) { |
70
|
|
|
if ($type === 'default') { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
$browser = $this->detectByPattern($patternList); |
74
|
|
|
if ($browser) { |
75
|
|
|
return array_merge($browser, ['type' => $type]); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return []; |
79
|
|
|
} |
80
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.