Skip to content

Commit

Permalink
Added the new() method to the Establish link class
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 14, 2024
1 parent 7a82552 commit 0f1ea9f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- 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 the possibility to force new link group creation using the `new()` method of the `Establish` class
- Added support for configurable decimal separator used by the `format_price()` helper function
- Added the discountable shipping fee calculator
- Added the `taxes_total`, `shipping_total` and `total` attribute getters to the Foundation `Order` model
Expand Down
1 change: 1 addition & 0 deletions src/Links/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- 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 possibility to force new link group creation using the `new()` method of the `Establish` class
- Added the `link_items` helper, shortcut to Get::the()->linkItems()

## 4.0.0
Expand Down
15 changes: 13 additions & 2 deletions src/Links/Query/Establish.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class Establish

private bool $unidirectional = false;

private bool $dontUseExistingGroup = false;

private string $wants = 'link';

public static function a(LinkType|string $type): self
Expand All @@ -48,6 +50,13 @@ public function unidirectional(): self
return $this;
}

public function new(): self
{
$this->dontUseExistingGroup = true;

return $this;
}

public function link(): self
{
$this->wants = 'link';
Expand All @@ -64,8 +73,10 @@ public function group(): self

public function and(Model ...$models): void
{
$groups = $this->linkGroupsOfModel($this->baseModel);
$destinationGroup = $groups->first();
$destinationGroup = match ($this->dontUseExistingGroup) {
true => null,
false => $this->linkGroupsOfModel($this->baseModel)->first(),
};
if (null === $destinationGroup) {
$destinationGroup = $this->createNewLinkGroup();
$rootItem = LinkGroupItemProxy::create([
Expand Down
17 changes: 17 additions & 0 deletions src/Links/Tests/Feature/QueryEstablishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ public function unidirectional_links_can_be_established_between_multiple_entries
$this->assertCount(0, $case2->links('protection'));
}

/** @test */
public function it_can_be_requested_to_create_new_groups_instead_of_reusing_existing_ones()
{
$phone = TestLinkableProduct::create(['name' => 'iPhone 19'])->fresh();
$caseBlue = TestLinkableProduct::create(['name' => 'iPhone 19 Silicon Case Blue'])->fresh();
$caseGreen = TestLinkableProduct::create(['name' => 'iPhone 19 Silicon Case Green'])->fresh();
LinkType::create(['name' => 'Supplements']);

Establish::a('supplements')->new()->link()->between($phone)->and($caseBlue);
Establish::a('supplements')->new()->link()->between($phone)->and($caseGreen);

$this->assertCount(2, Get::the('supplements')->groups()->of($phone));
$this->assertCount(2, $phone->links('supplements'));
$this->assertCount(1, $caseBlue->links('supplements'));
$this->assertCount(1, $caseGreen->links('supplements'));
}

protected function setUpDatabase($app)
{
$this->loadMigrationsFrom(dirname(__DIR__) . '/migrations_of_property_module');
Expand Down

0 comments on commit 0f1ea9f

Please sign in to comment.