Skip to content

Commit a7e5338

Browse files
committed
add Flatten::unflattenToArray for convinience
1 parent cb6d503 commit a7e5338

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/Flatten.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct(
5151
}
5252

5353
/**
54-
* Flattens a traversable or array into a 1-dimensional array.
54+
* Flattens an iterable into a 1-dimensional generator.
5555
*
5656
* Each key (fully-qualified key or FQK) in the returned one-dimensional array is the join of all keys leading to
5757
* each (non-traversable) value, in all dimensions, separated by the configured separator.
@@ -68,18 +68,33 @@ public function flatten($var)
6868
}
6969
}
7070

71+
/**
72+
* Unflattens a 1-dimensional iterable into a multi-dimensional generator.
73+
*
74+
* Fully Qualitifed Keys (FQks) in the input array will be split by the
75+
* configured separator, and resulting splits will form keys for each level
76+
* down the resulting multi-dimensional array.
77+
*
78+
* The configured prefix will be removed from FQKs in the input array first.
79+
*
80+
* The resulting generator can be recursively converted into a final array
81+
* using the utility class `TraversableToArray` which accounts for the fact
82+
* that generatos may yield same key with different values and combine these
83+
* values into an array under that key as expected.
84+
*
85+
* @param mixed $var
86+
* @return multi-dimensional generator.
87+
* @see Util\TraversableToArray
88+
* @see unflattenToArray
89+
*/
7190
public function unflatten($var)
7291
{
7392
if (!$this->canTraverse($var)) {
7493
yield $var;
7594
}
7695

7796
foreach ($var as $key => $value) {
78-
if (!empty($this->prefix)
79-
&& substr($key, 0, strlen($this->prefix)) === $this->prefix
80-
) {
81-
$key = substr($key, strlen($this->prefix));
82-
}
97+
$key = substr($key, strlen($this->prefix));
8398

8499
if (!empty($key)) {
85100
foreach ($this->unflattenGenerator($key, $value) as $k => $v) {
@@ -97,6 +112,18 @@ public function unflatten($var)
97112
}
98113
}
99114

115+
/**
116+
* Unflattens a 1-dimensional iterable into a multi-dimensional array.
117+
*
118+
* @param mixed $var
119+
* @return array
120+
* @see flatten
121+
*/
122+
public function unflattenToArray($var)
123+
{
124+
return Util\TraversableToArray::toArray($this->unflatten($var));
125+
}
126+
100127
private function flattenGenerator($var, $prefix)
101128
{
102129
if (!$this->canTraverse($var)) {

test/UnflattenTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,6 @@ private function unflattenToArray(
242242
$prefix = Flatten::DEFAULT_PREFIX,
243243
$flags = Flatten::DEFAULT_FLAGS
244244
) {
245-
return TraversableToArray::toArray((new Flatten($separator, $prefix, $flags))->unflatten($input));
245+
return (new Flatten($separator, $prefix, $flags))->unflattenToArray($input);
246246
}
247247
}

0 commit comments

Comments
 (0)