Completed
Push — 4.0 ( 604d5a...33061c )
by Serhii
02:27
created

AbstractDetection::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
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
}