Skip to content

Commit

Permalink
Added support for laravel collection in get method
Browse files Browse the repository at this point in the history
  • Loading branch information
MaestroError committed Mar 4, 2024
1 parent b2fd99e commit a157a6b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ To stay updated, follow the GitHub repository for the latest changes, releases,
- usernameLength: Set minimum and maximum length for the username part of the email.
- dateFormat, timeFormat: Specify the format of date and time (e.g., MM-DD-YYYY, HH:MM).
- Consider to register Patterns like options using key (name) => value (class) pairs (check performance) ✔️ (_No significant change before 50+ patterns_)
- Return collection on get method if laravel is available.
- Return collection on get method if laravel is available.✔️
- Add builderPattern methods list MD file and link from the Docs.
- Implement usage of named groups: `/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/`
- Add some tool for debuging options
Expand Down
14 changes: 12 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,21 @@ public function setString(string $str): void {
$this->str = $str;
}

public function get(): ?array {
public function get(): mixed {
if (!$this->patternIsSet()) {
throw new \LogicException("Pattern must be set before getting matches.");
}
return $this->getAllMatches();

$matches = $this->getAllMatches();

// Check if Laravel Collection class exists and the collect helper function is available
if (class_exists(\Illuminate\Support\Collection::class) && function_exists('collect')) {
// Return matches as a Laravel Collection
return collect($matches);
}

// Return matches as an array if Collection or collect() is not available
return $matches;
}

public function check(): bool {
Expand Down

0 comments on commit a157a6b

Please sign in to comment.