1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Cqrs package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Cqrs; |
12
|
|
|
|
13
|
|
|
use ArrayIterator; |
14
|
|
|
use Countable; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Iqbal Maulana <[email protected]> |
19
|
|
|
* @created 8/18/15 |
20
|
|
|
*/ |
21
|
|
|
class ParameterBag implements Countable, IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $parameters = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param array $parameters |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $parameters = array()) |
34
|
|
|
{ |
35
|
|
|
$this->parameters = $parameters; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Add sets of value. |
40
|
|
|
* |
41
|
|
|
* @param array $values |
42
|
|
|
*/ |
43
|
|
|
public function add(array $values) |
44
|
|
|
{ |
45
|
|
|
foreach ($values as $index => $value) { |
46
|
|
|
$this->set($index, $value); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get parameter value. |
52
|
|
|
* |
53
|
|
|
* @param string|int $index |
54
|
|
|
* @param mixed|null $default |
55
|
|
|
* |
56
|
|
|
* @return mixed|null |
57
|
|
|
*/ |
58
|
|
|
public function get($index, $default = null) |
59
|
|
|
{ |
60
|
|
|
if (isset($this->parameters[$index])) { |
61
|
|
|
return $this->parameters[$index]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $default; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set parameter value. |
69
|
|
|
* |
70
|
|
|
* @param string|int $index |
71
|
|
|
* @param mixed $value |
72
|
|
|
*/ |
73
|
|
|
public function set($index, $value) |
74
|
|
|
{ |
75
|
|
|
$this->parameters[$index] = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Remove parameter by index. |
80
|
|
|
* |
81
|
|
|
* @param string|int $index |
82
|
|
|
*/ |
83
|
|
|
public function remove($index) |
84
|
|
|
{ |
85
|
|
|
unset($this->parameters[$index]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get all parameters. |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function all() |
94
|
|
|
{ |
95
|
|
|
return $this->parameters; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function count() |
102
|
|
|
{ |
103
|
|
|
return count($this->parameters); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function getIterator() |
110
|
|
|
{ |
111
|
|
|
return new ArrayIterator($this->parameters); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|