|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Serhii Nekhaienko <[email protected]> |
|
4
|
|
|
* @license GPL |
|
5
|
|
|
* @copyright Serhii Nekhaienko © 2018 |
|
6
|
|
|
* @version 4.0.0 |
|
7
|
|
|
* @project browser-detector |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace EndorphinStudio\Detector; |
|
11
|
|
|
|
|
12
|
|
|
class Tools |
|
13
|
|
|
{ |
|
14
|
|
|
private static $windowsConfig; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param mixed $windowsConfig |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function setWindowsConfig($windowsConfig) |
|
20
|
|
|
{ |
|
21
|
|
|
self::$windowsConfig = $windowsConfig; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function getVersion(string $phrase, string $ua): string |
|
25
|
|
|
{ |
|
26
|
|
|
$version = static::getVersionPattern($phrase); |
|
27
|
|
|
$uaString = str_replace(' NT', '', $ua); |
|
28
|
|
|
if (preg_match($version, $uaString)) { |
|
29
|
|
|
preg_match($version, $uaString, $v); |
|
30
|
|
|
$version = $v[0]; |
|
31
|
|
|
$version = preg_replace('/' . $phrase . '/', '', $version); |
|
32
|
|
|
$version = str_replace(';', '', $version); |
|
33
|
|
|
$version = str_replace(' ', '', $version); |
|
34
|
|
|
$version = str_replace('/', '', $version); |
|
35
|
|
|
$version = str_replace('_', '.', $version); |
|
36
|
|
|
|
|
37
|
|
|
if(preg_match('/ Windows /', $uaString)) { |
|
38
|
|
|
$version = static::getWindowsVersion($version); |
|
39
|
|
|
} |
|
40
|
|
|
return $version; |
|
41
|
|
|
} |
|
42
|
|
|
return "not available"; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getVersionPattern(string $phrase): string |
|
46
|
|
|
{ |
|
47
|
|
|
return sprintf('/%s(\/| )[\w-._]{1,15}/', $phrase); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public static function getWindowsVersion(string $version) |
|
51
|
|
|
{ |
|
52
|
|
|
$config = static::$windowsConfig; |
|
|
|
|
|
|
53
|
|
|
return \array_key_exists($version, $config) ? $config[$version] : $version; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function getMethodName(string $name, string $type = 'set') |
|
57
|
|
|
{ |
|
58
|
|
|
return sprintf('%s%s', $type, ucfirst($name)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function runSetter(&$object, string $key, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
$methodName = static::getMethodName($key); |
|
64
|
|
|
if (method_exists($object, $methodName)) { |
|
65
|
|
|
$object->$methodName($value); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function runGetter(&$object, string $key) |
|
70
|
|
|
{ |
|
71
|
|
|
$methodName = static::getMethodName($key, 'get'); |
|
72
|
|
|
if (method_exists($object, $methodName)) { |
|
73
|
|
|
return $object->$methodName(); |
|
74
|
|
|
} |
|
75
|
|
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static function resolvePath(array &$files, $path) |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_array($path)) { |
|
81
|
|
|
$files = \array_merge($files, $path); |
|
82
|
|
|
} else { |
|
83
|
|
|
$files[] = $path; |
|
84
|
|
|
} |
|
85
|
|
|
return \array_unique($files); |
|
86
|
|
|
} |
|
87
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: