Completed
Push — master ( 54876d...8e7d36 )
by Hong
03:06
created

Tree::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Shared
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Shared\Tree;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
19
/**
20
 * Dealing with tree structure
21
 *
22
 * @package Phossa2\Shared
23
 * @author  Hong Zhang <[email protected]>
24
 * @version 2.0.3
25
 * @since   2.0.3 added
26
 */
27
class Tree extends ObjectAbstract
28
{
29
    /**
30
     * node splitter
31
     *
32
     * @var    string
33
     * @access protected
34
     */
35
    protected $splitter = '.';
36
37
    /**
38
     * the result tree
39
     *
40
     * @var    array
41
     * @access protected
42
     */
43
    protected $tree;
44
45
    /**
46
     * construct a tree
47
     *
48
     * @param  array $data
49
     * @param  string $splitter
50
     * @access public
51
     */
52
    public function __construct(array $data, /*# string */ $splitter = '.')
53
    {
54
        $this->splitter = $splitter;
55
        $this->tree = $this->fixTree($data);
56
    }
57
58
    /**
59
     * return the whole tree
60
     *
61
     * @return array
62
     * @access public
63
     */
64
    public function getTree()/*# : array */
65
    {
66
        return $this->tree;
67
    }
68
69
    /**
70
     * Get one node, NULL if not found
71
     *
72
     * @param  string $nodeName
73
     * @return mixed
74
     * @access public
75
     */
76
    public function getNode(/*# string */ $nodeName)
77
    {
78
        $result = &$this->searchNode($nodeName, $this->tree, false);
79
        return $result;
80
    }
81
82
    /**
83
     * Fix array, convert flat name to tree node name
84
     *
85
     * @param  array $data
86
     * @return array
87
     * @access protected
88
     */
89
    protected function fixTree(array $data)/*# : array */
90
    {
91
        $result = [];
92
        foreach ($data as $k => $v) {
93
            $res = &$this->searchNode($k, $result);
94
            $res = is_array($v) ? $this->fixValue($v) : $v;
0 ignored issues
show
Bug introduced by
The method fixValue() does not seem to exist on object<Phossa2\Shared\Tree\Tree>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        }
96
        return $result;
97
    }
98
99
    /**
100
     * Search a node in the $data
101
     *
102
     * @param  string $key
103
     * @param  array &$data
104
     * @param  bool $create create the node if not exist
105
     * @access protected
106
     */
107
    protected function &searchNode(
108
        /*# string */ $key,
109
        array &$data,
110
        /*# bool */ $create = true
111
    ) {
112
        $found = &$data;
113
        foreach (explode($this->splitter, $key) as $k) {
114
            if (isset($found[$k])) {
115
                $found = &$found[$k];
116
            } elseif ($create) {
117
                $found[$k] = [];
118
                $found = &$found[$k];
119
            } else {
120
                $found = null;
121
                break;
122
            }
123
        }
124
        return $found;
125
    }
126
}
127