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

[5.2] Add hasAny and hasAll methods to MessageBag #14151

Merged
merged 4 commits into from
Jun 29, 2016
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
34 changes: 34 additions & 0 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ public function has($key = null)
return $this->first($key) !== '';
}

/**
* Determine if messages exist for all given keys.
*
* @param array $keys
* @return bool
*/
public function hasAll($keys = [])
{
foreach ($keys as $key) {
if ($this->first($key) === '') {
return false;
}
}

return true;
}

/**
* Determine if messages exist for any given key.
*
* @param array $keys
* @return bool
*/
public function hasAny($keys = [])
{
foreach ($keys as $key) {
if ($this->first($key) !== '') {
return true;
}
}

return false;
}

/**
* Get the first message from the bag for a given key.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Support/SupportMessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ public function testHasIndicatesExistence()
$this->assertFalse($container->has('bar'));
}

public function testHasAnyIndicatesExistence()
{
$container = new MessageBag;
$container->setFormat(':message');
$container->add('foo', 'bar');
$container->add('bar', 'foo');
$container->add('boom', 'baz');
$this->assertTrue($container->hasAny(['foo', 'bar']));
$this->assertTrue($container->hasAny(['boom', 'baz']));
$this->assertFalse($container->hasAny(['baz']));
}

public function testHasAllIndicatesExistence()
{
$container = new MessageBag;
$container->setFormat(':message');
$container->add('foo', 'bar');
$container->add('bar', 'foo');
$container->add('boom', 'baz');
$this->assertTrue($container->hasAll(['foo', 'bar', 'boom']));
$this->assertFalse($container->hasAll(['foo', 'bar', 'boom', 'baz']));
$this->assertFalse($container->hasAll(['foo', 'baz']));
}

public function testAllReturnsAllMessages()
{
$container = new MessageBag;
Expand Down