Skip to content

Commit

Permalink
LinkGroup and LinkGroupItem improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 14, 2024
1 parent 5495b5a commit 7a82552
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Links/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
15 changes: 15 additions & 0 deletions src/Links/Models/LinkGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
6 changes: 6 additions & 0 deletions src/Links/Models/LinkGroupItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 1 addition & 4 deletions src/Links/Query/EliminateLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Links/Query/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7a82552

Please sign in to comment.