From 27ac244dfd4d103c28d204ab59b3deeec8411c63 Mon Sep 17 00:00:00 2001 From: Pedro Oliveira <38913462+PH7-Jack@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:03:23 -0300 Subject: [PATCH] add missing method to message bag class (#48348) --- src/Illuminate/Support/MessageBag.php | 13 +++++++++++++ tests/Support/SupportMessageBagTest.php | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Illuminate/Support/MessageBag.php b/src/Illuminate/Support/MessageBag.php index 27116941b7f5..4b303168d63f 100755 --- a/src/Illuminate/Support/MessageBag.php +++ b/src/Illuminate/Support/MessageBag.php @@ -159,6 +159,19 @@ public function hasAny($keys = []) return false; } + /** + * Determine if messages don't exist for all of the given keys. + * + * @param array|string|null $key + * @return bool + */ + public function missing($key) + { + $keys = is_array($key) ? $key : func_get_args(); + + return ! $this->hasAny($keys); + } + /** * Get the first message from the message bag for a given key. * diff --git a/tests/Support/SupportMessageBagTest.php b/tests/Support/SupportMessageBagTest.php index 1f5477124ca9..af55b6d3f7c6 100755 --- a/tests/Support/SupportMessageBagTest.php +++ b/tests/Support/SupportMessageBagTest.php @@ -115,6 +115,19 @@ public function testHasIndicatesExistence() $this->assertFalse($container->has('bar')); } + public function testMissingIndicatesNonExistence() + { + $container = new MessageBag; + $container->setFormat(':message'); + $container->add('foo', 'bar'); + $this->assertFalse($container->missing('foo')); + $this->assertFalse($container->missing(['foo', 'baz'])); + $this->assertFalse($container->missing('foo', 'baz')); + $this->assertTrue($container->missing('baz')); + $this->assertTrue($container->missing(['baz', 'biz'])); + $this->assertTrue($container->missing('baz', 'biz')); + } + public function testAddIf() { $container = new MessageBag;