mirror of
https://github.com/guzzle/guzzle.git
synced 2025-01-18 05:48:15 +01:00
Optimizing Collection ArrayAccess and cleaning up
This commit is contained in:
parent
7db99356cf
commit
7a739903c2
@ -30,16 +30,16 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
* @return self
|
||||
* @throws InvalidArgumentException if a parameter is missing
|
||||
*/
|
||||
public static function fromConfig(array $config = null, array $defaults = null, array $required = null)
|
||||
public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array())
|
||||
{
|
||||
$collection = new self($defaults);
|
||||
|
||||
foreach ((array) $config as $key => $value) {
|
||||
foreach ($config as $key => $value) {
|
||||
$collection->set($key, $value);
|
||||
}
|
||||
|
||||
foreach ((array) $required as $key) {
|
||||
if ($collection->hasKey($key) === false) {
|
||||
foreach ($required as $key) {
|
||||
if (!isset($collection[$key])) {
|
||||
throw new InvalidArgumentException("Config must contain a '{$key}' key");
|
||||
}
|
||||
}
|
||||
@ -47,6 +47,72 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function count()
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all key value pairs
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->data = array();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all or a subset of matching key value pairs
|
||||
*
|
||||
* @param array $keys Pass an array of keys to retrieve only a subset of key value pairs
|
||||
*
|
||||
* @return array Returns an array of all matching key value pairs
|
||||
*/
|
||||
public function getAll(array $keys = null)
|
||||
{
|
||||
return $keys ? array_intersect_key($this->data, array_flip($keys)) : $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific key value.
|
||||
*
|
||||
* @param string $key Key to retrieve.
|
||||
*
|
||||
* @return mixed|null Value of the key or NULL
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return isset($this->data[$key]) ? $this->data[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a key value pair
|
||||
*
|
||||
* @param string $key Key to set
|
||||
* @param mixed $value Value to set
|
||||
*
|
||||
* @return Collection Returns a reference to the object
|
||||
*/
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to a key. If a key of the same name has already been added, the key value will be converted into an
|
||||
* array and the new value will be pushed to the end of the array.
|
||||
@ -70,88 +136,19 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all key value pairs
|
||||
* Remove a specific key value pair
|
||||
*
|
||||
* @param string $key A key to remove
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function clear()
|
||||
public function remove($key)
|
||||
{
|
||||
$this->data = array();
|
||||
unset($this->data[$key]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of keys
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over each key value pair in the collection passing them to the Closure. If the Closure function returns
|
||||
* true, the current value from input is returned into the result Collection. The Closure must accept three
|
||||
* parameters: (string) $key, (string) $value and return Boolean TRUE or FALSE for each value.
|
||||
*
|
||||
* @param \Closure $closure Closure evaluation function
|
||||
* @param bool $static Set to TRUE to use the same class as the return rather than returning a Collection
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function filter(\Closure $closure, $static = true)
|
||||
{
|
||||
$collection = ($static) ? new static() : new self();
|
||||
foreach ($this->data as $key => $value) {
|
||||
if ($closure($key, $value)) {
|
||||
$collection->add($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an iterator object
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific key value.
|
||||
*
|
||||
* @param string $key Key to retrieve.
|
||||
*
|
||||
* @return mixed|null Value of the key or NULL
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return isset($this->data[$key]) ? $this->data[$key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all or a subset of matching key value pairs
|
||||
*
|
||||
* @param array $keys Pass an array of keys to retrieve only a subset of key value pairs
|
||||
*
|
||||
* @return array Returns an array of all matching key value pairs
|
||||
*/
|
||||
public function getAll(array $keys = null)
|
||||
{
|
||||
return $keys ? array_intersect_key($this->data, array_flip($keys)) : $this->data;
|
||||
}
|
||||
|
||||
public function toArray()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all keys in the collection
|
||||
*
|
||||
@ -204,6 +201,60 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
return array_search($value, $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the data of the object with the value of an array
|
||||
*
|
||||
* @param array $data Associative array of data
|
||||
*
|
||||
* @return Collection Returns a reference to the object
|
||||
*/
|
||||
public function replace(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add and merge in a Collection or array of key value pair data.
|
||||
*
|
||||
* @param Collection|array $data Associative array of key value pair data
|
||||
*
|
||||
* @return Collection Returns a reference to the object.
|
||||
*/
|
||||
public function merge($data)
|
||||
{
|
||||
if ($data instanceof self) {
|
||||
$data = $data->getAll();
|
||||
}
|
||||
|
||||
if (!$this->data) {
|
||||
$this->data = $data;
|
||||
} else {
|
||||
foreach ($data as $key => $value) {
|
||||
$this->add($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Over write key value pairs in this collection with all of the data from an array or collection.
|
||||
*
|
||||
* @param array|\Traversable $data Values to override over this config
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function overwriteWith($data)
|
||||
{
|
||||
foreach ($data as $k => $v) {
|
||||
$this->set($k, $v);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Collection containing all the elements of the collection after applying the callback function to each
|
||||
* one. The Closure should accept three parameters: (string) $key, (string) $value, (array) $context and return a
|
||||
@ -226,117 +277,45 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
}
|
||||
|
||||
/**
|
||||
* Add and merge in a Collection or array of key value pair data.
|
||||
* Iterates over each key value pair in the collection passing them to the Closure. If the Closure function returns
|
||||
* true, the current value from input is returned into the result Collection. The Closure must accept three
|
||||
* parameters: (string) $key, (string) $value and return Boolean TRUE or FALSE for each value.
|
||||
*
|
||||
* @param Collection|array $data Associative array of key value pair data
|
||||
*
|
||||
* @return Collection Returns a reference to the object.
|
||||
*/
|
||||
public function merge($data)
|
||||
{
|
||||
if ($data instanceof self) {
|
||||
$data = $data->getAll();
|
||||
} elseif (!is_array($data)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (empty($this->data)) {
|
||||
$this->data = $data;
|
||||
} else {
|
||||
foreach ($data as $key => $value) {
|
||||
$this->add($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess implementation of offsetExists()
|
||||
*
|
||||
* @param string $offset Array key
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return $this->hasKey($offset) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess implementation of offsetGet()
|
||||
*
|
||||
* @param string $offset Array key
|
||||
*
|
||||
* @return null|mixed
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->get($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess implementation of offsetGet()
|
||||
*
|
||||
* @param string $offset Array key
|
||||
* @param mixed $value Value to set
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* ArrayAccess implementation of offsetUnset()
|
||||
*
|
||||
* @param string $offset Array key
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
$this->remove($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a specific key value pair
|
||||
*
|
||||
* @param string $key A key to remove
|
||||
* @param \Closure $closure Closure evaluation function
|
||||
* @param bool $static Set to TRUE to use the same class as the return rather than returning a Collection
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function remove($key)
|
||||
public function filter(\Closure $closure, $static = true)
|
||||
{
|
||||
unset($this->data[$key]);
|
||||
$collection = ($static) ? new static() : new self();
|
||||
foreach ($this->data as $key => $value) {
|
||||
if ($closure($key, $value)) {
|
||||
$collection->add($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the data of the object with the value of an array
|
||||
*
|
||||
* @param array $data Associative array of data
|
||||
*
|
||||
* @return Collection Returns a reference to the object
|
||||
*/
|
||||
public function replace(array $data)
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
return isset($this->data[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a key value pair
|
||||
*
|
||||
* @param string $key Key to set
|
||||
* @param mixed $value Value to set
|
||||
*
|
||||
* @return Collection Returns a reference to the object
|
||||
*/
|
||||
public function set($key, $value)
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
$this->data[$key] = $value;
|
||||
return isset($this->data[$offset]) ? $this->data[$offset] : null;
|
||||
}
|
||||
|
||||
return $this;
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->data[$offset] = $value;
|
||||
}
|
||||
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->data[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -416,20 +395,4 @@ class Collection implements \ArrayAccess, \IteratorAggregate, \Countable, ToArra
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Over write key value pairs in this collection with all of the data from an array or collection.
|
||||
*
|
||||
* @param array|\Traversable $data Values to override over this config
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function overwriteWith($data)
|
||||
{
|
||||
foreach ($data as $k => $v) {
|
||||
$this->set($k, $v);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -90,9 +90,6 @@ class CollectionTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
$this->coll->merge($params);
|
||||
$this->assertEquals($this->coll->getAll(), $params);
|
||||
|
||||
// Pass an invalid value and expect the same unaltered object
|
||||
$this->assertEquals($this->coll->merge(false), $this->coll);
|
||||
|
||||
// Pass the same object to itself
|
||||
$this->assertEquals($this->coll->merge($this->coll), $this->coll);
|
||||
}
|
||||
@ -421,7 +418,7 @@ class CollectionTest extends \Guzzle\Tests\GuzzleTestCase
|
||||
), $c->getAll());
|
||||
|
||||
try {
|
||||
$c = Collection::fromConfig(null, null, array('a'));
|
||||
$c = Collection::fromConfig(array(), array(), array('a'));
|
||||
$this->fail('Exception not throw when missing config');
|
||||
} catch (InvalidArgumentException $e) {
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user