Skip to content

Commit 9bd7b44

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 9bd7b44

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

0 commit comments

Comments
 (0)