|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Common; |
|
26
|
|
|
|
|
27
|
|
|
use ArrayIterator; |
|
28
|
|
|
use Brickoo\Component\Common\Exception\InvalidValueTypeException; |
|
29
|
|
|
use Brickoo\Component\Common\Assert; |
|
30
|
|
|
use Brickoo\Component\Validation\Validator\Validator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Container |
|
34
|
|
|
* |
|
35
|
|
|
* Implements a simple array object to store any kind of values. |
|
36
|
|
|
* @author Celestino Diaz <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class Container implements \IteratorAggregate, \Countable { |
|
39
|
|
|
|
|
40
|
|
|
/** @var array */ |
|
41
|
|
|
protected $container; |
|
42
|
|
|
|
|
43
|
|
|
/** @var null|\Brickoo\Component\Validation\Validator\Validator */ |
|
44
|
|
|
protected $validator; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Class constructor. |
|
48
|
|
|
* @param array $container |
|
49
|
|
|
* @param null|Validator $validator |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function __construct(array $container = [], Validator $validator = null) { |
|
52
|
1 |
|
$this->container = []; |
|
53
|
1 |
|
$this->validator = $validator; |
|
54
|
1 |
|
$this->fromArray($container); |
|
55
|
1 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns an external array iterator. |
|
59
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
60
|
|
|
* @return \ArrayIterator |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getIterator() { |
|
63
|
1 |
|
return new ArrayIterator($this->container); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the number of elements contained to iterate. |
|
68
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
69
|
|
|
* @return integer the number of elements available |
|
70
|
|
|
*/ |
|
71
|
1 |
|
public function count() { |
|
72
|
1 |
|
return count($this->container); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks if the container contains the key. |
|
77
|
|
|
* @param string|integer $key |
|
78
|
|
|
* @throws \InvalidArgumentException |
|
79
|
|
|
* @return boolean check result |
|
80
|
|
|
*/ |
|
81
|
5 |
|
public function contains($key) { |
|
82
|
5 |
|
Assert::isStringOrInteger($key); |
|
83
|
5 |
|
return array_key_exists($key, $this->container); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the value of the given key. |
|
88
|
|
|
* @param string|integer $key |
|
89
|
|
|
* @param mixed $defaultValue |
|
90
|
|
|
* @return mixed the key associated value otherwise the default value |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public function get($key, $defaultValue = null) { |
|
93
|
1 |
|
Assert::isStringOrInteger($key); |
|
94
|
|
|
|
|
95
|
1 |
|
if ($this->contains($key)) { |
|
96
|
1 |
|
return $this->container[$key]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $defaultValue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Sets a key-value pair into the container. |
|
104
|
|
|
* @param string|integer $key |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
* @throws \Brickoo\Component\Common\Exception\InvalidValueTypeException |
|
107
|
|
|
* @return \Brickoo\Component\Common\Container |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function set($key, $value) { |
|
110
|
2 |
|
Assert::isStringOrInteger($key); |
|
111
|
|
|
|
|
112
|
2 |
|
if (!$this->isValueTypeValid($value)) { |
|
113
|
1 |
|
throw new InvalidValueTypeException($value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
$this->container[$key] = $value; |
|
117
|
1 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Removes the container entry by its key. |
|
122
|
|
|
* @param string|integer $key |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
* @return \Brickoo\Component\Common\Container |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function remove($key) { |
|
127
|
1 |
|
Assert::isStringOrInteger($key); |
|
128
|
|
|
|
|
129
|
1 |
|
if ($this->contains($key)) { |
|
130
|
1 |
|
unset($this->container[$key]); |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Checks if the container is empty. |
|
138
|
|
|
* @return boolean check result |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function isEmpty() { |
|
141
|
1 |
|
return empty($this->container); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Clear the container. |
|
146
|
|
|
* @return \Brickoo\Component\Common\Container |
|
147
|
|
|
*/ |
|
148
|
1 |
|
public function clear() { |
|
149
|
1 |
|
$this->container = []; |
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Imports the elements from an array. |
|
155
|
|
|
* @param array $container |
|
156
|
|
|
* @throws \Brickoo\Component\Common\Exception\InvalidValueTypeException |
|
157
|
|
|
* @return \Brickoo\Component\Common\Container |
|
158
|
|
|
*/ |
|
159
|
2 |
|
public function fromArray(array $container) { |
|
160
|
2 |
|
foreach ($container as $key => $value) { |
|
161
|
2 |
|
if (!$this->isValueTypeValid($value)) { |
|
162
|
1 |
|
throw new InvalidValueTypeException($value); |
|
163
|
|
|
} |
|
164
|
2 |
|
$this->container[$key] = $value; |
|
165
|
2 |
|
} |
|
166
|
2 |
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the container entries as an array. |
|
171
|
|
|
* @return array the container entries |
|
172
|
|
|
*/ |
|
173
|
1 |
|
public function toArray() { |
|
174
|
1 |
|
return $this->container; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Checks if the values type is valid. |
|
179
|
|
|
* @param mixed $value |
|
180
|
|
|
* @return boolean check result |
|
181
|
|
|
*/ |
|
182
|
4 |
|
protected function isValueTypeValid($value) { |
|
183
|
4 |
|
return ($this->validator === null || $this->validator->isValid($value)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|