From 668bcd30dc397de29bee90e1279dc2239111d681 Mon Sep 17 00:00:00 2001 From: Amir Date: Tue, 23 Nov 2021 10:35:19 +0100 Subject: [PATCH 1/4] Add elevate() method to Arr helpers and Collections --- src/Illuminate/Collections/Arr.php | 17 +++++++++ src/Illuminate/Collections/Collection.php | 10 ++++++ src/Illuminate/Collections/LazyCollection.php | 10 ++++++ tests/Support/SupportArrTest.php | 24 +++++++++++++ tests/Support/SupportCollectionTest.php | 36 +++++++++++++++++++ 5 files changed, 97 insertions(+) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d4ebd89900a2..f7dc5194df5e 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -121,6 +121,23 @@ public static function dot($array, $prepend = '') return $results; } + /** + * Elevate an associative array with dots to a multi-dimensional array. + * + * @param iterable $array + * @return array + */ + public static function elevate($array) + { + $results = []; + + foreach ($array as $key => $value) { + static::set($results, $key, $value); + } + + return $results; + } + /** * Get all of the given array except for a specified array of keys. * diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 6435b87ca64d..6a48d3133e8e 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1547,6 +1547,16 @@ public function add($item) return $this; } + /** + * Elevate the collection to one that holds a multi-dimensional array. + * + * @return static + */ + public function elevate() + { + return new static(Arr::elevate($this->all())); + } + /** * Get a base Support collection instance from this collection. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 70f107061898..132775ecbbea 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1464,6 +1464,16 @@ public function pad($size, $value) }); } + /** + * Elevate the lazy collection to one that holds a multi-dimensional array. + * + * @return static + */ + public function elevate() + { + return $this->passthru('elevate', []); + } + /** * Get the values iterator. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index c38099cb2137..ae7959863601 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -113,6 +113,30 @@ public function testDot() $this->assertEquals(['name' => 'taylor', 'languages.php' => true], $array); } + public function testElevate() + { + $array = Arr::elevate([ + 'user.name' => 'Taylor', + 'user.age' => 25, + 'user.languages.0' => 'PHP', + 'user.languages.1' => 'C#', + ]); + $this->assertEquals(['user' => ['name' => 'Taylor', 'age' => 25, 'languages' => ['PHP', 'C#']]], $array); + + $array = Arr::elevate([ + 'pagination.previous' => '<<', + 'pagination.next' => '>>', + ]); + $this->assertEquals(['pagination' => ['previous' => '<<', 'next' => '>>']], $array); + + $array = Arr::elevate([ + 'foo', + 'foo.bar' => 'baz', + 'foo.baz' => ['a' => 'b'], + ]); + $this->assertEquals(['foo', 'foo' => ['bar' => 'baz', 'baz' => ['a' => 'b']]], $array); + } + public function testExcept() { $array = ['name' => 'taylor', 'age' => 26]; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 61727ce9ccc8..803bccba6625 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -4855,6 +4855,42 @@ public function testCollect($collection) ], $data->all()); } + /** + * @dataProvider collectionClassProvider + */ + public function testElevate($collection) + { + $data = $collection::make([ + 'name' => 'Taylor', + 'meta.foo' => 'bar', + 'meta.baz' => 'boom', + 'meta.bam.boom' => 'bip', + ])->elevate(); + $this->assertSame([ + 'name' => 'Taylor', + 'meta' => [ + 'foo' => 'bar', + 'baz' => 'boom', + 'bam' => [ + 'boom' => 'bip', + ], + ], + ], $data->all()); + + $data = $collection::make([ + 'foo.0' => 'bar', + 'foo.1' => 'baz', + 'foo.baz' => 'boom', + ])->elevate(); + $this->assertSame([ + 'foo' => [ + 'bar', + 'baz', + 'baz' => 'boom', + ], + ], $data->all()); + } + /** * Provides each collection class, respectively. * From 88d48857b9500596cecedf07564da4c6a614bc2a Mon Sep 17 00:00:00 2001 From: Amir Date: Wed, 24 Nov 2021 15:36:25 +0100 Subject: [PATCH 2/4] Fix CS --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index f7dc5194df5e..eb169f368d19 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -124,7 +124,7 @@ public static function dot($array, $prepend = '') /** * Elevate an associative array with dots to a multi-dimensional array. * - * @param iterable $array + * @param iterable $array * @return array */ public static function elevate($array) From d66365267b224595d1025c78d1c3fd29514393d5 Mon Sep 17 00:00:00 2001 From: Amir Date: Wed, 24 Nov 2021 15:37:15 +0100 Subject: [PATCH 3/4] Fix CS --- src/Illuminate/Collections/Arr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index eb169f368d19..4366d207789b 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -124,7 +124,7 @@ public static function dot($array, $prepend = '') /** * Elevate an associative array with dots to a multi-dimensional array. * - * @param iterable $array + * @param iterable $array * @return array */ public static function elevate($array) From 0c0f4869cbecf8ee036fb8dd35713b953819b930 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 26 Nov 2021 09:42:28 -0600 Subject: [PATCH 4/4] rename to undot --- src/Illuminate/Collections/Arr.php | 4 ++-- src/Illuminate/Collections/Collection.php | 20 +++++++++---------- src/Illuminate/Collections/LazyCollection.php | 20 +++++++++---------- tests/Support/SupportArrTest.php | 8 ++++---- tests/Support/SupportCollectionTest.php | 6 +++--- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index 4366d207789b..1ce093079f64 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -122,12 +122,12 @@ public static function dot($array, $prepend = '') } /** - * Elevate an associative array with dots to a multi-dimensional array. + * Convert a flatten "dot" notation array into an expanded array. * * @param iterable $array * @return array */ - public static function elevate($array) + public static function undot($array) { $results = []; diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 6a48d3133e8e..33b5b5de78f6 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -1435,6 +1435,16 @@ public function transform(callable $callback) return $this; } + /** + * Convert a flatten "dot" notation array into an expanded array. + * + * @return static + */ + public function undot() + { + return new static(Arr::undot($this->all())); + } + /** * Return only unique items from the collection array. * @@ -1547,16 +1557,6 @@ public function add($item) return $this; } - /** - * Elevate the collection to one that holds a multi-dimensional array. - * - * @return static - */ - public function elevate() - { - return new static(Arr::elevate($this->all())); - } - /** * Get a base Support collection instance from this collection. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 132775ecbbea..f0f2a8de20a2 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1372,6 +1372,16 @@ public function tapEach(callable $callback) }); } + /** + * Convert a flatten "dot" notation array into an expanded array. + * + * @return static + */ + public function undot() + { + return $this->passthru('undot', []); + } + /** * Return only unique items from the collection array. * @@ -1464,16 +1474,6 @@ public function pad($size, $value) }); } - /** - * Elevate the lazy collection to one that holds a multi-dimensional array. - * - * @return static - */ - public function elevate() - { - return $this->passthru('elevate', []); - } - /** * Get the values iterator. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index ae7959863601..e34f469c848d 100644 --- a/tests/Support/SupportArrTest.php +++ b/tests/Support/SupportArrTest.php @@ -113,9 +113,9 @@ public function testDot() $this->assertEquals(['name' => 'taylor', 'languages.php' => true], $array); } - public function testElevate() + public function testUndot() { - $array = Arr::elevate([ + $array = Arr::undot([ 'user.name' => 'Taylor', 'user.age' => 25, 'user.languages.0' => 'PHP', @@ -123,13 +123,13 @@ public function testElevate() ]); $this->assertEquals(['user' => ['name' => 'Taylor', 'age' => 25, 'languages' => ['PHP', 'C#']]], $array); - $array = Arr::elevate([ + $array = Arr::undot([ 'pagination.previous' => '<<', 'pagination.next' => '>>', ]); $this->assertEquals(['pagination' => ['previous' => '<<', 'next' => '>>']], $array); - $array = Arr::elevate([ + $array = Arr::undot([ 'foo', 'foo.bar' => 'baz', 'foo.baz' => ['a' => 'b'], diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 803bccba6625..28f05639a4ab 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -4858,14 +4858,14 @@ public function testCollect($collection) /** * @dataProvider collectionClassProvider */ - public function testElevate($collection) + public function testUndot($collection) { $data = $collection::make([ 'name' => 'Taylor', 'meta.foo' => 'bar', 'meta.baz' => 'boom', 'meta.bam.boom' => 'bip', - ])->elevate(); + ])->undot(); $this->assertSame([ 'name' => 'Taylor', 'meta' => [ @@ -4881,7 +4881,7 @@ public function testElevate($collection) 'foo.0' => 'bar', 'foo.1' => 'baz', 'foo.baz' => 'boom', - ])->elevate(); + ])->undot(); $this->assertSame([ 'foo' => [ 'bar',