Completed
Push — master ( 9af992...6b9ba2 )
by Vincent
02:00
created

Walker::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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