Skip to content

Commit 1736b76

Browse files
Junaid Farooqjunaidbinfarooq
authored andcommitted
feat: Adds an assertion to check if all keys exist in an array
- Adds the assertion to assert if all the provided keys exist in the given array - Adds types - Regenerates mixin
1 parent 6bfce1b commit 1736b76

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/Assert.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,22 @@ public static function keyExists(mixed $array, string|int $key, string $message
18571857
return $array;
18581858
}
18591859

1860+
/**
1861+
* @psalm-pure
1862+
*
1863+
* @param list<int|string> $keys
1864+
*
1865+
* @throws InvalidArgumentException
1866+
*/
1867+
public static function KeysExist(mixed $array, array $keys, string $message = ''): void
1868+
{
1869+
static::isArray($array);
1870+
1871+
foreach ($keys as $key) {
1872+
static::keyExists($array, $key, $message ?: 'Expected the key %s to exist.',);
1873+
}
1874+
}
1875+
18601876
/**
18611877
* @psalm-pure
18621878
*

src/Mixin.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,6 +4490,62 @@ public static function allNullOrKeyExists(mixed $array, string|int $key, string
44904490
return $array;
44914491
}
44924492

4493+
/**
4494+
* @psalm-pure
4495+
*
4496+
* @param list<int|string> $keys
4497+
*
4498+
* @return mixed
4499+
*
4500+
* @throws InvalidArgumentException
4501+
*/
4502+
public static function nullOrKeysExist(mixed $array, array $keys, string $message = ''): mixed
4503+
{
4504+
null === $array || static::KeysExist($array, $keys, $message);
4505+
4506+
return $array;
4507+
}
4508+
4509+
/**
4510+
* @psalm-pure
4511+
*
4512+
* @param list<int|string> $keys
4513+
*
4514+
* @return mixed
4515+
*
4516+
* @throws InvalidArgumentException
4517+
*/
4518+
public static function allKeysExist(mixed $array, array $keys, string $message = ''): mixed
4519+
{
4520+
static::isIterable($array);
4521+
4522+
foreach ($array as $entry) {
4523+
static::KeysExist($entry, $keys, $message);
4524+
}
4525+
4526+
return $array;
4527+
}
4528+
4529+
/**
4530+
* @psalm-pure
4531+
*
4532+
* @param list<int|string> $keys
4533+
*
4534+
* @return mixed
4535+
*
4536+
* @throws InvalidArgumentException
4537+
*/
4538+
public static function allNullOrKeysExist(mixed $array, array $keys, string $message = ''): mixed
4539+
{
4540+
static::isIterable($array);
4541+
4542+
foreach ($array as $entry) {
4543+
null === $entry || static::KeysExist($entry, $keys, $message);
4544+
}
4545+
4546+
return $array;
4547+
}
4548+
44934549
/**
44944550
* @psalm-pure
44954551
*

0 commit comments

Comments
 (0)