Skip to content

Commit a5dd4aa

Browse files
committed
Added 'action' methods in BuilderPattern similar to Builder class and created Dockblocks
1 parent 4b13837 commit a5dd4aa

File tree

10 files changed

+363
-77
lines changed

10 files changed

+363
-77
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ Examples: `->dot("zeroOrMore")` `->exact("hello worls", false, "1+")`
6161

6262
- Add benchmarks and tests for search against large data ✔️
6363
- Add Feature Tests for BuilderPattern ✔️
64-
- Add Dockblocs and comments for new methods
64+
- Remove need for "end" method in BuilderPattern ✔️
65+
- Add Dockblocs and comments for new methods ✔️
6566

6667
- Add facade for Laravel
6768
- Wrap Builder in class for static start
69+
- "string" and "source" for builder start
70+
- "start" and "pattern" for builderPattern start
6871
- Write documentation (add credit for https://regexr.com/ and ChatGPT)
6972

7073
- Implement string resolver pattern to use strings like "text(2)-digits()" (or "text:2-digits", or "text|2-digits") as pattern
@@ -74,3 +77,4 @@ Examples: `->dot("zeroOrMore")` `->exact("hello worls", false, "1+")`
7477
- Make options controllable from config or provider (?)
7578
- Make pattern controllable from config or provider (?)
7679
- I should be able to make new pattern using BuilderPattern
80+
- I should be able to make add custom pattern to the existing one using BuilderPattern

src/Builder.php

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,24 @@ class Builder implements BuilderContract {
2828

2929
use BuilderPatternMethods;
3030

31+
/**
32+
* The string to be processed with regex.
33+
*/
3134
protected string $str;
35+
36+
/**
37+
* The current pattern being applied.
38+
*/
3239
protected PatternContract $pattern;
40+
41+
/**
42+
* Manages options for the current pattern.
43+
*/
3344
protected OptionsManager $manager;
3445

46+
/**
47+
* List of available patterns.
48+
*/
3549
public array $patterns =
3650
[
3751
TextOrNumbersPattern::class,
@@ -52,39 +66,69 @@ class Builder implements BuilderContract {
5266
FilePathWinPattern::class,
5367
];
5468

69+
/**
70+
* Constructs a new Builder instance.
71+
*
72+
* @param string $str The initial string to process with regex.
73+
*/
5574
public function __construct(string $str = "") {
5675
$this->str = $str;
5776
$this->manager = new OptionsManager();
5877
// @todo add patterns via config
5978
}
6079

80+
/**
81+
* Processes an options array and applies it to the current pattern.
82+
*
83+
* @param array $options An associative array of options.
84+
*/
6185
protected function processConfigArray(array $options) {
6286
// Build options in options manager
6387
$this->manager->buildOptions($options);
6488
// Set used options to pattern
6589
$this->pattern->setOptions($this->manager->getUsedOptions());
6690
}
6791

92+
/**
93+
* Processes a callback function to configure options.
94+
*
95+
* @param callable $callback A function that configures options using an OptionsBuilder.
96+
*/
6897
protected function processCallback(callable $callback) {
6998
$optionsBuilder = new OptionsBuilder();
7099
$callback($optionsBuilder);
71100
$this->processConfigArray($optionsBuilder->getOptions());
72101
}
73102

103+
/**
104+
* Validates the current string as an exact match against the current pattern.
105+
*
106+
* @return bool True if the string is an exact match, false otherwise.
107+
*/
74108
protected function validateAsInput(): bool {
75109
if (!$this->pattern->validateInput($this->str)) {
76110
return false;
77111
}
78112
return true;
79113
}
80114

115+
/**
116+
* Validates if the current string contains any matches for the current pattern.
117+
*
118+
* @return bool True if the string contains matches, false otherwise.
119+
*/
81120
protected function validateAsString(): bool {
82121
if (!$this->pattern->validateMatches($this->str)) {
83122
return false;
84123
}
85124
return true;
86125
}
87126

127+
/**
128+
* Retrieves all matches of the current pattern within the string.
129+
*
130+
* @return array|null An array of matches or null if no matches are found.
131+
*/
88132
protected function getAllMatches(): ?array {
89133
return $this->pattern->getMatches($this->str);
90134
}
@@ -157,11 +201,23 @@ public function setOptions(array|callable $config): void {
157201
}
158202
}
159203

204+
/**
205+
* Registers a single pattern in the Builder.
206+
*
207+
* @param PatternContract $pattern The pattern to register.
208+
* @return self The Builder instance, for method chaining.
209+
*/
160210
public function registerPattern(PatternContract $pattern): self {
161211
$this->patterns[] = $pattern;
162212
return $this;
163213
}
164214

215+
/**
216+
* Registers multiple patterns in the Builder.
217+
*
218+
* @param array $patterns An array of patterns to register.
219+
* @return self The Builder instance, for method chaining.
220+
*/
165221
public function registerPatterns(array $patterns): self {
166222
foreach ($patterns as $pattern) {
167223
if (is_subclass_of($pattern, PatternContract::class)) {
@@ -173,10 +229,17 @@ public function registerPatterns(array $patterns): self {
173229
return $this;
174230
}
175231

232+
/**
233+
* Retrieves the list of registered patterns.
234+
*
235+
* @return array An array of registered patterns.
236+
*/
176237
public function getPatterns(): array {
177238
return $this->patterns;
178239
}
179240

241+
// Expression flag methods:
242+
180243
public function asCaseInsensitive(): self {
181244
$this->pattern->addExpressionFlag("i");
182245
return $this;
@@ -201,7 +264,17 @@ public function asSticky(): self {
201264
$this->pattern->addExpressionFlag("y");
202265
return $this;
203266
}
204-
267+
268+
/**
269+
* Dynamically handles calls to pattern methods.
270+
*
271+
* This method is invoked when a method is called on the Builder that matches the name of a registered pattern.
272+
*
273+
* @param string $name The name of the method being called.
274+
* @param array $args The arguments passed to the method.
275+
* @return self The Builder instance, for method chaining.
276+
* @throws \LogicException If no patterns are registered or the pattern name is not found.
277+
*/
205278
public function __call(
206279
$name,
207280
$args

src/Contracts/PatternContract.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public function getInputValidationPattern(): string;
7676
*/
7777
public function getMatchesValidationPattern(): string;
7878

79+
/**
80+
* Adds regex flag to the pattern
81+
*
82+
* @param string $flag to add after regex pattern
83+
* @return void
84+
*/
7985
public function addExpressionFlag(string $flag): void;
8086

8187
/**

src/Options/CharOption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function checkSpecialChars(string $input) {
5353
return true;
5454
}
5555

56-
// Option methods
56+
// Option methods:
5757
public function minSpecialCharacters(int $count) {
5858
$this->minSpecialCharacters = $count;
5959
return $this;

src/Patterns/BasePattern.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,24 @@ class BasePattern implements PatternContract {
2727
*/
2828
protected string $pattern = "[a-z]";
2929

30+
/**
31+
* @var string Static property to define the pattern name.
32+
*/
3033
public static string $name = "";
3134

35+
/**
36+
* @var array Static property to define the pattern arguments one by one.
37+
*/
3238
public static array $args = [];
3339

40+
/**
41+
* @var array Static property to apply the default options for the pattern.
42+
*/
3443
public static array $defaultOptions = [];
3544

45+
/**
46+
* @var string A string to hold expression flags for the regex pattern.
47+
*/
3648
protected string $expressionFlags = "";
3749

3850
/**
@@ -169,6 +181,13 @@ public function getMatchesValidationPattern(): string {
169181
return "/{$this->pattern}/" . $this->expressionFlags;
170182
}
171183

184+
/**
185+
* Processes an array of arguments and builds an options array.
186+
*
187+
* @param array $args Names of the arguments.
188+
* @param array $values Values of the arguments.
189+
* @return array An associative array of options.
190+
*/
172191
protected static function processArguments(array $args, array $values): array {
173192
$options = [];
174193
// Build options array based on condition
@@ -184,12 +203,23 @@ protected static function processArguments(array $args, array $values): array {
184203
return $options;
185204
}
186205

206+
/**
207+
* Processes a callback function to configure options.
208+
*
209+
* @param callable $callback The callback function used for configuring options.
210+
* @return array An associative array of options set by the callback.
211+
*/
187212
protected static function processCallback(callable $callback): array {
188213
$optionsBuilder = new OptionsBuilder();
189214
$callback($optionsBuilder);
190215
return $optionsBuilder->getOptions();
191216
}
192217

218+
/**
219+
* Adds a regex expression flag to the pattern.
220+
*
221+
* @param string $flag The single-character flag to add to the regex pattern.
222+
*/
193223
public function addExpressionFlag(string $flag): void {
194224
if (strpos($this->expressionFlags, $flag) === false) {
195225
$this->expressionFlags .= $flag;

0 commit comments

Comments
 (0)