Skip to content

Commit 63df11b

Browse files
committed
Se agrega método Arr::nested() como el contrario de Arr:dot() y sus tests.
1 parent ad31343 commit 63df11b

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

src/Arr.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ public static function dot(array $array, string $prefix = ''): array
4747
return $result;
4848
}
4949

50+
/**
51+
* Converts a flat array with dot notation keys back to a multi-dimensional
52+
* array.
53+
*
54+
* @param array $array The flat array with dot notation keys.
55+
* @return array The multi-dimensional array.
56+
*/
57+
public static function nested(array $array): array
58+
{
59+
$result = [];
60+
foreach ($array as $key => $value) {
61+
if (str_contains($key, '.')) {
62+
$keys = explode('.', $key);
63+
$current = &$result;
64+
65+
foreach ($keys as $k) {
66+
if (!isset($current[$k]) || !is_array($current[$k])) {
67+
$current[$k] = [];
68+
}
69+
$current = &$current[$k];
70+
}
71+
72+
$current = $value;
73+
} else {
74+
$result[$key] = $value;
75+
}
76+
}
77+
78+
return $result;
79+
}
80+
5081
/**
5182
* Recursively casts array values automatically based on their content.
5283
*

tests/src/Arr/ArrDotNestedTest.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Derafu: Support - Essential PHP Utilities.
7+
*
8+
* Copyright (c) 2025 Esteban De La Fuente Rubio / Derafu <https://www.derafu.dev>
9+
* Licensed under the MIT License.
10+
* See LICENSE file for more details.
11+
*/
12+
13+
namespace Derafu\TestsSupport\Arr;
14+
15+
use Derafu\Support\Arr;
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\Attributes\DataProvider;
18+
use PHPUnit\Framework\Attributes\Test;
19+
use PHPUnit\Framework\TestCase;
20+
21+
#[CoversClass(Arr::class)]
22+
class ArrDotNestedTest extends TestCase
23+
{
24+
#[Test]
25+
#[DataProvider('provideDotData')]
26+
public function shouldFlattenArrayWithDotNotation(
27+
array $input,
28+
string $prefix,
29+
array $expected
30+
): void {
31+
$result = Arr::dot($input, $prefix);
32+
$this->assertSame($expected, $result);
33+
}
34+
35+
public static function provideDotData(): array
36+
{
37+
return [
38+
'simple nested array' => [
39+
['a' => ['b' => 'c']],
40+
'',
41+
['a.b' => 'c'],
42+
],
43+
'with prefix' => [
44+
['a' => ['b' => 'c']],
45+
'x',
46+
['x.a.b' => 'c'],
47+
],
48+
'flat array' => [
49+
['a' => 1, 'b' => 2],
50+
'',
51+
['a' => 1, 'b' => 2],
52+
],
53+
'empty array' => [
54+
[],
55+
'',
56+
[],
57+
],
58+
];
59+
}
60+
61+
#[Test]
62+
#[DataProvider('provideNestedData')]
63+
public function shouldConvertDotNotationToNestedArray(
64+
array $input,
65+
array $expected
66+
): void {
67+
$result = Arr::nested($input);
68+
$this->assertSame($expected, $result);
69+
}
70+
71+
public static function provideNestedData(): array
72+
{
73+
return [
74+
'simple dot notation' => [
75+
['a.b' => 'c'],
76+
['a' => ['b' => 'c']],
77+
],
78+
'flat array' => [
79+
['a' => 1, 'b' => 2],
80+
['a' => 1, 'b' => 2],
81+
],
82+
'empty array' => [
83+
[],
84+
[],
85+
],
86+
];
87+
}
88+
89+
#[Test]
90+
#[DataProvider('provideCycleData')]
91+
public function shouldCompleteCycleDotToNestedAndBack(
92+
array $original
93+
): void {
94+
$dot = Arr::dot($original);
95+
$nested = Arr::nested($dot);
96+
$this->assertSame($original, $nested);
97+
}
98+
99+
public static function provideCycleData(): array
100+
{
101+
return [
102+
'simple nested structure' => [
103+
['a' => ['b' => 'c']],
104+
],
105+
'flat array' => [
106+
['a' => 1, 'b' => 2],
107+
],
108+
'empty array' => [
109+
[],
110+
],
111+
];
112+
}
113+
}

0 commit comments

Comments
 (0)