1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gobie\Regex\Wrappers\Pcre; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Wrapper around PCRE library. |
7
|
|
|
* |
8
|
|
|
* It is meant to be replaceable for PCRE functions. |
9
|
|
|
* |
10
|
|
|
* Usage: |
11
|
|
|
* <code> |
12
|
|
|
* if ($matches = PcreRegex::getAll($pattern, $subject)) { |
13
|
|
|
* // do stuff here with $matches |
14
|
|
|
* } |
15
|
|
|
* </code> |
16
|
|
|
* |
17
|
|
|
* @link http://php.net/pcre |
18
|
|
|
*/ |
19
|
|
|
class PcreRegex |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Regular expression match and return if pattern matches given subject. |
24
|
|
|
* |
25
|
|
|
* @param string $pattern Pattern |
26
|
|
|
* @param string $subject Subject |
27
|
|
|
* @param int $offset Offset |
28
|
|
|
* @return bool True if pattern matches given subject, false otherwise |
29
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
30
|
|
|
* @link http://php.net/function.preg-match.php |
31
|
|
|
*/ |
32
|
15 |
View Code Duplication |
public static function match($pattern, $subject, $offset = 0) |
|
|
|
|
33
|
|
|
{ |
34
|
15 |
|
static::setUp($pattern); |
35
|
15 |
|
$res = \preg_match($pattern, $subject, $matches, 0, $offset); |
36
|
10 |
|
static::tearDown(); |
37
|
10 |
|
static::handleError($pattern); |
38
|
|
|
|
39
|
7 |
|
return (bool) $res; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Regular expression match and return first match. |
44
|
|
|
* |
45
|
|
|
* @param string $pattern Pattern |
46
|
|
|
* @param string $subject Subject |
47
|
|
|
* @param int $flags Flags |
48
|
|
|
* @param int $offset Offset |
49
|
|
|
* @return string[] Array with first match that matches given subject, empty array otherwise |
50
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
51
|
|
|
* @link http://php.net/function.preg-match.php |
52
|
|
|
*/ |
53
|
13 |
View Code Duplication |
public static function get($pattern, $subject, $flags = 0, $offset = 0) |
|
|
|
|
54
|
|
|
{ |
55
|
13 |
|
static::setUp($pattern); |
56
|
13 |
|
\preg_match($pattern, $subject, $matches, $flags, $offset); |
57
|
8 |
|
static::tearDown(); |
58
|
8 |
|
static::handleError($pattern); |
59
|
|
|
|
60
|
6 |
|
return $matches; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Global regular expression match and return all matches. |
65
|
|
|
* |
66
|
|
|
* @param string $pattern Pattern |
67
|
|
|
* @param string $subject Subject |
68
|
|
|
* @param int $flags Flags |
69
|
|
|
* @param int $offset Offset |
70
|
|
|
* @return string[][] Array of matches that match given subject, empty array otherwise |
71
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
72
|
|
|
* @link http://php.net/function.preg-match-all.php |
73
|
|
|
*/ |
74
|
15 |
View Code Duplication |
public static function getAll($pattern, $subject, $flags = \PREG_PATTERN_ORDER, $offset = 0) |
|
|
|
|
75
|
|
|
{ |
76
|
15 |
|
static::setUp($pattern); |
77
|
15 |
|
\preg_match_all($pattern, $subject, $matches, $flags, $offset); |
78
|
10 |
|
static::tearDown(); |
79
|
10 |
|
static::handleError($pattern); |
80
|
|
|
|
81
|
8 |
|
return \array_filter($matches); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Regular expression replace and return replaced. |
86
|
|
|
* |
87
|
|
|
* @param string|string[] $pattern Pattern or array of patterns |
88
|
|
|
* @param callable|string|string[] $replacement Replacement (string or callable) or array of string replacements |
89
|
|
|
* @param string|string[] $subject Subject or array of subjects |
90
|
|
|
* @param int $limit Limit of replacements |
91
|
|
|
* @return string|string[] Replaced subject or array of subjects |
92
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
93
|
|
|
* @link http://php.net/function.preg-replace.php |
94
|
|
|
* @link http://php.net/function.preg-replace-callback.php |
95
|
|
|
*/ |
96
|
31 |
|
public static function replace($pattern, $replacement, $subject, $limit = -1) |
97
|
|
|
{ |
98
|
31 |
|
static::setUp($pattern); |
99
|
|
|
|
100
|
31 |
|
if ((\is_object($replacement) || \is_array($replacement)) && \is_callable($replacement)) { |
101
|
12 |
|
foreach ((array) $pattern as $patternPart) { |
102
|
12 |
|
\preg_match($patternPart, ''); |
103
|
|
|
} |
104
|
|
|
|
105
|
7 |
|
static::tearDown(); |
106
|
|
|
|
107
|
7 |
|
$res = \preg_replace_callback($pattern, $replacement, $subject, $limit); |
108
|
|
|
|
109
|
6 |
|
if ($res === null) { |
110
|
2 |
|
static::handleError($pattern); |
111
|
|
|
} |
112
|
|
|
|
113
|
4 |
|
return $res; |
114
|
|
|
} |
115
|
|
|
|
116
|
19 |
|
$res = \preg_replace($pattern, $replacement, $subject, $limit); |
117
|
|
|
|
118
|
12 |
|
static::tearDown(); |
119
|
12 |
|
static::handleError($pattern); |
120
|
|
|
|
121
|
10 |
|
return $res; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Regular expression split and return all parts. |
126
|
|
|
* |
127
|
|
|
* @param string $pattern Pattern |
128
|
|
|
* @param string $subject Subject |
129
|
|
|
* @param int $limit Limit |
130
|
|
|
* @param int $flags Flags defaults to PREG_SPLIT_DELIM_CAPTURE |
131
|
|
|
* @return string[] Array of split parts, array with original string otherwise |
132
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
133
|
|
|
* @link http://php.net/function.preg-split.php |
134
|
|
|
*/ |
135
|
13 |
View Code Duplication |
public static function split($pattern, $subject, $limit = -1, $flags = \PREG_SPLIT_DELIM_CAPTURE) |
|
|
|
|
136
|
|
|
{ |
137
|
13 |
|
static::setUp($pattern); |
138
|
13 |
|
$res = \preg_split($pattern, $subject, $limit, $flags); |
139
|
8 |
|
static::tearDown(); |
140
|
8 |
|
static::handleError($pattern); |
141
|
|
|
|
142
|
6 |
|
return $res; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Regular expression grep and return matching items. |
147
|
|
|
* |
148
|
|
|
* @param string $pattern Pattern |
149
|
|
|
* @param string|string[] $subject Subject or array of subjects |
150
|
|
|
* @param int $flags Flags |
151
|
|
|
* @return string[] Array with items that matches given pattern, empty array otherwise |
152
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
153
|
|
|
* @link http://php.net/function.preg-grep.php |
154
|
|
|
*/ |
155
|
11 |
View Code Duplication |
public static function grep($pattern, $subject, $flags = 0) |
|
|
|
|
156
|
|
|
{ |
157
|
11 |
|
static::setUp($pattern); |
158
|
11 |
|
$res = \preg_grep($pattern, (array) $subject, $flags); |
159
|
6 |
|
static::tearDown(); |
160
|
6 |
|
static::handleError($pattern); |
161
|
|
|
|
162
|
4 |
|
return $res; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Regular expression filter and return only replaced. |
167
|
|
|
* |
168
|
|
|
* @param string|string[] $pattern Pattern or array of patterns |
169
|
|
|
* @param string|string[] $replacement Replacement or array of replacements |
170
|
|
|
* @param string|string[] $subject Subject or array of subjects |
171
|
|
|
* @param int $limit Limit of replacements |
172
|
|
|
* @return string|string[] Replaced subject or array of subjects |
173
|
|
|
* @throws PcreRegexException When compilation or runtime error occurs |
174
|
|
|
* @link http://php.net/function.preg-filter.php |
175
|
|
|
*/ |
176
|
12 |
View Code Duplication |
public static function filter($pattern, $replacement, $subject, $limit = -1) |
|
|
|
|
177
|
|
|
{ |
178
|
12 |
|
static::setUp($pattern); |
179
|
12 |
|
$res = \preg_filter($pattern, $replacement, $subject, $limit); |
180
|
5 |
|
static::tearDown(); |
181
|
5 |
|
static::handleError($pattern); |
182
|
|
|
|
183
|
3 |
|
return $res; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Prepare error handler for catching compilation errors. |
188
|
|
|
* |
189
|
|
|
* @param string|string[] $pattern Pattern or array of patterns |
190
|
|
|
*/ |
191
|
|
|
protected static function setUp($pattern) |
192
|
|
|
{ |
193
|
110 |
|
\set_error_handler(function ($errno, $errstr) use ($pattern) { |
194
|
45 |
|
\restore_error_handler(); |
195
|
45 |
|
throw new PcreRegexException($errstr, null, $pattern); |
196
|
110 |
|
}); |
197
|
110 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Clean up after setUp(). |
201
|
|
|
*/ |
202
|
66 |
|
protected static function tearDown() |
203
|
|
|
{ |
204
|
66 |
|
\restore_error_handler(); |
205
|
66 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Handle runtime errors in PCRE. |
209
|
|
|
* |
210
|
|
|
* @param string|string[] $pattern Pattern or array of patterns |
211
|
|
|
* @throws PcreRegexException When runtime error occurs |
212
|
|
|
*/ |
213
|
61 |
|
protected static function handleError($pattern) |
214
|
|
|
{ |
215
|
61 |
|
if ($error = \preg_last_error()) { |
216
|
17 |
|
throw new PcreRegexException(null, $error, $pattern); |
217
|
|
|
} |
218
|
44 |
|
} |
219
|
|
|
} |
220
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.