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