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

Implement clearMessage & clearMessages() #34

Merged
merged 1 commit into from
Oct 22, 2017
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
36 changes: 36 additions & 0 deletions src/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,40 @@ public function hasMessage($key)
$messages = $this->getMessages();
return isset($messages[$key]);
}

/**
* Clear all messages
*
* @return void
*/
public function clearMessages()
{
if (isset($this->storage[$this->storageKey])) {
$this->storage[$this->storageKey] = [];
}

$this->fromPrevious = [];
$this->forNow = [];
}

/**
* Clear specific message
*
* @param String $key The key to clear
* @return void
*/
public function clearMessage($key)
{
if (isset($this->storage[$this->storageKey][$key])) {
unset($this->storage[$this->storageKey][$key]);
}

if (isset($this->fromPrevious[$key])) {
unset($this->fromPrevious[$key]);
}

if (isset($this->forNow[$key])) {
unset($this->forNow[$key]);
}
}
}
35 changes: 35 additions & 0 deletions tests/MessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,39 @@ public function testHasMessage()
$flash = new Messages($storage);
$this->assertTrue($flash->hasMessage('Test'));
}

public function testClearMessages()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$storage = ['slimFlash' => [ 'Test' => ['Test']]];
$flash = new Messages($storage);
$flash->addMessageNow('Now', 'hear this');
$this->assertTrue($flash->hasMessage('Test'));
$this->assertTrue($flash->hasMessage('Now'));

$flash->clearMessages();
$this->assertFalse($flash->hasMessage('Test'));
$this->assertFalse($flash->hasMessage('Now'));
}

public function testClearMessage()
{
$storage = ['slimFlash' => []];
$flash = new Messages($storage);

$storage = ['slimFlash' => [ 'Test' => ['Test'], 'Foo' => ['Bar']]];
$flash = new Messages($storage);
$flash->addMessageNow('Now', 'hear this');
$this->assertTrue($flash->hasMessage('Test'));
$this->assertTrue($flash->hasMessage('Foo'));
$this->assertTrue($flash->hasMessage('Now'));

$flash->clearMessage('Test');
$flash->clearMessage('Now');
$this->assertFalse($flash->hasMessage('Test'));
$this->assertFalse($flash->hasMessage('Now'));
$this->assertTrue($flash->hasMessage('Foo'));
}
}