Skip to content

Commit 5b2aa2b

Browse files
authored
Merge pull request #7 from MaestroError/support-laravel-collection-in-get-method
Added support for laravel collection in get method
2 parents b2fd99e + a157a6b commit 5b2aa2b

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ To stay updated, follow the GitHub repository for the latest changes, releases,
11821182
- usernameLength: Set minimum and maximum length for the username part of the email.
11831183
- dateFormat, timeFormat: Specify the format of date and time (e.g., MM-DD-YYYY, HH:MM).
11841184
- Consider to register Patterns like options using key (name) => value (class) pairs (check performance) ✔️ (_No significant change before 50+ patterns_)
1185-
- Return collection on get method if laravel is available.
1185+
- Return collection on get method if laravel is available.✔️
11861186
- Add builderPattern methods list MD file and link from the Docs.
11871187
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
11881188
- Add some tool for debuging options

src/Builder.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,21 @@ public function setString(string $str): void {
192192
$this->str = $str;
193193
}
194194

195-
public function get(): ?array {
195+
public function get(): mixed {
196196
if (!$this->patternIsSet()) {
197197
throw new \LogicException("Pattern must be set before getting matches.");
198198
}
199-
return $this->getAllMatches();
199+
200+
$matches = $this->getAllMatches();
201+
202+
// Check if Laravel Collection class exists and the collect helper function is available
203+
if (class_exists(\Illuminate\Support\Collection::class) && function_exists('collect')) {
204+
// Return matches as a Laravel Collection
205+
return collect($matches);
206+
}
207+
208+
// Return matches as an array if Collection or collect() is not available
209+
return $matches;
200210
}
201211

202212
public function check(): bool {

0 commit comments

Comments
 (0)