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