Skip to content

Commit a4777c6

Browse files
author
Alaa Sarhan
committed
Scalars return in array, and can be prefixed
1 parent 81fa457 commit a4777c6

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

src/Flatten.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
namespace Sarhan;
33

4+
/**
5+
* @author Alaa Sarhan <sarhan.alaa@gmail.com>
6+
* @license LGPL
7+
*/
48
class Flatten
59
{
610
public static function flatten($var, $separator = '.', $prefix = '')
711
{
8-
if (!self::canTraverse($var)) {
9-
return $var;
10-
}
11-
1212
$flattened = [];
1313
foreach (self::flattenGenerator($var, $separator, $prefix) as $key => $value) {
1414
$flattened[$key] = $value;

test/FlattenTest.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class FlattenTest extends TestCase
88
public function scalarProvider()
99
{
1010
return [
11-
[ null, null ],
12-
[ '', '' ],
13-
[ 0, 0 ],
14-
[ 3.14, 3.14 ],
15-
[ 'test', 'test' ],
16-
[ false, false ]
11+
[ null, ['' => null] ],
12+
[ '', ['' => ''] ],
13+
[ 0, ['' => 0] ],
14+
[ 3.14, ['' => 3.14] ],
15+
[ 'test', ['' => 'test'] ],
16+
[ false, ['' => false] ]
1717
];
1818
}
1919

@@ -28,6 +28,29 @@ public function testFlattenScalar($input, $expectedOutput)
2828
$this->assertEquals($expectedOutput, $output);
2929
}
3030

31+
public function scalarSeparatorPrefixProvider()
32+
{
33+
return [
34+
[ null, '-', ':', [':' => null] ],
35+
[ '', '', '/', ['/' => ''] ],
36+
[ 0, '.', 'global', ['global' => 0] ],
37+
[ 3.14, '', 'local', ['local' => 3.14] ],
38+
[ 'test', 'sep', '', ['' => 'test'] ],
39+
[ false, '', '_', ['_' => false] ]
40+
];
41+
}
42+
43+
/**
44+
* @covers Flatten::flatten
45+
* @dataProvider scalarSeparatorPrefixProvider
46+
*/
47+
public function testFlattenScalarWithSeparatorAndPrefix($var, $separator, $prefix, $expectedOutput)
48+
{
49+
$output = Flatten::flatten($var, $separator, $prefix);
50+
51+
$this->assertEquals($expectedOutput, $output);
52+
}
53+
3154
public function arraysProvider()
3255
{
3356
return [

0 commit comments

Comments
 (0)