Completed
Push — 4.0 ( e9991b...604d5a )
by Serhii
02:10
created

Tools   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 42
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getVersion() 0 16 2
A getVersionPattern() 0 4 1
A getWindowsVersion() 0 4 2
A getMethodName() 0 4 1
A runSetter() 0 7 2
1
<?php
2
/**
3
 * @author Serhii Nekhaienko <[email protected]>
4
 * @license GPL
5
 * @copyright Serhii Nekhaienko &copy 2018
6
 * @version 4.0.0
7
 * @project browser-detector
8
 */
9
10
namespace EndorphinStudio\Detector;
11
12
class Tools
13
{
14
    public static function getVersion(string $phrase, string $ua): string
15
    {
16
        $version = static::getVersionPattern($phrase);
17
        $uaString = str_replace(' NT', '', $ua);
18
        if (preg_match($version, $uaString)) {
19
            preg_match($version, $uaString, $v);
20
            $version = $v[0];
21
            $version = preg_replace('/' . $phrase . '/', '', $version);
22
            $version = str_replace(';', '', $version);
23
            $version = str_replace(' ', '', $version);
24
            $version = str_replace('/', '', $version);
25
            $version = str_replace('_', '.', $version);
26
27
            return $version;
28
        }
29
    }
30
31
    public static function getVersionPattern(string $phrase): string
32
    {
33
        return sprintf('/%s(\/| )[\w-._]{1,15}/', $phrase);
34
    }
35
36
    public static function getWindowsVersion(string $version, array $config)
37
    {
38
        return array_key_exists($version, $config) ? $config[$version] : $version;
39
    }
40
41
    public static function getMethodName(string $name, string $type = 'set')
42
    {
43
        return sprintf('%s%s', $type, ucfirst($name));
44
    }
45
46
    public static function runSetter(&$object, string $key, $value)
47
    {
48
        $methodName = static::getMethodName($key);
49
        if (method_exists($object, $methodName)) {
50
            $object->$methodName($value);
51
        }
52
    }
53
}