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\OutOfBoundsException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
17
|
|
|
* @created 8/8/15 |
18
|
|
|
*/ |
19
|
|
|
trait NumericParameterTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $parameters = array(); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Assert index in range. |
28
|
|
|
* |
29
|
|
|
* @param int $index |
30
|
|
|
* @param array $parameters |
31
|
|
|
*/ |
32
|
|
|
private static function assertBoundRange($index, array $parameters) |
33
|
|
|
{ |
34
|
|
|
if ($index < 0 || $index > count($parameters) - 1) { |
35
|
|
|
throw new OutOfBoundsException($index, 0, count($parameters) - 1); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Clear and replace with sets of parameters. |
41
|
|
|
* |
42
|
|
|
* @param array $parameters |
43
|
|
|
* |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
|
|
public function replace(array $parameters) |
47
|
|
|
{ |
48
|
|
|
$this->parameters = array(); |
49
|
|
|
|
50
|
|
|
foreach ($parameters as $parameter) { |
51
|
|
|
$this->add($parameter); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add parameter. |
59
|
|
|
* |
60
|
|
|
* @param mixed $parameter |
61
|
|
|
* |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public function add($parameter) |
65
|
|
|
{ |
66
|
|
|
$this->parameters[] = $parameter; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set parameter to specified index. |
73
|
|
|
* |
74
|
|
|
* @param int $index |
75
|
|
|
* @param mixed $parameter |
76
|
|
|
* |
77
|
|
|
* @return static |
78
|
|
|
*/ |
79
|
|
|
public function set($index, $parameter) |
80
|
|
|
{ |
81
|
|
|
self::assertBoundRange($index, $this->parameters); |
82
|
|
|
|
83
|
|
|
$this->parameters[$index] = $parameter; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Clear all parameters. |
90
|
|
|
* |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
|
|
public function clear() |
94
|
|
|
{ |
95
|
|
|
$this->parameters = array(); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get parameter by index. |
102
|
|
|
* |
103
|
|
|
* @param int $index |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function get($index) |
108
|
|
|
{ |
109
|
|
|
self::assertBoundRange($index, $this->parameters); |
110
|
|
|
|
111
|
|
|
return $this->parameters[$index]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Remove parameter. |
116
|
|
|
* |
117
|
|
|
* @param int $index |
118
|
|
|
* |
119
|
|
|
* @return static |
120
|
|
|
*/ |
121
|
|
|
public function remove($index) |
122
|
|
|
{ |
123
|
|
|
self::assertBoundRange($index, $this->parameters); |
124
|
|
|
|
125
|
|
|
unset($this->parameters[$index]); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get all parameters. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function all() |
136
|
|
|
{ |
137
|
|
|
return $this->parameters; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|