Walker::asArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Walker;
4
5
/**
6
 * Class Walker
7
 */
8
class Walker
9
{
10
    /**
11
     * @var array
12
     */
13
    private $dataEntryPoint;
14
    /**
15
     * @var array
16
     */
17
    private $founds = [];
18
    /**
19
     * @var array
20
     */
21
    private $targets = [];
22
23
    /**
24
     * @param array $founds
25
     */
26 8
    private function setFounds(array $founds) : void
27
    {
28 8
        $this->founds = $founds;
29 8
    }
30
31
    /**
32
     * @param string $json
33
     * @return Walker
34
     */
35 1
    public function fromJson(string $json) : self
36
    {
37 1
        $this->setFounds([]);
38 1
        $this->dataEntryPoint = json_decode($json);
39
40 1
        return $this;
41
    }
42
43
    /**
44
     * @param array $rawData
45
     * @return Walker
46
     */
47 7
    public function from(array $rawData) : self
48
    {
49 7
        $this->setFounds([]);
50 7
        $this->dataEntryPoint = $rawData;
51
52 7
        return $this;
53
    }
54
55
    /**
56
     * @param $target
57
     * @return Walker
58
     */
59 8
    public function with($target) : self
60
    {
61 8
        $this->targets[] = $target;
62
63 8
        return $this;
64
    }
65
66
    /**
67
     * @param callable|null $formatter
68
     * @return string
69
     */
70 6
    public function asString(callable $formatter = null) : string
71
    {
72 6
        $this->run();
73
74 6
        return (is_null($formatter)) ? implode(', ', $this->founds) : $formatter($this->founds);
75
    }
76
77
    /**
78
     * @return array
79
     */
80 3
    public function asArray() : array
81
    {
82 3
        $this->run();
83
84 3
        return $this->founds;
85
    }
86
87
    /**
88
     * @param $target
89
     * @return array
90
     */
91 8
    protected function parseTarget($target) : array
92
    {
93 8
        $separator = '->';
94
95 8
        return explode($separator, $target);
96
    }
97
98
    /**
99
     * @param $node
100
     * @param array $matches
101
     * @return void
102
     */
103 8
    protected function walk($node, array $matches) : void
104
    {
105 8
        if (is_object($node)) {
106 6
            $match = array_shift($matches);
107 6
            if (property_exists($node, $match)) {
108 6
                if(count($matches) === 0 && isset($node->$match)) {
109 6
                    $this->founds[] = $node->$match;
110
111 6
                    return;
112
                }
113 6
                $this->walk($node->$match, $matches);
114
            }
115 7
        } elseif (is_array($node)) {
116 6
            foreach ($node as $n) {
117 5
                $this->walk($n, $matches);
118
            }
119
        }
120 8
    }
121
122 9
    private function run() : void
123
    {
124 9
        foreach ($this->targets as $target) {
125 8
            $this->walk($this->dataEntryPoint, $this->parseTarget($target));
126
        }
127 9
    }
128
}
129