Skip to content

Commit b2249bd

Browse files
committed
Allow Entity classname in BreadcrumbBuilder
1 parent b851015 commit b2249bd

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

docs/guide/link-to.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ In more complex cases you can also handle the full breadcrumb, by using the `wit
7070

7171
```php
7272
LinkToShowPage::make(PlayerEntity::class, 1)
73-
->withBreadcrumb(function (BreadcrumbBuilder $builder) {
74-
return $builder
75-
->appendEntityList('team')
76-
->appendShowPage('team', 6);
77-
})
73+
->withBreadcrumb(fn (BreadcrumbBuilder $builder) => $builder
74+
->appendEntityList(TeamEntity::class)
75+
->appendShowPage(TeamEntity::class, 6)
76+
)
7877
->renderAsUrl(),
7978
```
8079

docs/guide/testing-with-sharp.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ it('allow the user to display a leaf form', function () {
7777
->loginAsSharpUser()
7878
->withSharpBreadcrumb(function (BreadcrumbBuilder $builder) {
7979
return $builder
80-
->appendEntityList('trees')
81-
->appendShowPage('trees', 6)
82-
->appendShowPage('leaves', 16);
80+
->appendEntityList(TreeEntity::class)
81+
->appendShowPage(TreeEntity::class, 6)
82+
->appendShowPage(LeafEntity::class, 16);
8383
})
84-
->getSharpForm('leaves', 16)
84+
->getSharpForm(LeafEntity::class, 16)
8585
->assertOk();
8686
});
8787
```

src/Utils/Links/BreadcrumbBuilder.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Code16\Sharp\Exceptions\SharpInvalidBreadcrumbItemException;
66
use Code16\Sharp\Http\Context\Util\BreadcrumbItem;
7+
use Code16\Sharp\Utils\Entities\SharpEntityManager;
78
use Illuminate\Support\Traits\Conditionable;
89

910
class BreadcrumbBuilder
@@ -12,34 +13,37 @@ class BreadcrumbBuilder
1213

1314
protected array $breadcrumbParts = [];
1415

15-
public function appendEntityList(string $entityKey): self
16+
public function appendEntityList(string $entityClassNameOrKey): self
1617
{
1718
if (! empty($this->breadcrumbParts)) {
1819
throw new SharpInvalidBreadcrumbItemException('Entity list must be the first breadcrumb item');
1920
}
2021

22+
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
2123
$this->breadcrumbParts[] = new BreadcrumbItem('s-list', $entityKey);
2224

2325
return $this;
2426
}
2527

26-
public function appendSingleShowPage(string $entityKey): self
28+
public function appendSingleShowPage(string $entityClassNameOrKey): self
2729
{
2830
if (! empty($this->breadcrumbParts)) {
2931
throw new SharpInvalidBreadcrumbItemException('Single show page must be the first breadcrumb item');
3032
}
3133

34+
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
3235
$this->breadcrumbParts[] = new BreadcrumbItem('s-show', $entityKey);
3336

3437
return $this;
3538
}
3639

37-
public function appendShowPage(string $entityKey, string $instanceId): self
40+
public function appendShowPage(string $entityClassNameOrKey, string $instanceId): self
3841
{
3942
if (empty($this->breadcrumbParts)) {
4043
throw new SharpInvalidBreadcrumbItemException('Show page can not be the first breadcrumb item');
4144
}
4245

46+
$entityKey = $this->resolveEntityKey($entityClassNameOrKey);
4347
$this->breadcrumbParts[] = (new BreadcrumbItem('s-show', $entityKey))->setInstance($instanceId);
4448

4549
return $this;
@@ -51,4 +55,11 @@ public function generateUri(): string
5155
->map(fn (BreadcrumbItem $item) => $item->toUri())
5256
->implode('/');
5357
}
58+
59+
private function resolveEntityKey(string $entityClassNameOrKey): string
60+
{
61+
return class_exists($entityClassNameOrKey)
62+
? app(SharpEntityManager::class)->entityKeyFor($entityClassNameOrKey)
63+
: $entityClassNameOrKey;
64+
}
5465
}

tests/Unit/Utils/SharpLinkToTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
);
6262
});
6363

64-
it('allows to generate a link to a show page passing an SharpEntity class', function () {
64+
it('allows to generate a link to a show page passing a SharpEntity class', function () {
6565
app(SharpConfigBuilder::class)
6666
->addEntity('person', PersonEntity::class);
6767

@@ -84,6 +84,22 @@
8484
);
8585
});
8686

87+
it('allows to generate an url to a show page with a specific breadcrumb passing a SharpEntity class', function () {
88+
app(SharpConfigBuilder::class)
89+
->addEntity('person', PersonEntity::class);
90+
91+
$this->assertEquals(
92+
'http://localhost/sharp/s-list/person/s-show/person/3/s-show/person/4',
93+
LinkToShowPage::make(PersonEntity::class, 4)
94+
->withBreadcrumb(function (BreadcrumbBuilder $builder) {
95+
return $builder
96+
->appendEntityList(PersonEntity::class)
97+
->appendShowPage(PersonEntity::class, 3);
98+
})
99+
->renderAsUrl(),
100+
);
101+
});
102+
87103
it('allows to generate an url to a form with a specific breadcrumb', function () {
88104
$this->assertEquals(
89105
'http://localhost/sharp/s-list/base-entity/s-show/base-entity/3/s-show/my-entity/4/s-form/my-entity/4',

0 commit comments

Comments
 (0)