Completed
Pull Request — master (#43)
by Rick
04:00
created

Container::getArrayCopy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    /**
59
     * Removes a value from the container
60
     *
61
     * @param string $key
62
     */
63 6
    public function remove($key)
64
    {
65 6
        $value = &$this->values;
66 6 View Code Duplication
        foreach (explode('.', $key) as $part) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67 6
            if (!array_key_exists($part, $value)) {
68 4
                return;
69
            }
70 3
            $value = &$value[$part];
71 3
        }
72 2
        unset($value); // @todo: no worky worky
73 2
    }
74
75
    /**
76
     * Set the value of $key to $value.
77
     *
78
     * @param string $key
79
     * @param mixed $value
80
     * @return $this
81
     */
82 150
    public function set($key, $value)
83
    {
84 150
        if (strpos($key, '.') !== false) {
85 4
            return $this->setTraverse($key, $value);
86
        }
87 147
        $this->values[$key] = $value;
88 147
        return $this;
89
    }
90
91
    /**
92
     * Returns a plain array representation of the Value\Container object.
93
     *
94
     * @return array
95
     */
96 159
    public function getArrayCopy()
97
    {
98 159
        return $this->values;
99
    }
100
101
    /**
102
     * Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set.
103
     *
104
     * @param string $key
105
     * @param bool $returnValue
106
     * @return mixed
107
     */
108 155
    protected function traverse($key, $returnValue = true)
109
    {
110 155
        $value = $this->values;
111 155 View Code Duplication
        foreach (explode('.', $key) as $part) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112 155
            if (!array_key_exists($part, $value)) {
113 12
                return false;
114
            }
115 144
            $value = $value[$part];
116 144
        }
117 144
        return $returnValue ? $value : true;
118
    }
119
120
    /**
121
     * Uses dot-notation to set a value.
122
     *
123
     * @param string $key
124
     * @param mixed $value
125
     * @return $this
126
     */
127 4
    protected function setTraverse($key, $value)
128
    {
129 4
        $parts = explode('.', $key);
130 4
        $ref = &$this->values;
131
132 4
        foreach ($parts as $i => $part) {
133 4
            $ref = &$ref[$part];
134 4
        }
135
136 4
        $ref = $value;
137 4
        return $this;
138
    }
139
}
140