-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathFlatArrayTransformer.php
45 lines (40 loc) · 999 Bytes
/
FlatArrayTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* Author: Nil Portugués Calderó <[email protected]>
* Date: 8/29/15
* Time: 2:48 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Serializer\Transformer;
class FlatArrayTransformer extends ArrayTransformer
{
/**
* @param mixed $value
*
* @return string
*/
public function serialize($value)
{
return $this->flatten(parent::serialize($value));
}
/**
* @param array $array
* @param string $prefix
*
* @return array
*/
private function flatten(array $array, $prefix = '')
{
$result = [];
foreach ($array as $key => $value) {
if (\is_array($value)) {
$result = $result + $this->flatten($value, $prefix.$key.'.');
} else {
$result[$prefix.$key] = $value;
}
}
return $result;
}
}