Skip to content

Commit b6cbd0a

Browse files
author
Alaa Sarhan
committed
Do not append separator to initial prefix
1 parent 0543d75 commit b6cbd0a

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Flatten.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Flatten
1515
*
1616
* Non-traversable values will be returned as-is, after being put into the final array with the fully-qualified key.
1717
*
18-
* An initial prefix can be optionally to namespace all returned keys using that prefix.
18+
* An initial prefix can be optionally provided, but it will not separated using the separator.
1919
*
2020
* @param mixed $var
2121
* @param string $separator
@@ -25,8 +25,8 @@ class Flatten
2525
public static function flatten($var, $separator = '.', $prefix = '')
2626
{
2727
$flattened = [];
28-
foreach (self::flattenGenerator($var, $separator, $prefix) as $key => $value) {
29-
$flattened[$key] = $value;
28+
foreach (self::flattenGenerator($var, $separator, '') as $key => $value) {
29+
$flattened[$prefix . $key] = $value;
3030
}
3131
return $flattened;
3232
}
@@ -36,7 +36,7 @@ private static function flattenGenerator($var, $separator, $prefix = '')
3636
if (self::canTraverse($var)) {
3737
$prefix .= (empty($prefix) ? '' : $separator);
3838
foreach ($var as $key => $value) {
39-
foreach (self::flattenGenerator($value, $separator, $prefix . $key) as $k => $v) {
39+
foreach (self::flattenGenerator($value, $separator, $prefix .$key) as $k => $v) {
4040
yield $k => $v;
4141
}
4242
}

test/FlattenTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,19 @@ public function testFlattenTraversable($input, $expectedOutput)
117117
public function traversablesSeparatorPrefixProvider()
118118
{
119119
return [
120-
[ new \ArrayIterator([ ]), '-', 'global', [ ] ],
121-
[ new \ArrayIterator([ 0 ]), '-', 'global', [ 'global-0' => 0 ] ],
122-
[ new \ArrayIterator([ 1, 2 ]), '-', 'global', [ 'global-0' => 1, 'global-1' => 2 ] ],
120+
[ new \ArrayIterator([ ]), '-', 'global-', [ ] ],
121+
[ new \ArrayIterator([ 0 ]), '-', 'global-', [ 'global-0' => 0 ] ],
122+
[ new \ArrayIterator([ 1, 2 ]), '-', 'global-', [ 'global-0' => 1, 'global-1' => 2 ] ],
123123
[
124124
new \ArrayIterator([ 1, 2, [ 3, 4 ] ]),
125125
'-',
126-
'global',
126+
'global-',
127127
[ 'global-0' => 1, 'global-1' => 2, 'global-2-0' => 3, 'global-2-1' => 4 ]
128128
],
129129
[
130130
new \ArrayIterator([ 'a' => 1, 2, 'b' => new \ArrayIterator([ 3, 'c' => 4 ]) ]),
131131
'/',
132-
'local',
132+
'local/',
133133
[ 'local/a' => 1, 'local/0' => 2, 'local/b/0' => 3, 'local/b/c' => 4 ]
134134
],
135135
[

0 commit comments

Comments
 (0)