Completed
Push — master ( 571837...d3453b )
by Vincent
04:28 queued 02:28
created

Walker::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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