From eec2b20e45c7796c84fdced751d6deab6ef44478 Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Tue, 5 Nov 2024 18:07:47 +0100 Subject: [PATCH 1/3] Add `ArrayUtils::toIndexedArray` --- CHANGELOG_add_array_utils.md | 2 ++ src/Utils/ArrayUtils.php | 24 ++++++++++++++++++++++++ tests/Utils/ArrayUtilsTest.php | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 CHANGELOG_add_array_utils.md diff --git a/CHANGELOG_add_array_utils.md b/CHANGELOG_add_array_utils.md new file mode 100644 index 0000000..e0548e9 --- /dev/null +++ b/CHANGELOG_add_array_utils.md @@ -0,0 +1,2 @@ +### Added +- `ArrayUtils::toIndexedArray` + tests Delete keys of array and multidimensional array \ No newline at end of file diff --git a/src/Utils/ArrayUtils.php b/src/Utils/ArrayUtils.php index ded0273..d8f510e 100644 --- a/src/Utils/ArrayUtils.php +++ b/src/Utils/ArrayUtils.php @@ -272,4 +272,28 @@ public static function hasDuplicateValue(array $array): bool { return count($array) !== count(array_flip($array)); } + + /** + * Delete keys of array and multidimensional array + * + *
+     *   1, 'doe' => ['smart' => 100, 'booster' => 200]);
+     *  ?>
+     *  
+ * The above example will output: + *
+     *  [1, [100, 200]]
+     *  
+ */ + public static function toIndexedArray(array $array): array + { + foreach ($array as $key => $value) { + if (is_array($value)) { + $array[$key] = self::toIndexedArray($value); + } + } + + return array_values($array); + } } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index e5e9d3b..98c45c8 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -606,4 +606,22 @@ public function hasDuplicateProvider(): array ], ]; } + + /** + * @dataProvider toIndexedArrayProvider + */ + public function testToIndexedArray(array $expected, array $array): void + { + $this->assertSame($expected, ArrayUtils::toIndexedArray($array)); + } + + public function toIndexedArrayProvider(): array + { + return [ + 'simple_array_indexed' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]], + 'simple_array_not_indexed' => [[1, 2, 3], [1, 2, 3]], + 'multidimensional_array_indexed' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]], + 'multidimensional_array_not_indexed' => [[[1, 2], [3, 4]], [[1, 2], [3, 4]]], + ]; + } } From 78f77cdaa6968d98cdd0d78bed9378f9d79147e9 Mon Sep 17 00:00:00 2001 From: Louis Fortunier Date: Tue, 5 Nov 2024 18:08:25 +0100 Subject: [PATCH 2/3] Fix qa on DateUtils::getMonthChoices --- src/Utils/DateUtils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Utils/DateUtils.php b/src/Utils/DateUtils.php index 963cc45..c7ed9d8 100644 --- a/src/Utils/DateUtils.php +++ b/src/Utils/DateUtils.php @@ -43,7 +43,7 @@ public static function getMonthChoices(string $locale = 'fr_FR'): array { $formatter = new \IntlDateFormatter(locale: $locale, dateType: \IntlDateFormatter::FULL, timeType: \IntlDateFormatter::FULL, pattern: 'MMMM'); for ($i = 1; $i <= 12; $i++) { - $month = $formatter->format(strtotime("2000-$i")); // @phpstan-ignore-line MDT the year is arbitrary and does not impact the month + $month = $formatter->format(strtotime("2000-$i")); // MDT the year is arbitrary and does not impact the month $toReturn[ucfirst((string) $month)] = $i; } From e6d7612231cc2c813a2b92a94b4b04dbf60bb5d8 Mon Sep 17 00:00:00 2001 From: Mathieu Ducrot Date: Wed, 13 Nov 2024 11:44:13 +0100 Subject: [PATCH 3/3] Fix ArrayUtilsTest::toIndexedArrayProvider case tests keys --- tests/Utils/ArrayUtilsTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index 98c45c8..f1a3082 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -618,10 +618,10 @@ public function testToIndexedArray(array $expected, array $array): void public function toIndexedArrayProvider(): array { return [ - 'simple_array_indexed' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]], - 'simple_array_not_indexed' => [[1, 2, 3], [1, 2, 3]], - 'multidimensional_array_indexed' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]], - 'multidimensional_array_not_indexed' => [[[1, 2], [3, 4]], [[1, 2], [3, 4]]], + 'simple_associative_array' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]], + 'simple_indexed_array_no_chagnes' => [[1, 2, 3], [1, 2, 3]], + 'multidimensional_associative_array' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]], + 'multidimensional_indexed_array_no_changes' => [[[1, 2], [3, 4]], [[1, 2], [3, 4]]], ]; } }