Skip to content

Commit

Permalink
Do not append separator to initial prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Alaa Sarhan committed Sep 27, 2016
1 parent 0543d75 commit b6cbd0a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Flatten
*
* Non-traversable values will be returned as-is, after being put into the final array with the fully-qualified key.
*
* An initial prefix can be optionally to namespace all returned keys using that prefix.
* An initial prefix can be optionally provided, but it will not separated using the separator.
*
* @param mixed $var
* @param string $separator
Expand All @@ -25,8 +25,8 @@ class Flatten
public static function flatten($var, $separator = '.', $prefix = '')
{
$flattened = [];
foreach (self::flattenGenerator($var, $separator, $prefix) as $key => $value) {
$flattened[$key] = $value;
foreach (self::flattenGenerator($var, $separator, '') as $key => $value) {
$flattened[$prefix . $key] = $value;
}
return $flattened;
}
Expand All @@ -36,7 +36,7 @@ private static function flattenGenerator($var, $separator, $prefix = '')
if (self::canTraverse($var)) {
$prefix .= (empty($prefix) ? '' : $separator);
foreach ($var as $key => $value) {
foreach (self::flattenGenerator($value, $separator, $prefix . $key) as $k => $v) {
foreach (self::flattenGenerator($value, $separator, $prefix .$key) as $k => $v) {
yield $k => $v;
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/FlattenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,19 @@ public function testFlattenTraversable($input, $expectedOutput)
public function traversablesSeparatorPrefixProvider()
{
return [
[ new \ArrayIterator([ ]), '-', 'global', [ ] ],
[ new \ArrayIterator([ 0 ]), '-', 'global', [ 'global-0' => 0 ] ],
[ new \ArrayIterator([ 1, 2 ]), '-', 'global', [ 'global-0' => 1, 'global-1' => 2 ] ],
[ new \ArrayIterator([ ]), '-', 'global-', [ ] ],
[ new \ArrayIterator([ 0 ]), '-', 'global-', [ 'global-0' => 0 ] ],
[ new \ArrayIterator([ 1, 2 ]), '-', 'global-', [ 'global-0' => 1, 'global-1' => 2 ] ],
[
new \ArrayIterator([ 1, 2, [ 3, 4 ] ]),
'-',
'global',
'global-',
[ 'global-0' => 1, 'global-1' => 2, 'global-2-0' => 3, 'global-2-1' => 4 ]
],
[
new \ArrayIterator([ 'a' => 1, 2, 'b' => new \ArrayIterator([ 3, 'c' => 4 ]) ]),
'/',
'local',
'local/',
[ 'local/a' => 1, 'local/0' => 2, 'local/b/0' => 3, 'local/b/c' => 4 ]
],
[
Expand Down

0 comments on commit b6cbd0a

Please sign in to comment.