1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) 2008 Dejan Spasic <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @package Util |
9
|
|
|
* @subpackage UserAgentParser |
10
|
|
|
* @author Dejan Spasic <[email protected]> |
11
|
|
|
* @version GIT: $Id:$ |
12
|
|
|
*/ |
13
|
|
|
namespace Fam\Util\UserAgentParser; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Base class for web clients |
17
|
|
|
* |
18
|
|
|
* @package Util |
19
|
|
|
* @subpackage UserAgentParser |
20
|
|
|
* @author Dejan Spasic <[email protected]> |
21
|
|
|
* @version @@PACKAGE_VERSION@@ |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractWebClient implements WebClient |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $version = ""; |
29
|
|
|
|
30
|
16 |
|
public function match(string $userAgent): bool |
31
|
|
|
{ |
32
|
16 |
|
foreach ($this->getPatterns() as $currentPattern) { |
33
|
15 |
|
$matches = array(); |
34
|
15 |
|
if (preg_match($currentPattern, $userAgent, $matches)) { |
35
|
10 |
|
$this->version = $matches[1]; |
36
|
|
|
|
37
|
15 |
|
return true; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
abstract protected function getPatterns(): array; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string|WebClient $webClient |
48
|
|
|
* |
49
|
|
|
* @return boolean |
50
|
|
|
* |
51
|
|
|
* @throws \InvalidArgumentException |
52
|
|
|
*/ |
53
|
3 |
|
public function isNameEquals($webClient): bool |
54
|
|
|
{ |
55
|
3 |
|
if ($webClient instanceof WebClient) { |
56
|
1 |
|
$name = $webClient->getName(); |
57
|
2 |
|
} elseif (is_string($webClient)) { |
58
|
1 |
|
$name = $webClient; |
59
|
|
|
} else { |
60
|
1 |
|
throw new \InvalidArgumentException( |
61
|
1 |
|
'Invalid argument given. Excpected argument are string or ' . WebClient::class |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return strtoupper($this->getName()) === strtoupper($name); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
public function getVersion(): string |
69
|
|
|
{ |
70
|
5 |
|
return $this->version; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function isVersionEquals(string $version): bool |
74
|
|
|
{ |
75
|
2 |
|
return $this->getVersion() === $version; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $lowerVersion |
80
|
|
|
* @param string $greaterVersion |
81
|
|
|
* |
82
|
|
|
* @return boolean |
83
|
|
|
*/ |
84
|
2 |
|
public function isVersionBetween(string $lowerVersion, string $greaterVersion): bool |
85
|
|
|
{ |
86
|
2 |
|
return version_compare($lowerVersion, $this->getVersion(), "<=") |
87
|
2 |
|
&& version_compare($greaterVersion, $this->getVersion(), ">="); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|