Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stringable #8

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use their documentation to craft your menus as simple or complex as you like:
* [Version 3](https://spatie.be/docs/menu/v3) (PHP 8 only)

Create your menus by extending `Tatter\Menus\Menu`. You will notice in the source code that
`Menu` requires you to provide one method: `public function get(): string;`. You may use the
`Menu` requires you to provide one method: `public function __toString(): string;`. You may use the
supplied `$builder` property to access the underlying `Spatie\Menu` to build your menu,
or provide your own HTML code or `view()` return. Some examples:
```
class MainMenu extends \Tatter\Menus\Menu
{
public function get(): string
public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
Expand All @@ -61,7 +61,7 @@ class MainMenu extends \Tatter\Menus\Menu

class FruitMenu extends \Tatter\Menus\Menu
{
public function get(): string
public function __toString(): string
{
return view('menus/fruit', ['active' => 'banana']);
}
Expand All @@ -75,7 +75,7 @@ relative and absolute URLs you must supply full URL values (e.g. with `site_url(

### Deploying

Since `Menu->get()` returns a `string` it can be used in your view or layout files as is.
Since `Menu` is `Stringable` it can be used in your view or layout files as is.
However, **Menus** also comes with a [Controller Filter](https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html)
that you can use to inject menu content directly into your responses. First you need to create
an alias for each `Menu` class you would like to use. Create **app/Config/Menus.php** (or
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/MenusFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
}

// Grab the menu content
$content = (new $class)->get();
$content = (string) (new $class);
$count = 0;

// Swap the content for the placeholder and verify a match
Expand Down
2 changes: 1 addition & 1 deletion src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ public function builder(): BaseMenu
*
* @return string
*/
abstract public function get(): string;
abstract public function __toString(): string;
}
2 changes: 1 addition & 1 deletion tests/_support/Menus/TestMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestMenu extends Menu
*
* @return string
*/
public function get(): string
public function __toString(): string
{
return 'bananas';
}
Expand Down
8 changes: 4 additions & 4 deletions tests/menu/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function setUp(): void

$this->menu = new class extends Menu {

public function get(): string
public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
Expand All @@ -42,21 +42,21 @@ public function testUsesBuilder()
{
$menu = new class(BaseMenu::new()->link(site_url('/home'), 'asparagus')) extends Menu {

public function get(): string
public function __toString(): string
{
return $this->builder->render();
}
};

$result = $menu->get();
$result = (string) $menu;

$this->assertSame('<ul><li><a href="http://example.com/home">asparagus</a></li></ul>', $result);
}

public function testGetUsesCurrentUrl()
{
$expected = '<ul><li><a href="http://example.com/">Home</a></li><li class="active exact-active"><a href="http://example.com/current">Grain</a></li></ul>';
$result = $this->menu->get();
$result = $this->menu->__toString();

$this->assertSame($expected, $result);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/menu/TraitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testBootstrapAppliesClasses()

use Bootstrap;

public function get(): string
public function __toString(): string
{
return $this->builder
->link(site_url('/'), 'Home')
Expand All @@ -21,7 +21,7 @@ public function get(): string
}
};

$result = $menu->get();
$result = $menu->__toString();

$this->assertStringContainsString('li class="nav-item"', $result);
$this->assertStringContainsString('class="nav-link"', $result);
Expand Down