Completed
Push — master ( e34852...9af992 )
by Vincent
01:50
created

Walker   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
dl 0
loc 112
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0
wmc 17

9 Methods

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