Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add undot() method to Arr helpers and Collections #39729

Merged
merged 4 commits into from Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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