Container::setTraverse()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4555
c 0
b 0
f 0
cc 5
nc 3
nop 2
crap 5
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\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 263
    public function __construct(array $values = [])
31
    {
32 263
        $this->values = $values;
33 263
    }
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 259
    public function has($key)
42
    {
43 259
        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 255
    public function get($key)
53
    {
54 255
        return $this->traverse($key, true);
55
    }
56
57
    /**
58
     * Set the value of $key to $value.
59
     *
60
     * @param string $key
61
     * @param mixed $value
62
     * @return $this
63
     */
64 142
    public function set($key, $value)
65
    {
66 142
        if (strpos($key, '.') !== false) {
67 4
            return $this->setTraverse($key, $value);
68
        }
69 138
        $this->values[$key] = $value;
70 138
        return $this;
71
    }
72
73
    /**
74
     * Returns a plain array representation of the Value\Container object.
75
     *
76
     * @return array
77
     */
78 262
    public function getArrayCopy()
79
    {
80 262
        return $this->values;
81
    }
82
83
    /**
84
     * Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set.
85
     *
86
     * @param string $key
87
     * @param bool $returnValue
88
     * @return mixed
89
     */
90 260
    protected function traverse($key, $returnValue = true)
91
    {
92 260
        $value = $this->values;
93 260
        foreach (explode('.', $key) as $part) {
94 260
            if (!is_array($value) || !array_key_exists($part, $value)) {
95 13
                return false;
96
            }
97 252
            $value = $value[$part];
98 252
        }
99 252
        return $returnValue ? $value : true;
100
    }
101
102
    /**
103
     * Uses dot-notation to set a value.
104
     *
105
     * @param string $key
106
     * @param mixed $value
107
     * @return $this
108
     */
109 4
    protected function setTraverse($key, $value)
110
    {
111 4
        $parts = explode('.', $key);
112 4
        $ref = &$this->values;
113
114 4
        foreach ($parts as $i => $part) {
115 4
            if ($i < count($parts) - 1 && (!isset($ref[$part]) || !is_array($ref[$part]))) {
116 4
                $ref[$part] = [];
117 4
            }
118 4
            $ref = &$ref[$part];
119 4
        }
120
121 4
        $ref = $value;
122 4
        return $this;
123
    }
124
}
125