endorphin-studio /
browser-detector-tools
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @author Serhii Nekhaienko <[email protected]> |
||
| 4 | * @license GPL |
||
| 5 | * @copyright Serhii Nekhaienko © 2020 |
||
| 6 | * @version 1.1 |
||
| 7 | * @project endorphin-studio/browser-detector-tools |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace EndorphinStudio\Detector; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Helper class |
||
| 14 | * Class Tools |
||
| 15 | * @package EndorphinStudio\Detector |
||
| 16 | */ |
||
| 17 | class Tools |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array List of windows version |
||
| 21 | */ |
||
| 22 | private static $windowsConfig; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param array $windowsConfig |
||
| 26 | */ |
||
| 27 | public static function setWindowsConfig(array $windowsConfig) |
||
| 28 | { |
||
| 29 | self::$windowsConfig = $windowsConfig; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Return version from UA string |
||
| 34 | * @param string $phrase |
||
| 35 | * @param string $ua |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | public static function getVersion(string $phrase, string $ua): string |
||
| 39 | { |
||
| 40 | $version = static::getVersionPattern($phrase); |
||
| 41 | $uaString = str_replace(' NT', '', $ua); |
||
| 42 | if (preg_match($version, $uaString)) { |
||
| 43 | preg_match($version, $uaString, $v); |
||
| 44 | $version = $v[0]; |
||
| 45 | $version = preg_replace('/' . $phrase . '/', '', $version); |
||
| 46 | $version = str_replace(';', '', $version); |
||
| 47 | $version = str_replace(' ', '', $version); |
||
| 48 | $version = str_replace('/', '', $version); |
||
| 49 | $version = str_replace('_', '.', $version); |
||
| 50 | |||
| 51 | if(preg_match('/ Windows /', $uaString)) { |
||
| 52 | $version = static::getWindowsVersion($version); |
||
| 53 | } |
||
| 54 | return $version; |
||
| 55 | } |
||
| 56 | return "not available"; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Return version pattern |
||
| 61 | * @param string $phrase |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public static function getVersionPattern(string $phrase): string |
||
| 65 | { |
||
| 66 | return str_replace('-', '\-', sprintf('/%s(\/| )[\w-._]{1,15}/', $phrase)); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Return windows version |
||
| 71 | * @param string $version |
||
| 72 | * @return mixed|string |
||
| 73 | */ |
||
| 74 | public static function getWindowsVersion(string $version) |
||
| 75 | { |
||
| 76 | $config = static::$windowsConfig; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 77 | return \array_key_exists($version, $config) ? $config[$version] : $version; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Return method name for getter or setter |
||
| 82 | * @param string $name |
||
| 83 | * @param string $type |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public static function getMethodName(string $name, string $type = 'set') |
||
| 87 | { |
||
| 88 | return sprintf('%s%s', $type, ucfirst($name)); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Execute setter method |
||
| 93 | * @param $object |
||
| 94 | * @param string $key |
||
| 95 | * @param $value |
||
| 96 | */ |
||
| 97 | public static function runSetter(&$object, string $key, $value) |
||
| 98 | { |
||
| 99 | $methodName = static::getMethodName($key); |
||
| 100 | if (method_exists($object, $methodName)) { |
||
| 101 | $object->$methodName($value); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Execute getter |
||
| 107 | * @param $object |
||
| 108 | * @param string $key |
||
| 109 | * @return null |
||
| 110 | */ |
||
| 111 | public static function runGetter(&$object, string $key) |
||
| 112 | { |
||
| 113 | $methodName = static::getMethodName($key, 'get'); |
||
| 114 | if (method_exists($object, $methodName)) { |
||
| 115 | return $object->$methodName(); |
||
| 116 | } |
||
| 117 | return null; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Add path to list |
||
| 122 | * @param array $files |
||
| 123 | * @param $path |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public static function resolvePath(array &$files, $path) |
||
| 127 | { |
||
| 128 | if (is_array($path)) { |
||
| 129 | $files = \array_merge($files, $path); |
||
| 130 | } else { |
||
| 131 | $files[] = $path; |
||
| 132 | } |
||
| 133 | return \array_unique($files); |
||
| 134 | } |
||
| 135 | } |