Completed
Push — 4.0 ( 9f3f64...adbbd2 )
by Serhii
01:58
created

Tools::resolvePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 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
54
    public static function resolvePath(array &$files, $path)
55
    {
56
        if(is_array($path)) {
57
            $files = \array_merge($files, $path);
58
        } else {
59
            $files[] = $path;
60
        }
61
        return \array_unique($files);
62
    }
63
}