Download the PHP package rexlabs/array-object without Composer
On this page you can find all versions of the php package rexlabs/array-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package array-object
Deprecated
This library isn't in active development. Various other collection libraries provide most functionality that we need out of the box.
Bug fixes only.
Array Object
ArrayObject is a PHP library that provides a light-weight interface for working fluently with array's.
ArrayObject provides a wrapper around PHP's built-in array
type which includes methods for filtering, and retrieving items, and conveniently treats individual items and collections as the same object.
This is especially useful for working with JSON responses from API requests.
This library is used by the Hyper Http Client to make extracting data from responses super intuitive.
Installation
Usage
The examples below are based on the follow input data.
Basics
Note: All of the methods gracefully handle treating an individual item as an array.
get($key, $default = null)
The get()
method allows you to fetch the value of a property, and receive a default if the value does not exist. It also supports depth which is indicated with a .
:
You can also fetch the properties using object property syntax (we overload __get()
to achieve this):
getRaw($key, $default = null)
When a value fetched via get('some.key')
is an array, it is boxed into an instance of ArrayObject
.
The getRaw()
method, however, does not perform any 'boxing' on the returned value.
set($key, $value, $onlyIfExists = false)
The set()
method allows you to set a property using dot notation. It will automatically create the
underlying array structure:
When the $onlyIfExists
flag is passed as true
, it will only set that property if the key already exists.
getOrFail($key)
Similar to get()
but will throw a InvalidPropertyException
when the key is not found.
has($key)
To test for the existence of an element, use the has()
method:
each(callback)
Looping over a collection can be acheived by passing a callback to each()
:
If you have a single item, it still works.
iterator
Since ArrayObject
implements an Iterator interface, you can simply foreach()
over the object. This works for single items or collections.
pluck($key)
Returns a new collection which contains the plucked property.
pluckArray($key)
Returns a new array of the plucked property.
This provides a shortcut from pluck($key)->toArray()
:
count()
You can call count off any node:
Note: When called on a single item (ie. not a collection), it will return 1.
hasItems()
Returns true when the collection contains at least one item.
Note: When called on a single item (ie. not a collection), it will return true
filter(callback|array $filter)
Apply either a callback, or an array of "where" conditions, and only return items that match.
Using a callback, each item is an instance of ArrayObject:
You can also specify a list of conditions using an array:
toArray()
Getting the original array is available via the toArray()
method:
toJson()
Returns a json encoded string of the underlying array:
isCollection()
Determines if the underlying array is a collection.
unshift($val[, $val...])
Adds one or more items to the start of the collection. If the array is not currently a collection it will be converted to a collection with one element.
Note: you can pass ArrayObject
's as values.
shift()
Pulls the first item off the collection.
If the array is not currently a collection it will be converted to a collection with one element.
push($val[, $val...])
Adds one or more items to the end of the collection. If the array is not currently a collection it will be converted to a collection with one element.
Note: you can pass ArrayObject
's as values.
pop()
Pulls the last item off the collection.
If the array is not currently a collection it will be converted to a collection with one element.
Contributing
Contributions are welcome, please submit a pull-request or create an issue. Your submitted code should be formatted using PSR-1/PSR-2 standards.
About
- Author: Jodie Dunlop
- License: MIT
- Copyright (c) 2018 Rex Software Pty Ltd