diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d4ebd89900a2..1ce093079f64 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; } + /** + * Convert a flatten "dot" notation array into an expanded array. + * + * @param iterable $array + * @return array + */ + public static function undot($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..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. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 70f107061898..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. * diff --git a/tests/Support/SupportArrTest.php b/tests/Support/SupportArrTest.php index c38099cb2137..e34f469c848d 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 testUndot() + { + $array = Arr::undot([ + '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::undot([ + 'pagination.previous' => '<<', + 'pagination.next' => '>>', + ]); + $this->assertEquals(['pagination' => ['previous' => '<<', 'next' => '>>']], $array); + + $array = Arr::undot([ + '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..28f05639a4ab 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 testUndot($collection) + { + $data = $collection::make([ + 'name' => 'Taylor', + 'meta.foo' => 'bar', + 'meta.baz' => 'boom', + 'meta.bam.boom' => 'bip', + ])->undot(); + $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', + ])->undot(); + $this->assertSame([ + 'foo' => [ + 'bar', + 'baz', + 'baz' => 'boom', + ], + ], $data->all()); + } + /** * Provides each collection class, respectively. *