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 |
View Code Duplication |
foreach (explode('.', $key) as $part) { |
|
|
|
|
66
|
6 |
|
if (!array_key_exists($part, $value)) { |
67
|
4 |
|
return; |
68
|
|
|
} |
69
|
3 |
|
$value = &$value[$part]; |
70
|
3 |
|
} |
71
|
2 |
|
unset($value); // @todo: no worky worky |
72
|
2 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the value of $key to $value. |
76
|
|
|
* |
77
|
|
|
* @param string $key |
78
|
|
|
* @param mixed $value |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
150 |
|
public function set($key, $value) |
82
|
|
|
{ |
83
|
150 |
|
if (strpos($key, '.') !== false) { |
84
|
4 |
|
return $this->setTraverse($key, $value); |
85
|
|
|
} |
86
|
147 |
|
$this->values[$key] = $value; |
87
|
147 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns a plain array representation of the Value\Container object. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
159 |
|
public function getArrayCopy() |
96
|
|
|
{ |
97
|
159 |
|
return $this->values; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @param bool $returnValue |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
155 |
|
protected function traverse($key, $returnValue = true) |
108
|
|
|
{ |
109
|
155 |
|
$value = $this->values; |
110
|
155 |
View Code Duplication |
foreach (explode('.', $key) as $part) { |
|
|
|
|
111
|
155 |
|
if (!array_key_exists($part, $value)) { |
112
|
12 |
|
return false; |
113
|
|
|
} |
114
|
144 |
|
$value = $value[$part]; |
115
|
144 |
|
} |
116
|
144 |
|
return $returnValue ? $value : true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Uses dot-notation to set a value. |
121
|
|
|
* |
122
|
|
|
* @param string $key |
123
|
|
|
* @param mixed $value |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
4 |
|
protected function setTraverse($key, $value) |
127
|
|
|
{ |
128
|
4 |
|
$parts = explode('.', $key); |
129
|
4 |
|
$ref = &$this->values; |
130
|
|
|
|
131
|
4 |
|
foreach ($parts as $i => $part) { |
132
|
4 |
|
$ref = &$ref[$part]; |
133
|
4 |
|
} |
134
|
|
|
|
135
|
4 |
|
$ref = $value; |
136
|
4 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.