Completed
Pull Request — master (#43)
by Rick
54:37 queued 45:03
created

Container::removeRecursive()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 17
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 9
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Filter\Value;
10
11
/**
12
 * This class is used to wrap both input as output arrays.
13
 *
14
 * @package Particle\Validator
15
 */
16
class Container
17
{
18
    /**
19
     * Contains the values (either input or output).
20
     *
21
     * @var array
22
     */
23
    protected $values = [];
24
25
    /**
26
     * Construct the Value\Container.
27
     *
28
     * @param array $values
29
     */
30 153
    public function __construct(array $values = [])
31
    {
32 153
        $this->values = $values;
33 153
    }
34
35
    /**
36
     * Determines whether or not the container has a value for key $key.
37
     *
38
     * @param string $key
39
     * @return bool
40
     */
41 149
    public function has($key)
42
    {
43 149
        return $this->traverse($key, false);
44
    }
45
46
    /**
47
     * Returns the value for the key $key, or null if the value doesn't exist.
48
     *
49
     * @param string $key
50
     * @return mixed
51
     */
52 140
    public function get($key)
53
    {
54 140
        return $this->traverse($key, true);
55
    }
56
57
    /**
58
     * Removes a value from the container
59
     *
60
     * @param string $key
61
     */
62
    public function remove($key)
63
    {
64 148
        $this->removeRecursive(explode('.', $key), $this->values);
65
    }
66 148
67 4
    /**
68
     * Set the value of $key to $value.
69 145
     *
70 145
     * @param string $key
71
     * @param mixed $value
72
     * @return $this
73
     */
74
    public function set($key, $value)
75
    {
76
        if (strpos($key, '.') !== false) {
77
            return $this->setTraverse($key, $value);
78 153
        }
79
        $this->values[$key] = $value;
80 153
        return $this;
81
    }
82
83
    /**
84
     * Returns a plain array representation of the Value\Container object.
85
     *
86
     * @return array
87
     */
88
    public function getArrayCopy()
89
    {
90 149
        return $this->values;
91
    }
92 149
93 149
    /**
94 149
     * Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set.
95 10
     *
96
     * @param string $key
97 140
     * @param bool $returnValue
98 140
     * @return mixed
99 140
     */
100
    protected function traverse($key, $returnValue = true)
101
    {
102
        $value = $this->values;
103
        foreach (explode('.', $key) as $part) {
104
            if (!array_key_exists($part, $value)) {
105
                return false;
106
            }
107
            $value = $value[$part];
108
        }
109 4
        return $returnValue ? $value : true;
110
    }
111 4
112 4
    /**
113
     * Uses dot-notation to set a value.
114 4
     *
115 4
     * @param string $key
116 4
     * @param mixed $value
117
     * @return $this
118 4
     */
119 4
    protected function setTraverse($key, $value)
120
    {
121
        $parts = explode('.', $key);
122
        $ref = &$this->values;
123
124
        foreach ($parts as $i => $part) {
125
            $ref = &$ref[$part];
126
        }
127
128
        $ref = $value;
129
        return $this;
130
    }
131
132
    /**
133
     * @param array $keyParts
134
     * @param array $values
135
     */
136
    private function removeRecursive(array $keyParts, array &$values)
137
    {
138
        $key = $keyParts[0];
139
        if (array_key_exists($key, $values)) {
140
            if (is_array($values[$key])) {
141
                $this->removeRecursive(array_splice($keyParts, 1), $values[$key]);
142
143
                // unset self if the removed child clears this array
144
                if (count($values[$key]) === 0) {
145
                    unset($values[$key]);
146
                }
147
                return;
148
            }
149
            // Key is value, unset
150
            unset($values[$key]);
151
        }
152
    }
153
}
154