From a157a6b37b443cb533abf0032ceadda494511075 Mon Sep 17 00:00:00 2001 From: maestroerror Date: Mon, 4 Mar 2024 23:21:37 +0400 Subject: [PATCH] Added support for laravel collection in get method --- README.md | 2 +- src/Builder.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 19ee8ef..f7a65f0 100644 --- a/README.md +++ b/README.md @@ -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\d{4})-(?P\d{2})-(?P\d{2})/` - Add some tool for debuging options diff --git a/src/Builder.php b/src/Builder.php index c8cf82d..6c01663 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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 {