scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | namespace GuzzleHttp; |
||
4 | |||
5 | /** |
||
6 | * Key value pair collection object |
||
7 | */ |
||
8 | class Collection implements |
||
9 | \ArrayAccess, |
||
10 | \IteratorAggregate, |
||
11 | \Countable, |
||
12 | ToArrayInterface |
||
13 | { |
||
14 | use HasDataTrait; |
||
15 | |||
16 | /** |
||
17 | * @param array $data Associative array of data to set |
||
18 | */ |
||
19 | public function __construct(array $data = []) |
||
20 | { |
||
21 | $this->data = $data; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Create a new collection from an array, validate the keys, and add default |
||
26 | * values where missing |
||
27 | * |
||
28 | * @param array $config Configuration values to apply. |
||
29 | * @param array $defaults Default parameters |
||
30 | * @param array $required Required parameter names |
||
31 | * |
||
32 | * @return self |
||
33 | * @throws \InvalidArgumentException if a parameter is missing |
||
34 | */ |
||
35 | public static function fromConfig( |
||
36 | array $config = [], |
||
37 | array $defaults = [], |
||
38 | array $required = [] |
||
39 | ) { |
||
40 | $data = $config + $defaults; |
||
41 | |||
42 | if ($missing = array_diff($required, array_keys($data))) { |
||
43 | throw new \InvalidArgumentException( |
||
44 | 'Config is missing the following keys: ' . |
||
45 | implode(', ', $missing)); |
||
46 | } |
||
47 | |||
48 | return new self($data); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Removes all key value pairs |
||
53 | * |
||
54 | * @return Collection |
||
55 | */ |
||
56 | public function clear() |
||
57 | { |
||
58 | $this->data = []; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get a specific key value. |
||
65 | * |
||
66 | * @param string $key Key to retrieve. |
||
67 | * |
||
68 | * @return mixed|null Value of the key or NULL |
||
69 | */ |
||
70 | public function get($key) |
||
71 | { |
||
72 | return isset($this->data[$key]) ? $this->data[$key] : null; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Set a key value pair |
||
77 | * |
||
78 | * @param string $key Key to set |
||
79 | * @param mixed $value Value to set |
||
80 | * |
||
81 | * @return Collection Returns a reference to the object |
||
82 | */ |
||
83 | public function set($key, $value) |
||
84 | { |
||
85 | $this->data[$key] = $value; |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Add a value to a key. If a key of the same name has already been added, |
||
92 | * the key value will be converted into an array and the new value will be |
||
93 | * pushed to the end of the array. |
||
94 | * |
||
95 | * @param string $key Key to add |
||
96 | * @param mixed $value Value to add to the key |
||
97 | * |
||
98 | * @return Collection Returns a reference to the object. |
||
99 | */ |
||
100 | public function add($key, $value) |
||
101 | { |
||
102 | if (!array_key_exists($key, $this->data)) { |
||
103 | $this->data[$key] = $value; |
||
104 | } elseif (is_array($this->data[$key])) { |
||
105 | $this->data[$key][] = $value; |
||
106 | } else { |
||
107 | $this->data[$key] = array($this->data[$key], $value); |
||
108 | } |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Remove a specific key value pair |
||
115 | * |
||
116 | * @param string $key A key to remove |
||
117 | * |
||
118 | * @return Collection |
||
119 | */ |
||
120 | public function remove($key) |
||
121 | { |
||
122 | unset($this->data[$key]); |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Get all keys in the collection |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function getKeys() |
||
133 | { |
||
134 | return array_keys($this->data); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Returns whether or not the specified key is present. |
||
139 | * |
||
140 | * @param string $key The key for which to check the existence. |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function hasKey($key) |
||
145 | { |
||
146 | return array_key_exists($key, $this->data); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Checks if any keys contains a certain value |
||
151 | * |
||
152 | * @param string $value Value to search for |
||
153 | * |
||
154 | * @return mixed Returns the key if the value was found FALSE if the value |
||
155 | * was not found. |
||
156 | */ |
||
157 | public function hasValue($value) |
||
158 | { |
||
159 | return array_search($value, $this->data, true); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Replace the data of the object with the value of an array |
||
164 | * |
||
165 | * @param array $data Associative array of data |
||
166 | * |
||
167 | * @return Collection Returns a reference to the object |
||
168 | */ |
||
169 | public function replace(array $data) |
||
170 | { |
||
171 | $this->data = $data; |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Add and merge in a Collection or array of key value pair data. |
||
178 | * |
||
179 | * @param Collection|array $data Associative array of key value pair data |
||
180 | * |
||
181 | * @return Collection Returns a reference to the object. |
||
182 | */ |
||
183 | public function merge($data) |
||
184 | { |
||
185 | foreach ($data as $key => $value) { |
||
186 | $this->add($key, $value); |
||
187 | } |
||
188 | |||
189 | return $this; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Over write key value pairs in this collection with all of the data from |
||
194 | * an array or collection. |
||
195 | * |
||
196 | * @param array|\Traversable $data Values to override over this config |
||
197 | * |
||
198 | * @return self |
||
199 | */ |
||
200 | public function overwriteWith($data) |
||
201 | { |
||
202 | if (is_array($data)) { |
||
203 | $this->data = $data + $this->data; |
||
204 | } elseif ($data instanceof Collection) { |
||
205 | $this->data = $data->toArray() + $this->data; |
||
206 | } else { |
||
207 | foreach ($data as $key => $value) { |
||
208 | $this->data[$key] = $value; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | return $this; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Returns a Collection containing all the elements of the collection after |
||
217 | * applying the callback function to each one. |
||
218 | * |
||
219 | * The callable should accept three arguments: |
||
220 | * - (string) $key |
||
221 | * - (string) $value |
||
222 | * - (array) $context |
||
223 | * |
||
224 | * The callable must return a the altered or unaltered value. |
||
225 | * |
||
226 | * @param callable $closure Map function to apply |
||
227 | * @param array $context Context to pass to the callable |
||
228 | * |
||
229 | * @return Collection |
||
230 | */ |
||
231 | public function map(callable $closure, array $context = []) |
||
232 | { |
||
233 | $collection = new static(); |
||
234 | foreach ($this as $key => $value) { |
||
235 | $collection[$key] = $closure($key, $value, $context); |
||
236 | } |
||
237 | |||
238 | return $collection; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Iterates over each key value pair in the collection passing them to the |
||
243 | * callable. If the callable returns true, the current value from input is |
||
244 | * returned into the result Collection. |
||
245 | * |
||
246 | * The callable must accept two arguments: |
||
247 | * - (string) $key |
||
248 | * - (string) $value |
||
249 | * |
||
250 | * @param callable $closure Evaluation function |
||
251 | * |
||
252 | * @return Collection |
||
253 | */ |
||
254 | public function filter(callable $closure) |
||
255 | { |
||
256 | $collection = new static(); |
||
257 | foreach ($this->data as $key => $value) { |
||
258 | if ($closure($key, $value)) { |
||
259 | $collection[$key] = $value; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | return $collection; |
||
264 | } |
||
265 | } |