Open Monograph Press  3.3.0
Collection.php
1 <?php
2 
3 namespace Guzzle\Common;
4 
7 
11 class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArrayInterface
12 {
14  protected $data;
15 
19  public function __construct(array $data = array())
20  {
21  $this->data = $data;
22  }
23 
34  public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array())
35  {
36  $data = $config + $defaults;
37 
38  if ($missing = array_diff($required, array_keys($data))) {
39  throw new InvalidArgumentException('Config is missing the following keys: ' . implode(', ', $missing));
40  }
41 
42  return new self($data);
43  }
44 
45  public function count()
46  {
47  return count($this->data);
48  }
49 
50  public function getIterator()
51  {
52  return new \ArrayIterator($this->data);
53  }
54 
55  public function toArray()
56  {
57  return $this->data;
58  }
59 
65  public function clear()
66  {
67  $this->data = array();
68 
69  return $this;
70  }
71 
79  public function getAll(array $keys = null)
80  {
81  return $keys ? array_intersect_key($this->data, array_flip($keys)) : $this->data;
82  }
83 
91  public function get($key)
92  {
93  return isset($this->data[$key]) ? $this->data[$key] : null;
94  }
95 
104  public function set($key, $value)
105  {
106  $this->data[$key] = $value;
107 
108  return $this;
109  }
110 
120  public function add($key, $value)
121  {
122  if (!array_key_exists($key, $this->data)) {
123  $this->data[$key] = $value;
124  } elseif (is_array($this->data[$key])) {
125  $this->data[$key][] = $value;
126  } else {
127  $this->data[$key] = array($this->data[$key], $value);
128  }
129 
130  return $this;
131  }
132 
140  public function remove($key)
141  {
142  unset($this->data[$key]);
143 
144  return $this;
145  }
146 
152  public function getKeys()
153  {
154  return array_keys($this->data);
155  }
156 
164  public function hasKey($key)
165  {
166  return array_key_exists($key, $this->data);
167  }
168 
176  public function keySearch($key)
177  {
178  foreach (array_keys($this->data) as $k) {
179  if (!strcasecmp($k, $key)) {
180  return $k;
181  }
182  }
183 
184  return false;
185  }
186 
194  public function hasValue($value)
195  {
196  return array_search($value, $this->data);
197  }
198 
206  public function replace(array $data)
207  {
208  $this->data = $data;
209 
210  return $this;
211  }
212 
220  public function merge($data)
221  {
222  foreach ($data as $key => $value) {
223  $this->add($key, $value);
224  }
225 
226  return $this;
227  }
228 
236  public function overwriteWith($data)
237  {
238  if (is_array($data)) {
239  $this->data = $data + $this->data;
240  } elseif ($data instanceof Collection) {
241  $this->data = $data->toArray() + $this->data;
242  } else {
243  foreach ($data as $key => $value) {
244  $this->data[$key] = $value;
245  }
246  }
247 
248  return $this;
249  }
250 
262  public function map(\Closure $closure, array $context = array(), $static = true)
263  {
264  $collection = $static ? new static() : new self();
265  foreach ($this as $key => $value) {
266  $collection->add($key, $closure($key, $value, $context));
267  }
268 
269  return $collection;
270  }
271 
282  public function filter(\Closure $closure, $static = true)
283  {
284  $collection = ($static) ? new static() : new self();
285  foreach ($this->data as $key => $value) {
286  if ($closure($key, $value)) {
287  $collection->add($key, $value);
288  }
289  }
290 
291  return $collection;
292  }
293 
294  public function offsetExists($offset)
295  {
296  return isset($this->data[$offset]);
297  }
298 
299  public function offsetGet($offset)
300  {
301  return isset($this->data[$offset]) ? $this->data[$offset] : null;
302  }
303 
304  public function offsetSet($offset, $value)
305  {
306  $this->data[$offset] = $value;
307  }
308 
309  public function offsetUnset($offset)
310  {
311  unset($this->data[$offset]);
312  }
313 
323  public function setPath($path, $value)
324  {
325  $current =& $this->data;
326  $queue = explode('/', $path);
327  while (null !== ($key = array_shift($queue))) {
328  if (!is_array($current)) {
329  throw new RuntimeException("Trying to setPath {$path}, but {$key} is set and is not an array");
330  } elseif (!$queue) {
331  $current[$key] = $value;
332  } elseif (isset($current[$key])) {
333  $current =& $current[$key];
334  } else {
335  $current[$key] = array();
336  $current =& $current[$key];
337  }
338  }
339 
340  return $this;
341  }
342 
354  public function getPath($path, $separator = '/', $data = null)
355  {
356  if ($data === null) {
358  }
359 
360  $path = is_array($path) ? $path : explode($separator, $path);
361  while (null !== ($part = array_shift($path))) {
362  if (!is_array($data)) {
363  return null;
364  } elseif (isset($data[$part])) {
365  $data =& $data[$part];
366  } elseif ($part != '*') {
367  return null;
368  } else {
369  // Perform a wildcard search by diverging and merging paths
370  $result = array();
371  foreach ($data as $value) {
372  if (!$path) {
373  $result = array_merge_recursive($result, (array) $value);
374  } elseif (null !== ($test = $this->getPath($path, $separator, $value))) {
375  $result = array_merge_recursive($result, (array) $test);
376  }
377  }
378  return $result;
379  }
380  }
381 
382  return $data;
383  }
384 
393  public function inject($input)
394  {
395  Version::warn(__METHOD__ . ' is deprecated');
396  $replace = array();
397  foreach ($this->data as $key => $val) {
398  $replace['{' . $key . '}'] = $val;
399  }
400 
401  return strtr($input, $replace);
402  }
403 }
Guzzle\Common\Collection\offsetSet
offsetSet($offset, $value)
Definition: Collection.php:307
Guzzle\Common\ToArrayInterface
Definition: ToArrayInterface.php:8
Guzzle\Common\Collection\replace
replace(array $data)
Definition: Collection.php:209
Guzzle\Common\Collection\count
count()
Definition: Collection.php:48
Guzzle\Common\Collection\clear
clear()
Definition: Collection.php:68
Guzzle\Common\Collection\getKeys
getKeys()
Definition: Collection.php:155
Guzzle\Common\Collection\getPath
getPath($path, $separator='/', $data=null)
Definition: Collection.php:357
Guzzle\Common\Collection\keySearch
keySearch($key)
Definition: Collection.php:179
Guzzle\Common\Collection\overwriteWith
overwriteWith($data)
Definition: Collection.php:239
Guzzle\Common\Collection\hasValue
hasValue($value)
Definition: Collection.php:197
Guzzle\Common\Version\warn
static warn($message)
Definition: Version.php:23
Guzzle\Common\Collection\offsetExists
offsetExists($offset)
Definition: Collection.php:297
Guzzle\Common\Collection\hasKey
hasKey($key)
Definition: Collection.php:167
Guzzle\Common\Collection\offsetGet
offsetGet($offset)
Definition: Collection.php:302
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Common\Collection\getAll
getAll(array $keys=null)
Definition: Collection.php:82
Guzzle\Common\Collection\getIterator
getIterator()
Definition: Collection.php:53
Guzzle\Common\Collection\inject
inject($input)
Definition: Collection.php:396
Guzzle\Common\Collection\setPath
setPath($path, $value)
Definition: Collection.php:326
Guzzle\Common\Collection\$data
$data
Definition: Collection.php:17
Guzzle\Common\Collection\add
add($key, $value)
Definition: Collection.php:123
Guzzle\Common\Collection\offsetUnset
offsetUnset($offset)
Definition: Collection.php:312
Guzzle\Common\Collection\merge
merge($data)
Definition: Collection.php:223
Guzzle\Common\Collection\filter
filter(\Closure $closure, $static=true)
Definition: Collection.php:285
Guzzle\Common\Collection\fromConfig
static fromConfig(array $config=array(), array $defaults=array(), array $required=array())
Definition: Collection.php:37
Guzzle\Common\Exception\RuntimeException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/RuntimeException.php:5
Guzzle\Common
Definition: AbstractHasDispatcher.php:3
Guzzle\Common\Collection\toArray
toArray()
Definition: Collection.php:58
Guzzle\Common\Collection\map
map(\Closure $closure, array $context=array(), $static=true)
Definition: Collection.php:265
Guzzle\Common\Collection
Definition: Collection.php:11
Guzzle\Common\Collection\__construct
__construct(array $data=array())
Definition: Collection.php:22