1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Utility; |
4: | |
5: | use Salient\Utility\Exception\PcreErrorException; |
6: | use Stringable; |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | final class Regex extends AbstractUtility |
15: | { |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | public const INVISIBLE_CHAR = '[\x{00A0}\x{00AD}\x{034F}\x{061C}\x{115F}\x{1160}\x{1680}\x{17B4}\x{17B5}\x{180B}-\x{180F}\x{2000}-\x{200F}\x{202A}-\x{202F}\x{205F}-\x{206F}\x{3000}\x{3164}\x{FE00}-\x{FE0F}\x{FEFF}\x{FFA0}\x{FFF0}-\x{FFF8}\x{1BCA0}-\x{1BCA3}\x{1D173}-\x{1D17A}\x{E0000}-\x{E0FFF}]'; |
23: | |
24: | |
25: | |
26: | |
27: | public const BOOLEAN_STRING = <<<'REGEX' |
28: | (?xi) |
29: | \s*+ (?: |
30: | (?<true> 1 | on | y(?:es)? | true | enabled? ) | |
31: | (?<false> 0 | off | no? | false | disabled? ) |
32: | ) \s*+ |
33: | REGEX; |
34: | |
35: | |
36: | |
37: | |
38: | public const INTEGER_STRING = '\s*+[+-]?[0-9]+\s*+'; |
39: | |
40: | |
41: | |
42: | |
43: | public const HTTP_TOKEN = '(?i)[-0-9a-z!#$%&\'*+.^_`|~]++'; |
44: | |
45: | |
46: | |
47: | |
48: | public const UUID = '(?i)[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}'; |
49: | |
50: | |
51: | |
52: | |
53: | public const MONGODB_OBJECTID = '(?i)[0-9a-f]{24}'; |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | |
60: | |
61: | public const PHP_IDENTIFIER = '[[:alpha:]_\x80-\xff][[:alnum:]_\x80-\xff]*'; |
62: | |
63: | |
64: | |
65: | |
66: | public const PHP_TYPE = '(?:\\\\?' . self::PHP_IDENTIFIER . ')+'; |
67: | |
68: | |
69: | |
70: | |
71: | public const PHP_UNION_TYPE = self::PHP_TYPE . '(?:\|' . self::PHP_TYPE . ')+'; |
72: | |
73: | |
74: | |
75: | |
76: | public const PHP_INTERSECTION_TYPE = self::PHP_TYPE . '(?:&' . self::PHP_TYPE . ')+'; |
77: | |
78: | |
79: | |
80: | |
81: | |
82: | |
83: | public const PHP_DNF_SEGMENT = '(?:' . self::PHP_TYPE . '|\(' . self::PHP_INTERSECTION_TYPE . '\))'; |
84: | |
85: | |
86: | |
87: | |
88: | |
89: | |
90: | public const PHP_DNF_TYPE = self::PHP_DNF_SEGMENT . '(?:\|' . self::PHP_DNF_SEGMENT . ')+'; |
91: | |
92: | |
93: | |
94: | |
95: | public const PHP_FULL_TYPE = self::PHP_DNF_SEGMENT . '(?:\|' . self::PHP_DNF_SEGMENT . ')*'; |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | public static function grep( |
108: | string $pattern, |
109: | array $array, |
110: | int $flags = 0 |
111: | ): array { |
112: | $result = preg_grep($pattern, $array, $flags); |
113: | $error = preg_last_error(); |
114: | if ($result === false || $error !== \PREG_NO_ERROR) { |
115: | throw new PcreErrorException($error, 'preg_grep', $pattern, $array); |
116: | } |
117: | return $result; |
118: | } |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | |
137: | |
138: | |
139: | public static function match( |
140: | string $pattern, |
141: | string $subject, |
142: | ?array &$matches = null, |
143: | int $flags = 0, |
144: | int $offset = 0 |
145: | ): int { |
146: | $result = preg_match($pattern, $subject, $matches, $flags, $offset); |
147: | if ($result === false) { |
148: | throw new PcreErrorException(null, 'preg_match', $pattern, $subject); |
149: | } |
150: | return $result; |
151: | } |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | |
167: | |
168: | |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | |
179: | |
180: | |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | public static function matchAll( |
188: | string $pattern, |
189: | string $subject, |
190: | ?array &$matches = null, |
191: | int $flags = 0, |
192: | int $offset = 0 |
193: | ): int { |
194: | $result = preg_match_all($pattern, $subject, $matches, $flags, $offset); |
195: | if ($result === false) { |
196: | throw new PcreErrorException(null, 'preg_match_all', $pattern, $subject); |
197: | } |
198: | return $result; |
199: | } |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | |
211: | public static function replace( |
212: | $pattern, |
213: | $replacement, |
214: | $subject, |
215: | int $limit = -1, |
216: | ?int &$count = null |
217: | ) { |
218: | $result = preg_replace($pattern, $replacement, $subject, $limit, $count); |
219: | if ($result === null) { |
220: | throw new PcreErrorException(null, 'preg_replace', $pattern, $subject); |
221: | } |
222: | return $result; |
223: | } |
224: | |
225: | |
226: | |
227: | |
228: | |
229: | |
230: | |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | |
242: | |
243: | |
244: | |
245: | |
246: | |
247: | |
248: | public static function replaceCallback( |
249: | $pattern, |
250: | callable $callback, |
251: | $subject, |
252: | int $limit = -1, |
253: | ?int &$count = null, |
254: | int $flags = 0 |
255: | ) { |
256: | $result = preg_replace_callback($pattern, $callback, $subject, $limit, $count, $flags); |
257: | if ($result === null) { |
258: | throw new PcreErrorException(null, 'preg_replace_callback', $pattern, $subject); |
259: | } |
260: | return $result; |
261: | } |
262: | |
263: | |
264: | |
265: | |
266: | |
267: | |
268: | |
269: | |
270: | |
271: | |
272: | |
273: | |
274: | |
275: | |
276: | |
277: | |
278: | |
279: | |
280: | |
281: | |
282: | |
283: | |
284: | |
285: | public static function replaceCallbackArray( |
286: | array $pattern, |
287: | $subject, |
288: | int $limit = -1, |
289: | ?int &$count = null, |
290: | int $flags = 0 |
291: | ) { |
292: | $result = preg_replace_callback_array($pattern, $subject, $limit, $count, $flags); |
293: | if ($result === null) { |
294: | throw new PcreErrorException(null, 'preg_replace_callback_array', $pattern, $subject); |
295: | } |
296: | return $result; |
297: | } |
298: | |
299: | |
300: | |
301: | |
302: | |
303: | |
304: | |
305: | public static function split( |
306: | string $pattern, |
307: | string $subject, |
308: | int $limit = -1, |
309: | int $flags = 0 |
310: | ): array { |
311: | $result = preg_split($pattern, $subject, $limit, $flags); |
312: | if ($result === false) { |
313: | throw new PcreErrorException(null, 'preg_split', $pattern, $subject); |
314: | } |
315: | return $result; |
316: | } |
317: | |
318: | |
319: | |
320: | |
321: | public static function delimit(string $pattern, string $delimiter = '/'): string |
322: | { |
323: | return sprintf( |
324: | '%s%s%s', |
325: | $delimiter, |
326: | str_replace($delimiter, '\\' . $delimiter, $pattern), |
327: | $delimiter, |
328: | ); |
329: | } |
330: | |
331: | |
332: | |
333: | |
334: | |
335: | |
336: | |
337: | public static function quoteCharacterClass( |
338: | string $characters, |
339: | ?string $delimiter = null |
340: | ): string { |
341: | $orDelimiter = $delimiter === null || $delimiter === '' |
342: | ? '' |
343: | : '|' . preg_quote($delimiter, '/'); |
344: | |
345: | |
346: | |
347: | return self::replace("/(?:[]^\\\\-]$orDelimiter)/", '\\\\$0', $characters); |
348: | } |
349: | |
350: | |
351: | |
352: | |
353: | public static function quoteReplacement(string $replacement): string |
354: | { |
355: | return self::replace('/[$\\\\]/', '\\\\$0', $replacement); |
356: | } |
357: | } |
358: | |