|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of WebHelper Parser. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) James <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WebHelper\Parser\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
use Webmozart\PathUtil\Path; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Helper class to check strings. |
|
20
|
|
|
* |
|
21
|
|
|
* @author James <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Checker |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var string a string to check */ |
|
26
|
|
|
private $string = ''; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Sets an empty string if the parameter has the wrong type. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $string a string to check |
|
32
|
|
|
*/ |
|
33
|
26 |
|
public function setString($string = '') |
|
34
|
|
|
{ |
|
35
|
|
|
try { |
|
36
|
26 |
|
Assert::string($string); |
|
37
|
26 |
|
} catch (InvalidArgumentException $e) { |
|
38
|
4 |
|
$this->string = ''; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
26 |
|
$this->string = $string; |
|
42
|
|
|
|
|
43
|
26 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Gets the string. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string the string |
|
50
|
|
|
*/ |
|
51
|
26 |
|
public function getString() |
|
52
|
|
|
{ |
|
53
|
26 |
|
return $this->string; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Confirms if a string is an existing absolute path. |
|
58
|
|
|
* |
|
59
|
|
|
* @return bool true if the string is an existing absolute path, false otherwise |
|
60
|
|
|
*/ |
|
61
|
10 |
|
public function isValidAbsolutePath() |
|
62
|
|
|
{ |
|
63
|
10 |
|
if (!Path::isAbsolute($this->string)) { |
|
64
|
1 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
9 |
|
if (!is_dir($this->string)) { |
|
68
|
1 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
8 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Confirms if a string is a valid regular expression. |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool true if the string is a valid regex, false otherwise |
|
78
|
|
|
*/ |
|
79
|
19 |
|
public function isValidRegex() |
|
80
|
|
|
{ |
|
81
|
19 |
|
if (false === @preg_match($this->string, 'tester')) { |
|
82
|
3 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
16 |
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Confirms if a valid regex string contains a key and a value named suppattern. |
|
90
|
|
|
* |
|
91
|
|
|
* A simple directive matcher MUST contain a key and a value named subpattern. |
|
92
|
|
|
* A starting block directive matcher MUST contain a key and a value named subpattern. |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool true if the string is valid, false otherwise |
|
95
|
|
|
*/ |
|
96
|
17 |
|
public function hasKeyAndValueSubPattern() |
|
97
|
|
|
{ |
|
98
|
17 |
|
if (!$this->isValidRegex()) { |
|
99
|
2 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
15 |
|
if (false === strpos($this->string, '(?<key>') || false === strpos($this->string, '(?<value>')) { |
|
103
|
6 |
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
9 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|