ArrayFlattener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A flatten() 0 4 1
A walkKeyIndexArray() 0 20 3
1
<?php
2
3
namespace holyshared\fixture;
4
5
/**
6
 * This file is part of file-fixture.
7
 *
8
 * (c) Noritaka Horio <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
class ArrayFlattener
15
{
16
17
    /**
18
     * @var \holyshared\fixture\PathRecorder
19
     */
20
    private $currentPath;
21
22
    /**
23
     * @param string $segment
24
     */
25
    public function __construct($segment = ':')
26
    {
27
        $this->currentPath = new PathRecorder($segment);
28
    }
29
30
    /**
31
     * @param array $values
32
     * @return array
33
     */
34
    public function flatten(array $values = [])
35
    {
36
        return $this->walkKeyIndexArray($values);
37
    }
38
39
    /**
40
     * @param array $values
41
     * @return array
42
     */
43
    private function walkKeyIndexArray(array $values = [])
44
    {
45
        $result = [];
46
47
        foreach ($values as $path => $value) {
48
            $this->currentPath->moveTo($path);
49
50
            if (is_array($value)) {
51
                $childResult = $this->walkKeyIndexArray($value);
52
                $result = array_merge($result, $childResult);
53
            } else {
54
                $key = (string) $this->currentPath;
55
                $result[$key] = $value;
56
            }
57
58
            $this->currentPath->moveParent();
59
        }
60
61
        return $result;
62
    }
63
64
}
65