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

Allow zero hops for flashNow() #8

Merged
merged 4 commits into from
Apr 14, 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
12 changes: 11 additions & 1 deletion docs/book/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ the flash message will persist for. The default value is `1`, indicating a
single hop. This value is provided when you call `flash()` as an optional third
argument.

The `$hops` value passed to `flash()` must be greater than zero. Passing a value
less than one will result in `flash()` throwing an exception of type
`Mezzio\Flash\Exception\InvalidHopsValueException`.

To have a message persist for three "hops", you might call `flash()` as follows:

```php
Expand Down Expand Up @@ -169,4 +173,10 @@ $flashMessages->flashNow($messageName, $messageValue);
```

The signature of this method is the same as for `flash()`, and allows you to
optionally provide a `$hops` value as well.
optionally provide a `$hops` value as well. Unlike `flash()`, `flashNow()` will
accept a value of zero for `$hops`, which is useful if you want your message to
be visible _exclusively_ in the current request.

```php
$flashMessages->flashNow($messageName, 'One night (request) only!', 0);
```
7 changes: 6 additions & 1 deletion src/FlashMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ public function flash(string $key, $value, int $hops = 1): void
* using this method, you may make the value available during the current
* request as well.
*
* If you want the value to be visible only in the current request, you may
* pass zero as the third argument.
*
* @param mixed $value
*/
public function flashNow(string $key, $value, int $hops = 1): void
{
$this->currentMessages[$key] = $value;
$this->flash($key, $value, $hops);
if ($hops > 0) {
$this->flash($key, $value, $hops);
}
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
18 changes: 18 additions & 0 deletions test/FlashMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,24 @@ public function testCreationAggregatesThrowsExceptionIfInvalidNumberOfHops()
$this->anything()
);

$flash = FlashMessages::createFromSession($this->session);
$flash->flash('test', 'value', 0);
}

public function testFlashNowAcceptsZeroHops()
{
$flash = FlashMessages::createFromSession($this->session);
$flash->flashNow('test', 'value', 0);

$this->assertSame('value', $flash->getFlash('test'));
}

public function testFlashNowWithZeroHopsShouldNotSetValueToSession()
{
$this->session
->expects($this->never())
->method('set');

$flash = FlashMessages::createFromSession($this->session);
$flash->flashNow('test', 'value', 0);
}
Expand Down