Skip to content

Commit 0472a76

Browse files
committed
Options feature improved, group and nonCapturingGroup methods added to BuilderPattern
1 parent 063c8f2 commit 0472a76

File tree

7 files changed

+73
-9
lines changed

7 files changed

+73
-9
lines changed

searchTest/search.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
===========================================================
2222
| ROWS | Found in (count) | F & Decoded | F & D as whole |
2323
===========================================================
24-
| 1000 | 7.6 ms (890) | 44.8 ms | 11.5 ms |
25-
| 2500 | 17.25ms (2186) | 108 ms | 30 ms |
26-
| 5000 | 34.4 ms (4347) | 219 ms | 62 ms |
27-
| 10K | 67.4 ms (8669) | 413 ms | 112 ms |
28-
| 20K | 131 ms (17313) | 823 ms | 251 ms |
24+
| 1000 | 7.6 ms (890) | 44.8 ms | 11.5 ms | 5 Mb
25+
| 2500 | 17.25ms (2186) | 108 ms | 30 ms | 14 Mb
26+
| 5000 | 34.4 ms (4347) | 219 ms | 62 ms | 29 Mb
27+
| 10K | 67.4 ms (8669) | 413 ms | 112 ms | 58 Mb
28+
| 20K | 131 ms (17313) | 823 ms | 251 ms | 116 Mb
2929
===========================================================
3030
===Keyword:="green"========================================
3131
*/

src/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function processConfigArray(array $options) {
6767

6868
protected function processCallback(callable $callback) {
6969
$optionsBuilder = new OptionsBuilder();
70-
$optionsBuilder = $callback($optionsBuilder);
70+
$callback($optionsBuilder);
7171
$this->processConfigArray($optionsBuilder->getOptions());
7272
}
7373

src/Patterns/BasePattern.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected static function processArguments(array $args, array $values): array {
186186

187187
protected static function processCallback(callable $callback): array {
188188
$optionsBuilder = new OptionsBuilder();
189-
$optionsBuilder = $callback($optionsBuilder);
189+
$callback($optionsBuilder);
190190
return $optionsBuilder->getOptions();
191191
}
192192

src/Patterns/BuilderPattern.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,19 @@ private function getLengthOption($length = null, $minLength = 0, $maxLength = 0)
6363

6464
return "+"; // Default case, one or more times
6565
}
66+
67+
public function group(callable $callback): self {
68+
$subPattern = new self($this->builder);
69+
$callback($subPattern);
70+
$this->pattern .= '(' . $subPattern->getPattern() . ')';
71+
return $this;
72+
}
73+
74+
public function nonCapturingGroup(callable $callback): self {
75+
$subPattern = new self($this->builder);
76+
$callback($subPattern);
77+
$this->pattern .= '(?:' . $subPattern->getPattern() . ')';
78+
return $this;
79+
}
6680

6781
}

tests/Feature/Patterns/CurrencyPatternTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
$builder = new Builder("$1000, €200, £30");
5252

5353
$check = $builder->currency(function($query) {
54-
return $query->specificCurrencies(['']);
54+
$query->specificCurrencies(['']); // You can use options without return
55+
// return $query->specificCurrencies(['¥']); // Or with return, both has the same effect
5556
})->checkString();
5657

5758
// Assert that currencies not in the specific symbols are not matched

tests/Feature/Patterns/EmailPatternTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
$builder = new Builder("[email protected]");
4949

5050
$check = $builder->email(function($query) {
51-
return $query->onlyExtensions(['com', 'org']);
51+
$query->onlyExtensions(['com', 'org']);
5252
})->check();
5353

5454
// Assert that the email with an unlisted extension is not validated

tests/Unit/Patterns/BuilderPatternTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,52 @@
392392
expect(preg_match($regex, ' '))->toBe(0); // Only one invisible character
393393
expect(preg_match($regex, ' '))->toBe(0); // More than 2 invisible characters
394394
});
395+
396+
397+
test('group method creates capturing groups correctly', function () {
398+
$builder = new BuilderPattern();
399+
$builder->group(function($group) {
400+
$group->digits(3); // Matches exactly 3 digits
401+
})->character("-")->digits(2); // Followed by a hyphen and 2 digits
402+
403+
$regex = $builder->getMatchesValidationPattern();
404+
expect(preg_match($regex, '123-45'))->toBe(1);
405+
expect(preg_match($regex, '12345'))->toBe(0); // No hyphen
406+
expect(preg_match($regex, '12-34'))->toBe(0); // First group doesn't have 3 digits
407+
});
408+
409+
test('nonCapturingGroup method creates non-capturing groups correctly', function () {
410+
$builder = new BuilderPattern();
411+
$builder->nonCapturingGroup(function($group) {
412+
$group->digits(3); // Matches exactly 3 digits
413+
})->character("-")->digits(2); // Followed by a hyphen and 2 digits
414+
415+
$regex = $builder->getMatchesValidationPattern();
416+
expect(preg_match($regex, '123-45'))->toBe(1);
417+
expect(preg_match($regex, '12345'))->toBe(0); // No hyphen
418+
expect(preg_match($regex, '12-34'))->toBe(0); // First group doesn't have 3 digits
419+
});
420+
421+
test('capturing group captures content correctly', function () {
422+
$builder = new BuilderPattern();
423+
$builder->group(function($group) {
424+
$group->exact('abc');
425+
});
426+
427+
$regex = $builder->getMatchesValidationPattern();
428+
$matches = [];
429+
preg_match($regex, 'abcdef', $matches);
430+
expect($matches[1])->toBe('abc'); // Check the captured content
431+
});
432+
433+
test('non-capturing group does not capture content', function () {
434+
$builder = new BuilderPattern();
435+
$builder->nonCapturingGroup(function($group) {
436+
$group->exact('abc');
437+
});
438+
439+
$regex = $builder->getInputValidationPattern();
440+
$matches = [];
441+
preg_match($regex, 'abcdef', $matches);
442+
expect(isset($matches[1]))->toBeFalse(); // No captured content
443+
});

0 commit comments

Comments
 (0)