From 7a82552ca73f4912c371336e6a7e696ab2fe64e2 Mon Sep 17 00:00:00 2001 From: Attila Fulop <1162360+fulopattila122@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:17:35 +0300 Subject: [PATCH] LinkGroup and LinkGroupItem improvements --- Changelog.md | 2 ++ src/Links/Changelog.md | 2 ++ src/Links/Models/LinkGroup.php | 15 +++++++++++++++ src/Links/Models/LinkGroupItem.php | 6 ++++++ src/Links/Query/EliminateLinks.php | 5 +---- src/Links/Query/Get.php | 4 ++-- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Changelog.md b/Changelog.md index 8c345da5..9583fbc5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -8,6 +8,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 unidirectional links feature +- Added the `isUnidirectional()`, `isOmnidirectional()` and `isEmpty()` methods to the `LinkGroup` class +- Added the `pointsTo()` method to the `LinkGroupItem` 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() - Added support for configurable decimal separator used by the `format_price()` helper function diff --git a/src/Links/Changelog.md b/src/Links/Changelog.md index 0c069a79..9cea6309 100644 --- a/src/Links/Changelog.md +++ b/src/Links/Changelog.md @@ -6,6 +6,8 @@ ##### 2024-XX-YY - Added the unidirectional links feature +- Added the `isUnidirectional()`, `isOmnidirectional()` and `isEmpty()` methods to the `LinkGroup` class +- Added the `pointsTo()` method to the `LinkGroupItem` 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() diff --git a/src/Links/Models/LinkGroup.php b/src/Links/Models/LinkGroup.php index 321aeb2c..d98e84be 100644 --- a/src/Links/Models/LinkGroup.php +++ b/src/Links/Models/LinkGroup.php @@ -58,4 +58,19 @@ public function rootItem(): BelongsTo { return $this->belongsTo(LinkGroupItemProxy::modelClass(), 'root_item_id', 'id'); } + + public function isUnidirectional(): bool + { + return null !== $this->root_item_id; + } + + public function isOmnidirectional(): bool + { + return null === $this->root_item_id; + } + + public function isEmpty(): bool + { + return $this->items->isEmpty(); + } } diff --git a/src/Links/Models/LinkGroupItem.php b/src/Links/Models/LinkGroupItem.php index 95cd276c..bbe30ea2 100644 --- a/src/Links/Models/LinkGroupItem.php +++ b/src/Links/Models/LinkGroupItem.php @@ -45,4 +45,10 @@ public function linkable(): MorphTo { return $this->morphTo(); } + + public function pointsTo(Model $model): bool + { + return $this->linkable_id == $model->id && // Weak typing is intentional + $this->linkable_type === morph_type_of($model); + } } diff --git a/src/Links/Query/EliminateLinks.php b/src/Links/Query/EliminateLinks.php index 6f332cf8..a0170fec 100644 --- a/src/Links/Query/EliminateLinks.php +++ b/src/Links/Query/EliminateLinks.php @@ -37,10 +37,7 @@ public function and(Model ...$models): void foreach ($this->linkGroupsOfModel($this->baseModel) as $group) { $itemsToDelete = $group ->items - ->filter(fn ($item) => $toRemove->contains(function ($modelToRemove) use ($item) { - return $modelToRemove->id == $item->linkable_id && - $this->morphTypeOf($modelToRemove::class) === $item->linkable_type; - })); + ->filter(fn ($item) => $toRemove->contains(fn ($modelToRemove) => $item->pointsTo($modelToRemove))); LinkGroupItemProxy::destroy($itemsToDelete->map->id); } } diff --git a/src/Links/Query/Get.php b/src/Links/Query/Get.php index 1b1df22b..8540ba9d 100644 --- a/src/Links/Query/Get.php +++ b/src/Links/Query/Get.php @@ -45,7 +45,7 @@ public function of(Model $model): Collection $result = collect(); if ('linkItems' === $this->wants) { $groups->each(function ($group) use ($result, $model) { - if (is_null($group->root_item_id) || $group->rootItem->linkable_id === $model->id) { + if (is_null($group->root_item_id) || $group->rootItem->pointsTo($model)) { $result->push( ...$group ->items @@ -58,7 +58,7 @@ public function of(Model $model): Collection } $groups->each(function ($group) use ($result, $model) { - if (is_null($group->root_item_id) || $group->rootItem->linkable_id === $model->id) { + if (is_null($group->root_item_id) || $group->rootItem->pointsTo($model)) { $result->push( ...$group ->items