1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the League\Fractal package. |
5
|
|
|
* |
6
|
|
|
* (c) Phil Sturgeon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\Fractal; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A handy interface for getting at include parameters. |
16
|
|
|
*/ |
17
|
|
|
class ParamBag implements \ArrayAccess, \IteratorAggregate |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $params = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new parameter bag instance. |
26
|
|
|
* |
27
|
|
|
* @param array $params |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
43 |
|
public function __construct(array $params) |
32
|
|
|
{ |
33
|
43 |
|
$this->params = $params; |
34
|
43 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get parameter values out of the bag. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
2 |
|
public function get($key) |
44
|
|
|
{ |
45
|
2 |
|
return $this->__get($key); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get parameter values out of the bag via the property access magic method. |
50
|
|
|
* |
51
|
|
|
* @param string $key |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
5 |
|
public function __get($key) |
56
|
|
|
{ |
57
|
5 |
|
return isset($this->params[$key]) ? $this->params[$key] : null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if a param exists in the bag via an isset() check on the property. |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
2 |
|
public function __isset($key) |
68
|
|
|
{ |
69
|
2 |
|
return isset($this->params[$key]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Disallow changing the value of params in the data bag via property access. |
74
|
|
|
* |
75
|
|
|
* @param string $key |
76
|
|
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @throws \LogicException |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
1 |
|
public function __set($key, $value) |
83
|
|
|
{ |
84
|
1 |
|
throw new \LogicException('Modifying parameters is not permitted'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Disallow unsetting params in the data bag via property access. |
89
|
|
|
* |
90
|
|
|
* @param string $key |
91
|
|
|
* |
92
|
|
|
* @throws \LogicException |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
1 |
|
public function __unset($key) |
97
|
|
|
{ |
98
|
1 |
|
throw new \LogicException('Modifying parameters is not permitted'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check if a param exists in the bag via an isset() and array access. |
103
|
|
|
* |
104
|
|
|
* @param string $key |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
1 |
|
public function offsetExists($key) |
109
|
|
|
{ |
110
|
1 |
|
return $this->__isset($key); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get parameter values out of the bag via array access. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
2 |
|
public function offsetGet($key) |
121
|
|
|
{ |
122
|
2 |
|
return $this->__get($key); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Disallow changing the value of params in the data bag via array access. |
127
|
|
|
* |
128
|
|
|
* @param string $key |
129
|
|
|
* @param mixed $value |
130
|
|
|
* |
131
|
|
|
* @throws \LogicException |
132
|
|
|
* |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
1 |
|
public function offsetSet($key, $value) |
136
|
|
|
{ |
137
|
1 |
|
throw new \LogicException('Modifying parameters is not permitted'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Disallow unsetting params in the data bag via array access. |
142
|
|
|
* |
143
|
|
|
* @param string $key |
144
|
|
|
* |
145
|
|
|
* @throws \LogicException |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
1 |
|
public function offsetUnset($key) |
150
|
|
|
{ |
151
|
1 |
|
throw new \LogicException('Modifying parameters is not permitted'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* IteratorAggregate for iterating over the object like an array. |
156
|
|
|
* |
157
|
|
|
* @return \ArrayIterator |
158
|
|
|
*/ |
159
|
10 |
|
public function getIterator() |
160
|
|
|
{ |
161
|
10 |
|
return new \ArrayIterator($this->params); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|