diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index bea43ce76c26..b8fa075073a4 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -495,6 +495,30 @@ public static function has($array, $keys) return true; } + /** + * Determine if all keys exist in an array using "dot" notation. + * + * @param \ArrayAccess|array $array + * @param string|array $keys + * @return bool + */ + public static function hasAll($array, $keys) + { + $keys = (array) $keys; + + if (! $array || $keys === []) { + return false; + } + + foreach ($keys as $key) { + if (! static::has($array, $key)) { + return false; + } + } + + return true; + } + /** * Determine if any of the keys exist in an array using "dot" notation. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index a81e6eb79bee..94fb488ec3c7 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -702,6 +702,29 @@ public function testHas() $this->assertFalse(Arr::has([], [''])); } + public function testHasAllMethod() + { + $array = ['name' => 'Taylor', 'age' => '', 'city' => null]; + $this->assertTrue(Arr::hasAll($array, 'name')); + $this->assertTrue(Arr::hasAll($array, 'age')); + $this->assertFalse(Arr::hasAll($array, ['age', 'car'])); + $this->assertTrue(Arr::hasAll($array, 'city')); + $this->assertFalse(Arr::hasAll($array, ['city', 'some'])); + $this->assertTrue(Arr::hasAll($array, ['name', 'age', 'city'])); + $this->assertFalse(Arr::hasAll($array, ['name', 'age', 'city', 'country'])); + + $array = ['user' => ['name' => 'Taylor']]; + $this->assertTrue(Arr::hasAll($array, 'user.name')); + $this->assertFalse(Arr::hasAll($array, 'user.age')); + + $array = ['name' => 'Taylor', 'age' => '', 'city' => null]; + $this->assertFalse(Arr::hasAll($array, 'foo')); + $this->assertFalse(Arr::hasAll($array, 'bar')); + $this->assertFalse(Arr::hasAll($array, 'baz')); + $this->assertFalse(Arr::hasAll($array, 'bah')); + $this->assertFalse(Arr::hasAll($array, ['foo', 'bar', 'baz', 'bar'])); + } + public function testHasAnyMethod() { $array = ['name' => 'Taylor', 'age' => '', 'city' => null];