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
|
154 |
|
public function __construct(array $values = []) |
31
|
|
|
{ |
32
|
154 |
|
$this->values = $values; |
33
|
154 |
|
} |
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
|
150 |
|
public function has($key) |
42
|
|
|
{ |
43
|
150 |
|
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
|
139 |
|
public function get($key) |
53
|
|
|
{ |
54
|
139 |
|
return $this->traverse($key, true); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Removes a value from the container |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
*/ |
63
|
5 |
|
public function remove($key) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// @todo |
66
|
5 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the value of $key to $value. |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
* @param mixed $value |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
146 |
|
public function set($key, $value) |
76
|
|
|
{ |
77
|
146 |
|
if (strpos($key, '.') !== false) { |
78
|
4 |
|
return $this->setTraverse($key, $value); |
79
|
|
|
} |
80
|
143 |
|
$this->values[$key] = $value; |
81
|
143 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns a plain array representation of the Value\Container object. |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
154 |
|
public function getArrayCopy() |
90
|
|
|
{ |
91
|
154 |
|
return $this->values; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Traverses the key using dot notation. Based on the second parameter, it will return the value or if it was set. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param bool $returnValue |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
150 |
|
protected function traverse($key, $returnValue = true) |
102
|
|
|
{ |
103
|
150 |
|
$value = $this->values; |
104
|
150 |
|
foreach (explode('.', $key) as $part) { |
105
|
150 |
|
if (!array_key_exists($part, $value)) { |
106
|
12 |
|
return false; |
107
|
|
|
} |
108
|
139 |
|
$value = $value[$part]; |
109
|
139 |
|
} |
110
|
139 |
|
return $returnValue ? $value : true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Uses dot-notation to set a value. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* @param mixed $value |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
4 |
|
protected function setTraverse($key, $value) |
121
|
|
|
{ |
122
|
4 |
|
$parts = explode('.', $key); |
123
|
4 |
|
$ref = &$this->values; |
124
|
|
|
|
125
|
4 |
|
foreach ($parts as $i => $part) { |
126
|
4 |
|
$ref = &$ref[$part]; |
127
|
4 |
|
} |
128
|
|
|
|
129
|
4 |
|
$ref = $value; |
130
|
4 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.