- addiing toObj method
- adding merge metho - enhancing getArrayableItems
This commit is contained in:
@ -2,9 +2,9 @@ Ary
|
|||||||
===
|
===
|
||||||
Are you tired from casting objects and arrays to each other? Don't do that anymore! Ary is a light class/function that makes accessing array items more convenient.
|
Are you tired from casting objects and arrays to each other? Don't do that anymore! Ary is a light class/function that makes accessing array items more convenient.
|
||||||
|
|
||||||
1. You can access array items using `->` or `[]` syntax.
|
1. You can access array items using `->` or `['']` syntax.
|
||||||
2. You will get `null` if an index does not exists.
|
2. You will get `null` if an index does not exists (instead of a nasty notification!)
|
||||||
3. You can specify a default value for a missing index.
|
3. You can specify a default value for missing indexes.
|
||||||
|
|
||||||
* The class (`Ary()`) requires PHP 5.4 or newer.
|
* The class (`Ary()`) requires PHP 5.4 or newer.
|
||||||
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
||||||
@ -18,7 +18,7 @@ $ary = ary();
|
|||||||
//setting and getting array items.
|
//setting and getting array items.
|
||||||
|
|
||||||
$ary = ary(2, 4, 6, 8); //or
|
$ary = ary(2, 4, 6, 8); //or
|
||||||
$ary = ary([2, 4, 6, 8]); //or
|
$ary = ary([2, 4, 6, 8]);
|
||||||
|
|
||||||
$ary = ary(['x' => 'foo', 'y' => 'bar']);
|
$ary = ary(['x' => 'foo', 'y' => 'bar']);
|
||||||
$foo = $ary->x; //or
|
$foo = $ary->x; //or
|
||||||
|
28
src/Ary.php
28
src/Ary.php
@ -58,7 +58,12 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
|||||||
if ($items instanceof self) {
|
if ($items instanceof self) {
|
||||||
return $items->all();
|
return $items->all();
|
||||||
}
|
}
|
||||||
|
if (method_exists($items, 'toArray')) {
|
||||||
|
return $items->toArray();
|
||||||
|
}
|
||||||
|
if ($items instanceof JsonSerializable) {
|
||||||
|
return json_decode(json_encode($items), true);
|
||||||
|
}
|
||||||
return (array)$items;
|
return (array)$items;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,6 +129,17 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
|||||||
return $this->all();
|
return $this->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merge the collection with the given items.
|
||||||
|
*
|
||||||
|
* @param mixed $items
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function merge($items)
|
||||||
|
{
|
||||||
|
return new static(array_merge($this->items, $this->getArrayableItems($items)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a CachingIterator instance.
|
* Get a CachingIterator instance.
|
||||||
*
|
*
|
||||||
@ -241,4 +257,14 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
|||||||
{
|
{
|
||||||
return $this->offsetExists($key);
|
return $this->offsetExists($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the collection of items as a plain object.
|
||||||
|
*
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
public function toObject()
|
||||||
|
{
|
||||||
|
return (object)$this->all();
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user