From 44b9091e73a6a576f726147ae9255523256bb5e6 Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Tue, 11 Jun 2024 13:43:02 -0400 Subject: [PATCH 1/2] Fix bug causing undot to always return empty array --- src/Arrays/Arr.php | 2 +- tests/wpunit/UndotTest.php | 105 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 tests/wpunit/UndotTest.php diff --git a/src/Arrays/Arr.php b/src/Arrays/Arr.php index 8045561..d70b5c1 100644 --- a/src/Arrays/Arr.php +++ b/src/Arrays/Arr.php @@ -1317,7 +1317,7 @@ public static function undot( $array ) { $results = []; foreach ( $array as $key => $value ) { - static::set( $results, $key, $value ); + $results = static::set( $results, explode( '.', $key ), $value ); } return $results; diff --git a/tests/wpunit/UndotTest.php b/tests/wpunit/UndotTest.php new file mode 100644 index 0000000..eba1d1d --- /dev/null +++ b/tests/wpunit/UndotTest.php @@ -0,0 +1,105 @@ + 'fibonacci', + ]; + + $expected = [ + 'one' => [ + 'two' => [ + 'three' => [ + 'five' => [ + 'eight' => 'fibonacci' + ] + ] + ] + ], + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } + + /** + * @test + */ + public function it_expands_array_with_leaves() + { + $dotted = [ + 'one.first_leaf' => true, + 'one.two.second_leaf' => true, + 'one.two.three.third_leaf' => true, + 'one.two.three.five.fifth_leaf' => true, + 'one.two.three.five.eight' => 'fibonacci', + ]; + + $expected = [ + 'one' => [ + 'first_leaf' => true, + 'two' => [ + 'second_leaf' => true, + 'three' => [ + 'third_leaf' => true, + 'five' => [ + 'fifth_leaf' => true, + 'eight' => 'fibonacci' + ] + ] + ] + ], + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } + + /** + * @test + */ + public function it_expands_nested_arrays_with_numerical_keys() + { + $dotted = [ + 'first_array.0' => 'bacon', + 'first_array.1' => 'ham', + 'first_array.2' => 'cheese', + 'second_array.0' => 'bacon', + 'second_array.1' => 'egg', + 'second_array.2' => 'cheese', + ]; + + $expected = [ + 'first_array' => [ + 'bacon', + 'ham', + 'cheese', + ], + 'second_array' => [ + 'bacon', + 'egg', + 'cheese' + ] + ]; + + $this->assertSame($expected, Arr::undot($dotted)); + } +} From 429b2d0c5f2f1f47700e4cb0f15cbb61b95b576b Mon Sep 17 00:00:00 2001 From: Tanner Record Date: Tue, 11 Jun 2024 13:47:12 -0400 Subject: [PATCH 2/2] Fix really weird spacing --- tests/wpunit/UndotTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/wpunit/UndotTest.php b/tests/wpunit/UndotTest.php index eba1d1d..25218f2 100644 --- a/tests/wpunit/UndotTest.php +++ b/tests/wpunit/UndotTest.php @@ -27,15 +27,15 @@ public function it_properly_expands_array() ]; $expected = [ - 'one' => [ - 'two' => [ - 'three' => [ - 'five' => [ - 'eight' => 'fibonacci' - ] - ] - ] - ], + 'one' => [ + 'two' => [ + 'three' => [ + 'five' => [ + 'eight' => 'fibonacci' + ] + ] + ] + ], ]; $this->assertSame($expected, Arr::undot($dotted));