Skip to content

Commit

Permalink
[8.x] Add undot() method to Arr helpers and Collections (#39729)
Browse files Browse the repository at this point in the history
* Add elevate() method to Arr helpers and Collections

* Fix CS

* Fix CS

* rename to undot

Co-authored-by: Amir <a.rami@adaptech.me>
Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
3 people committed Nov 26, 2021
1 parent 75bf358 commit 78637dc
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Collections/Arr.php
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Collections/Collection.php
Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Support/SupportArrTest.php
Expand Up @@ -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];
Expand Down
36 changes: 36 additions & 0 deletions tests/Support/SupportCollectionTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 78637dc

Please sign in to comment.