From dcb50b332e9547b8a63e40dcc4d98fe488fb9cad Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Wed, 27 Mar 2024 15:09:13 +0100 Subject: [PATCH] `ArrayUtils::checkIssetKeys` : Modify method with not strict comparaison --- CHANGELOG.md | 5 +++++ src/Utils/ArrayUtils.php | 11 +++++++++-- tests/Utils/ArrayUtilsTest.php | 7 +++---- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19213b4..deaee85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ CHANGELOG for 1.x =================== +## v1.2.1 - (2024-03-27) +### Fix + +- `ArrayUtils::checkIssetKeys` : Modify method with not strict comparaison + ## v1.2.0 - (2024-03-27) ### Added - Common Entity Interface and Trait such as the `ProcessInterface` which we will use to monitor cron, api and file generation. diff --git a/src/Utils/ArrayUtils.php b/src/Utils/ArrayUtils.php index 713889b..fabfec1 100644 --- a/src/Utils/ArrayUtils.php +++ b/src/Utils/ArrayUtils.php @@ -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; } /** diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index 14c0b58..00ea48b 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -357,9 +357,9 @@ public function checkIssetKeysProvider(): array // $keys [0, 1, 5], ], - 'last missing' => [ + 'one missing' => [ // expected - false, + true, // array [ 0 => "000", @@ -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'],