-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathVerbalExpressions.php
557 lines (498 loc) · 12.6 KB
/
VerbalExpressions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
namespace VerbalExpressions\PHPVerbalExpressions;
/**
* Verbal Expressions v0.1 (https://github.com/jehna/VerbalExpressions) ported in PHP
* @author Mihai Ionut Vilcu (ionutvmi@gmail.com)
* 22.July.2013
*/
class VerbalExpressions
{
public $prefixes = '';
public $source = '';
public $suffixes = '';
public $modifiers = 'm'; // default to global multi line matching
public $replaceLimit = 1; // the limit of preg_replace when g modifier is not set
protected $lastAdded = false; // holds the last added regex
/**
* Sanitize
*
* Sanitation function for adding anything safely to the expression
*
* @access public
* @param string $value the to be added
* @return string escaped value
*/
public static function sanitize($value)
{
return $value ? preg_quote($value, '/') : $value;
}
/**
* Add
*
* Add stuff to the expression
*
* @access public
* @param string $value the stuff to be added
* @return VerbalExpressions
*/
public function add($value)
{
$this->source .= $this->lastAdded = $value;
return $this;
}
/**
* Start of Line
*
* Mark the expression to start at the beginning of the line.
*
* @access public
* @param boolean $enable Enables or disables the line starting. Default value: true
* @return VerbalExpressions
*/
public function startOfLine($enable = true)
{
$this->prefixes = $enable ? '^' : '';
return $this;
}
/**
* End of line
*
* Mark the expression to end at the last character of the line.
*
* @access public
* @param boolean $enable Enables or disables the line ending. Default value: true
* @return VerbalExpressions
*/
public function endOfLine($enable = true)
{
$this->suffixes = $enable ? '$' : '';
return $this;
}
/**
* Then
*
* Add a string to the expression
*
* @access public
* @param string $value The string to be looked for
* @return VerbalExpressions
*/
public function then($value)
{
return $this->add('(?:' . self::sanitize($value) . ')');
}
/**
* Find
*
* alias for then()
* @param string $value The string to be looked for
* @return VerbalExpressions
*/
public function find($value)
{
return $this->then($value);
}
/**
* Maybe
*
* Add a string to the expression that might appear once (or not).
*
* @access public
* @param string $value The string to be looked for
* @return VerbalExpressions
*/
public function maybe($value)
{
return $this->add('(?:' . self::sanitize($value) . ')?');
}
/**
* Anything
*
* Accept any string
*
* @access public
* @return VerbalExpressions
*/
public function anything()
{
return $this->add('(?:.*)');
}
/**
* AnythingBut
*
* Anything but this chars
*
* @access public
* @param string $value The unaccepted chars
* @return VerbalExpressions
*/
public function anythingBut($value)
{
return $this->add('(?:[^' . self::sanitize($value) . ']*)');
}
/**
* Something
*
* Accept any non-empty string
*
* @access public
* @return VerbalExpressions
*/
public function something()
{
return $this->add('(?:.+)');
}
/**
* Anything but
*
* Anything non-empty except for these chars
*
* @access public
* @param string $value The unaccepted chars
* @return VerbalExpressions
*/
public function somethingBut($value)
{
return $this->add('(?:[^' . self::sanitize($value) . ']+)');
}
/**
* Preg Replace
*
* Shorthand for preg_replace()
*
* @access public
* @param string $source the string that will be affected(subject)
* @param string $value the replacement
* @return VerbalExpressions
*/
public function replace($source, $value)
{
// php doesn't have g modifier so we remove it if it's there and we remove limit param
if (strpos($this->modifiers, 'g') !== false) {
$this->modifiers = str_replace('g', '', $this->modifiers);
return preg_replace($this->getRegex(), $value, $source);
}
return preg_replace($this->getRegex(), $value, $source, $this->replaceLimit);
}
/**
* Line break
*
* Match line break
*
* @access public
* @return VerbalExpressions
*/
public function lineBreak()
{
return $this->add('(?:\\n|(\\r\\n))');
}
/**
* Line break
*
* Shorthand for lineBreak
*
* @access public
* @return VerbalExpressions
*/
public function br()
{
return $this->lineBreak();
}
/**
* Tabs
*
* Match tabs.
*
* @access public
* @return VerbalExpressions
*/
public function tab()
{
return $this->add('\\t');
}
/**
* Alpha Numeric
*
* Match any alpha numeric
*
* @access public
* @return VerbalExpressions
*/
public function word()
{
return $this->add('\\w+');
}
/**
* Digit
*
* Match any digit
*
* @access public
* @return VerbalExpressions
*/
public function digit()
{
return $this->add('\\d');
}
/**
* List Chars
*
* Any of the listed chars
*
* @access public
* @param string $value The chars looked for
* @return VerbalExpressions
*/
public function anyOf($value)
{
return $this->add('[' . $value . ']');
}
/**
* Alias
*
* Shorthand for anyOf
*
* @access public
* @param string $value The chars looked for
* @return VerbalExpressions
*/
public function any($value)
{
return $this->anyOf($value);
}
/**
* Add a range
*
* Adds a range to our expression ex: range(a,z) => a-z, range(a,z,0,9) => a-z0-9
*
* @access public
* @return VerbalExpressions
* @throws \InvalidArgumentException
*/
public function range()
{
$args = func_get_args();
$arg_num = count($args);
if ($arg_num%2 != 0) {
throw new \InvalidArgumentException('Number of args must be even', 1);
}
$value = '[';
for ($i = 0; $i < $arg_num;) {
$value .= self::sanitize($args[$i++]) . '-' . self::sanitize($args[$i++]);
}
$value .= ']';
return $this->add($value);
}
/**
* Add a modifier
*
* Adds a modifier
*
* @access public
* @param string $modifier
* @return VerbalExpressions
*/
public function addModifier($modifier)
{
if (strpos($this->modifiers, $modifier) === false) {
$this->modifiers .= $modifier;
}
return $this;
}
/**
* Remove Modifier
*
* Removes a modifier
*
* @access public
* @param string $modifier
* @return VerbalExpressions
*/
public function removeModifier($modifier)
{
$this->modifiers = str_replace($modifier, '', $modifier);
return $this;
}
/**
* Case Sensitivity
*
* Match case insensitive or sensitive based on $enable value
*
* @access public
* @param boolean $enable Enables or disables case sensitive. Default true
* @return VerbalExpressions
*/
public function withAnyCase($enable = true)
{
return $enable ? $this->addModifier('i') : $this->removeModifier('i');
}
/**
* Stop At First
*
* Toggles g modifier
*
* @access public
* @param boolean $enable Enables or disables g modifier. Default true
* @return VerbalExpressions
*/
public function stopAtFirst($enable = true)
{
return $enable ? $this->addModifier('g') : $this->removeModifier('g');
}
/**
* SearchOneLine
*
* Toggles m modifier
*
* @access public
* @param boolean $enable Enables or disables m modifier. Default true
* @return VerbalExpressions
*/
public function searchOneLine($enable = true)
{
return $enable ? $this->addModifier('m') : $this->removeModifier('m');
}
/**
* Multiple
*
* Adds the multiple modifier at the end of your expression
*
* @access public
* @param string $value Your expression
* @return VerbalExpressions
*/
public function multiple($value)
{
$value = self::sanitize($value);
$last = substr($value, -1);
if ($last !== '+' && $last !== '*') {
$value .= '+';
}
return $this->add($value);
}
/**
* OR
*
* Wraps the current expression in an `or` with $value
* Notice: OR is a reserved keyword in PHP, so this method is prefixed with "_"
*
* @access public
* @param string $value new expression
* @return VerbalExpressions
*/
public function _or($value)
{
if (strpos($this->prefixes, '(') === false) {
$this->prefixes .= '(?:';
}
if (strpos($this->suffixes, ')') === false) {
$this->suffixes .= ')';
}
$this->add(')|(?:');
if ($value) {
$this->add($value);
}
return $this;
}
/**
* Object to string
*
* PHP Magic method to return a string representation of the object.
*
* @access public
* @return string
*/
public function __toString()
{
return $this->getRegex();
}
/**
* Get the Expression
*
* Creates the final regex.
*
* @access public
* @return string The final regex
*/
public function getRegex()
{
return '/' . $this->prefixes . $this->source . $this->suffixes . '/' . $this->modifiers;
}
/**
* Test
*
* tests the match of a string to the current regex
*
* @access public
* @param string $value The string to be tested
* @return boolean true if it's a match
*/
public function test($value)
{
// php doesn't have g modifier so we remove it if it's there and call preg_match_all()
if (strpos($this->modifiers, 'g') !== false) {
$this->modifiers = str_replace('g', '', $this->modifiers);
$matches = array();//because it's not optional in <5.4
return preg_match_all($this->getRegex(), $value, $matches);
}
return (bool) preg_match($this->getRegex(), $value);
}
/**
* Clean
*
* deletes the current regex for a fresh start
*
* @access public
* @param array $options
* @return VerbalExpressions
*/
public function clean(array $options = array())
{
$options = array_merge(
array(
'prefixes' => '',
'source' => '',
'suffixes' => '',
'modifiers' => 'gm',
'replaceLimit' => '1'
),
$options
);
$this->prefixes = $options['prefixes'];
$this->source = $options['source'];
$this->suffixes = $options['suffixes'];
$this->modifiers = $options['modifiers']; // default to global multi line matching
$this->replaceLimit = $options['replaceLimit']; // default to global multi line matching
return $this;
}
/**
* Limit
*
* Adds char limit to the last added expression.
* If $max is less then $min the limit will be: At least $min chars {$min,}
* If $max is 0 the limit will be: exactly $min chars {$min}
* If $max bigger then $min the limit will be: at least $min but not more then $max {$min, $max}
*
* @access public
* @param integer $min
* @param integer $max
* @return VerbalExpressions
*/
public function limit($min, $max = 0)
{
if ($max == 0) {
$value = '{' . $min . '}';
} elseif ($max < $min) {
$value = '{' . $min . ',}';
} else {
$value = '{' . $min . ',' . $max . '}';
}
// check if the expression has * or + for the last expression
if (preg_match('/\*|\+/', $this->lastAdded)) {
$l = 1;
$this->source = strrev(str_replace(array('+', '*'), strrev($value), strrev($this->source), $l));
return $this;
}
return $this->add($value);
}
}