Skip to content

Commit

Permalink
ArrayUtils::checkIssetKeys : Modify method with not strict comparaison
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Mar 27, 2024
1 parent 1c772b9 commit be469f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
CHANGELOG for 1.x
===================
## v1.1.1 - (2024-03-27)
### Fix

- `ArrayUtils::checkIssetKeys` : Modify method with not strict comparaison

## v1.1.0 - (2024-03-25)
### Added
- new `ArrayUtils` methods : `checkIssetKeys`, `trimExplode`, `removeEmpty`, `filterByPattern`, `flatToMap`
Expand Down
11 changes: 9 additions & 2 deletions src/Utils/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,21 @@ public static function flattenArrayValues(array $input, string $separator = ',')
}

/**
* Check if all keys are in array
* Check if all keys are set in array
* It's not a strict comparaison, all array values may not be in keys
*
* @param array $array array to compare
* @param array $keys keys to check
*/
public static function checkIssetKeys(array $array, array $keys): bool
{
return empty(array_diff(array_keys($array), $keys));
foreach ($keys as $key) {
if (!isset($array[$key])) {
return false;
}
}

return true;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions tests/Utils/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ public function checkIssetKeysProvider(): array
// $keys
[0, 1, 5],
],
'last missing' => [
'one missing' => [
// expected
false,
true,
// array
[
0 => "000",
Expand All @@ -369,14 +369,13 @@ public function checkIssetKeysProvider(): array
// $keys
[0, 1],
],
'first missing' => [
'key is not present' => [
// expected
false,
// array
[
'dummy' => "dummy",
'foo' => "foo",
'bar' => "bar",
],
// $keys
['foo', 'bar'],
Expand Down

0 comments on commit be469f9

Please sign in to comment.