Completed
Pull Request — master (#43)
by Rick
05:41 queued 10s
created

Container::remove()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4
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 159
    public function __construct(array $values = [])
31
    {
32 159
        $this->values = $values;
33 159
    }
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 155
    public function has($key)
42
    {
43 155
        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 144
    public function get($key)
53
    {
54 144
        return $this->traverse($key, true);
55
    }
56
57
    /**
58
     * Removes a value from the container
59
     *
60
     * @param string $key
61
     */
62 6
    public function remove($key)
63
    {
64 6
        $value = &$this->values;
65 6
        $exploded = explode('.', $key);
66
67 6
        foreach ($exploded as $i => $part) {
68 6
            if (!array_key_exists($part, $value)) {
69 4
                return;
70
            }
71 3
            if ($i === count($exploded) - 1) {
72 2
                unset($value[$key]);
73 2
            }
74 3
        }
75 2
    }
76
77
    /**
78
     * Set the value of $key to $value.
79
     *
80
     * @param string $key
81
     * @param mixed $value
82
     * @return $this
83
     */
84 150
    public function set($key, $value)
85
    {
86 150
        if (strpos($key, '.') !== false) {
87 4
            return $this->setTraverse($key, $value);
88
        }
89 147
        $this->values[$key] = $value;
90 147
        return $this;
91
    }
92
93
    /**
94
     * Returns a plain array representation of the Value\Container object.
95
     *
96
     * @return array
97
     */
98 159
    public function getArrayCopy()
99
    {
100 159
        return $this->values;
101
    }
102
103
    /**
104
     * Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set.
105
     *
106
     * @param string $key
107
     * @param bool $returnValue
108
     * @return mixed
109
     */
110 155
    protected function traverse($key, $returnValue = true)
111
    {
112 155
        $value = $this->values;
113 155
        foreach (explode('.', $key) as $part) {
114 155
            if (!array_key_exists($part, $value)) {
115 12
                return false;
116
            }
117 144
            $value = $value[$part];
118 144
        }
119 144
        return $returnValue ? $value : true;
120
    }
121
122
    /**
123
     * Uses dot-notation to set a value.
124
     *
125
     * @param string $key
126
     * @param mixed $value
127
     * @return $this
128
     */
129 4
    protected function setTraverse($key, $value)
130
    {
131 4
        $parts = explode('.', $key);
132 4
        $ref = &$this->values;
133
134 4
        foreach ($parts as $i => $part) {
135 4
            $ref = &$ref[$part];
136 4
        }
137
138 4
        $ref = $value;
139 4
        return $this;
140
    }
141
}
142