Container   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 104
ccs 52
cts 52
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 8 4
A all() 0 4 1
A get() 0 12 2
A has() 0 6 1
A set() 0 11 1
A delete() 0 13 2
A reset() 0 6 1
A buildTree() 0 14 2
A parsePath() 0 17 2
1
<?php
2
/**
3
 * (c) 2017 Marcos Sader.
4
 *
5
 * For the full copyright and license information, please view
6
 * the LICENSE file that was distributed with this source code.
7
 */
8
9
namespace xmarcos\Dot;
10
11
use ArrayAccess;
12
use ArrayObject;
13
14
class Container extends ArrayObject
15
{
16
    const PATH_SEPARATOR = '.';
17
18 10
    public static function create($data = null)
19
    {
20 10
        if (is_array($data) || (is_object($data) && $data instanceof ArrayAccess)) {
21 10
            return new static($data);
22
        }
23
24 2
        return new static();
25
    }
26
27 4
    public function all()
28
    {
29 4
        return $this->getArrayCopy();
30
    }
31
32 7
    public function get($path, $default = null)
33
    {
34 7
        $keys = $this->parsePath($path);
35
36 7
        return array_reduce(
37 7
            $keys,
38 7
            function ($acc, $key) use ($default) {
39 7
                return isset($acc[$key]) ? $acc[$key] : $default;
40 7
            },
41 7
            $this->getArrayCopy()
42
        );
43
    }
44
45 3
    public function has($path)
46
    {
47 3
        $control = md5(uniqid());
48
49 3
        return $this->get($path, $control) !== $control;
50
    }
51
52 4
    public function set($path, $value = null)
53
    {
54 4
        $this->exchangeArray(
55 4
            array_replace_recursive(
56 4
                $this->getArrayCopy(),
57 4
                $this->buildTree($path, $value)
58
            )
59
        );
60
61 4
        return $this;
62
    }
63
64 2
    public function delete($path)
65
    {
66 2
        if ($this->has($path)) {
67 2
            $this->exchangeArray(
68 2
                array_replace_recursive(
69 2
                    $this->getArrayCopy(),
70 2
                    $this->buildTree($path, null)
71
                )
72
            );
73
        }
74
75 2
        return $this;
76
    }
77
78 1
    public function reset()
79
    {
80 1
        $this->exchangeArray([]);
81
82 1
        return $this;
83
    }
84
85 5
    private function buildTree($path, $value = null)
86
    {
87 5
        $keys = $this->parsePath($path);
88 5
        $tree = [];
89 5
        $copy = &$tree;
90
91 5
        while (count($keys)) {
92 5
            $key  = array_shift($keys);
93 5
            $copy = &$copy[$key];
94
        }
95 5
        $copy = $value;
96
97 5
        return $tree;
98
    }
99
100 7
    private function parsePath($path)
101
    {
102 7
        $parts = array_filter(
103 7
            explode(static::PATH_SEPARATOR, (string) $path),
104 7
            'strlen'
105
        );
106
107 7
        return array_reduce(
108 7
            $parts,
109 7
            function ($acc, $v) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $v. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
110 7
                $acc[] = ctype_digit($v) ? intval($v) : $v;
111
112 7
                return $acc;
113 7
            },
114
            []
115
        );
116
    }
117
}
118