Completed
Push — 4.0 ( adbbd2...a3e626 )
by Serhii
02:06
created

Tools::getVersionPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
        return "not available";
30
    }
31
32
    public static function getVersionPattern(string $phrase): string
33
    {
34
        return sprintf('/%s(\/| )[\w-._]{1,15}/', $phrase);
35
    }
36
37
    public static function getWindowsVersion(string $version, array $config)
38
    {
39
        return array_key_exists($version, $config) ? $config[$version] : $version;
40
    }
41
42
    public static function getMethodName(string $name, string $type = 'set')
43
    {
44
        return sprintf('%s%s', $type, ucfirst($name));
45
    }
46
47
    public static function runSetter(&$object, string $key, $value)
48
    {
49
        $methodName = static::getMethodName($key);
50
        if (method_exists($object, $methodName)) {
51
            $object->$methodName($value);
52
        }
53
    }
54
55
    public static function runGetter(&$object, string $key)
56
    {
57
        $methodName = static::getMethodName($key, 'get');
58
        if (method_exists($object, $methodName)) {
59
            return $object->$methodName();
60
        }
61
        return null;
62
    }
63
64
    public static function resolvePath(array &$files, $path)
65
    {
66
        if (is_array($path)) {
67
            $files = \array_merge($files, $path);
68
        } else {
69
            $files[] = $path;
70
        }
71
        return \array_unique($files);
72
    }
73
}