1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-DependencyInjection 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\DependencyInjection; |
12
|
|
|
|
13
|
|
|
use Borobudur\DependencyInjection\Exception\ParameterNotFoundException; |
14
|
|
|
use Borobudur\DependencyInjection\ParameterBag\ParameterBagInterface; |
15
|
|
|
use Borobudur\DependencyInjection\ParameterBag\Resolver; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Iqbal Maulana <[email protected]> |
19
|
|
|
* @created 8/7/15 |
20
|
|
|
*/ |
21
|
|
|
class ParameterBag implements ParameterBagInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $parameters = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Resolver |
30
|
|
|
*/ |
31
|
|
|
protected $resolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param array $parameters |
37
|
|
|
* @param Resolver $resolver |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $parameters = array(), Resolver $resolver = null) |
40
|
|
|
{ |
41
|
|
|
if (!empty($parameters)) { |
42
|
|
|
$this->add($parameters); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->resolver = $resolver ?: new Resolver($this); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function replace(array $parameters) |
52
|
|
|
{ |
53
|
|
|
$this->parameters = array(); |
54
|
|
|
|
55
|
|
|
return $this->add($parameters); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function add(array $parameters) |
62
|
|
|
{ |
63
|
|
|
foreach ($parameters as $name => $value) { |
64
|
|
|
$this->set($name, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function set($name, $value) |
74
|
|
|
{ |
75
|
|
|
$this->parameters[strtolower($name)] = $value; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function remove($name) |
84
|
|
|
{ |
85
|
|
|
unset($this->parameters[strtolower($name)]); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function clear() |
94
|
|
|
{ |
95
|
|
|
$this->parameters = array(); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function get($name) |
104
|
|
|
{ |
105
|
|
|
$name = strtolower($name); |
106
|
|
|
|
107
|
|
|
if (!array_key_exists($name, $this->parameters)) { |
108
|
|
|
if (empty($name)) { |
109
|
|
|
throw new ParameterNotFoundException($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
throw new ParameterNotFoundException($name, alternative($name, $this->parameters)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->parameters[$name]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function has($name) |
122
|
|
|
{ |
123
|
|
|
return array_key_exists(strtolower($name), $this->parameters); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get all parameters. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function all() |
132
|
|
|
{ |
133
|
|
|
return $this->parameters; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function getResolver() |
140
|
|
|
{ |
141
|
|
|
return $this->resolver; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|