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
|
28 |
|
public function setString($string = '') |
34
|
|
|
{ |
35
|
|
|
try { |
36
|
28 |
|
Assert::string($string); |
37
|
28 |
|
} catch (InvalidArgumentException $e) { |
38
|
5 |
|
$this->string = ''; |
39
|
|
|
} |
40
|
|
|
|
41
|
28 |
|
$this->string = $string; |
42
|
|
|
|
43
|
28 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets the string. |
48
|
|
|
* |
49
|
|
|
* @return string the string |
50
|
|
|
*/ |
51
|
28 |
|
public function getString() |
52
|
|
|
{ |
53
|
28 |
|
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
|
20 |
|
public function isValidRegex() |
80
|
|
|
{ |
81
|
20 |
|
set_error_handler('self::warningOnPregMatch', E_WARNING); |
82
|
|
|
|
83
|
|
|
try { |
84
|
20 |
|
preg_match($this->string, 'tester'); |
85
|
20 |
|
} catch (InvalidArgumentException $e) { |
86
|
4 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
16 |
|
restore_error_handler(); |
90
|
|
|
|
91
|
16 |
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Throws an exception instead of a warning. |
96
|
|
|
* |
97
|
|
|
* @param int $errno error code of a bad preg_match call |
98
|
|
|
* @param string $errstr error message of a bad preg_match call |
99
|
|
|
* |
100
|
|
|
* @throws InvalidArgumentException [<description>] |
101
|
|
|
*/ |
102
|
4 |
|
private function warningOnPregMatch($errno, $errstr) |
|
|
|
|
103
|
|
|
{ |
104
|
4 |
|
throw new InvalidArgumentException($errstr, $errno); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Confirms if a valid regex string contains a key and a value named suppattern. |
109
|
|
|
* |
110
|
|
|
* A simple directive matcher MUST contain a key and a value named subpattern. |
111
|
|
|
* A starting block directive matcher MUST contain a key and a value named subpattern. |
112
|
|
|
* |
113
|
|
|
* @return bool true if the string is valid, false otherwise |
114
|
|
|
*/ |
115
|
17 |
|
public function hasKeyAndValueSubPattern() |
116
|
|
|
{ |
117
|
17 |
|
if (!$this->isValidRegex()) { |
118
|
2 |
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
15 |
|
if (false === strpos($this->string, '(?<key>') || false === strpos($this->string, '(?<value>')) { |
122
|
6 |
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
9 |
|
return true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|