Completed
Push — master ( cb7865...06cb05 )
by Rick
02:33
created

Container   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 15
c 4
b 0
f 0
lcom 1
cbo 0
dl 0
loc 109
ccs 33
cts 33
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A has() 0 4 1
A get() 0 4 1
A set() 0 8 2
A getArrayCopy() 0 4 1
A traverse() 0 11 4
B setTraverse() 0 15 5
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\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 137
    public function __construct(array $values = [])
31
    {
32 137
        $this->values = $values;
33 137
    }
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 134
    public function has($key)
42
    {
43 134
        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 134
    public function get($key)
53
    {
54 134
        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 136
    public function set($key, $value)
65
    {
66 136
        if (strpos($key, '.') !== false) {
67 5
            return $this->setTraverse($key, $value);
68
        }
69 131
        $this->values[$key] = $value;
70 131
        return $this;
71
    }
72
73
    /**
74
     * Returns a plain array representation of the Value\Container object.
75
     *
76
     * @return array
77
     */
78 137
    public function getArrayCopy()
79
    {
80 137
        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 134
    protected function traverse($key, $returnValue = true)
91
    {
92 134
        $value = $this->values;
93 134
        foreach (explode('.', $key) as $part) {
94 134
            if (!array_key_exists($part, $value)) {
95 8
                return false;
96
            }
97 127
            $value = $value[$part];
98 127
        }
99 127
        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 5
    protected function setTraverse($key, $value)
110
    {
111 5
        $parts = explode('.', $key);
112 5
        $ref = &$this->values;
113
114 5
        foreach ($parts as $i => $part) {
115 5
            if ($i < count($parts) - 1 && (!isset($ref[$part]) || !is_array($ref[$part]))) {
116 5
                $ref[$part] = [];
117 5
            }
118 5
            $ref = &$ref[$part];
119 5
        }
120
121 5
        $ref = $value;
122 5
        return $this;
123
    }
124
}
125