From 2333ac3fc6d6e945e4f475839d4f33db1d744bf7 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Wed, 29 May 2024 17:36:48 +0300 Subject: [PATCH] Added the feature to return linkItems directly --- Changelog.md | 2 ++ src/Links/Changelog.md | 6 ++++++ src/Links/Query/Get.php | 20 ++++++++++++++++---- src/Links/Query/WantsLinksOrGroups.php | 7 +++++++ src/Links/Support/helpers.php | 9 +++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index 498778cd..3cc27348 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,8 @@ - Added the `priceGreaterThan`, `priceLessThan` and `priceBetween` methods to the ProductSearch class by [Matima](https://github.com/mahdirezaei-dev) in [#176](https://github.com/vanilophp/framework/pull/176) - Added the `Macroable` trait to the `ProductSearch` class +- Added the possibility to retrieve the link items directly using `linkItems()` method as `Get::the($type)->linkItems()->of($model)` +- Added the `link_items` helper (shortcut to Get::the()->linkItems() - Changed the offline payment gateway's icon from a circle to a plug+x ## 4.0.1 diff --git a/src/Links/Changelog.md b/src/Links/Changelog.md index c51b66ed..b7a8b72a 100644 --- a/src/Links/Changelog.md +++ b/src/Links/Changelog.md @@ -2,6 +2,12 @@ ## 4.x Series +## Unreleased +##### 2024-XX-YY + +- Added the possibility to retrieve the link items directly using `linkItems()` method as `Get::the($type)->linkItems()->of($model)` +- Added the `link_items` helper (shortcut to Get::the()->linkItems() + ## 4.0.0 ##### 2024-04-25 diff --git a/src/Links/Query/Get.php b/src/Links/Query/Get.php index e8df290e..1613bd3b 100644 --- a/src/Links/Query/Get.php +++ b/src/Links/Query/Get.php @@ -42,9 +42,21 @@ public function of(Model $model): Collection return $groups; } - $links = collect(); - $groups->each(function ($group) use ($links, $model) { - $links->push( + $result = collect(); + if ('linkItems' === $this->wants) { + $groups->each(function ($group) use ($result, $model) { + $result->push( + ...$group + ->items + ->reject(fn ($item) => $item->linkable_id === $model->id) + ); + }); + + return $result; + } + + $groups->each(function ($group) use ($result, $model) { + $result->push( ...$group ->items ->map @@ -53,6 +65,6 @@ public function of(Model $model): Collection ); }); - return $links; + return $result; } } diff --git a/src/Links/Query/WantsLinksOrGroups.php b/src/Links/Query/WantsLinksOrGroups.php index dc007d1c..f1dce31e 100644 --- a/src/Links/Query/WantsLinksOrGroups.php +++ b/src/Links/Query/WantsLinksOrGroups.php @@ -31,4 +31,11 @@ public function groups(): self return $this; } + + public function linkItems(): self + { + $this->wants = 'linkItems'; + + return $this; + } } diff --git a/src/Links/Support/helpers.php b/src/Links/Support/helpers.php index d1ca8fcc..c6a4e04b 100644 --- a/src/Links/Support/helpers.php +++ b/src/Links/Support/helpers.php @@ -14,6 +14,15 @@ function links(string $type, string $property = null): Get } } +if (!function_exists('link_items')) { + function link_items(string $type, string $property = null): Get + { + $result = Get::the($type)->linkItems(); + + return null !== $property ? $result->basedOn($property) : $result; + } +} + if (!function_exists('link_groups')) { function link_groups(string $type, string $property = null): Get {