Download the PHP package markhj/collection without Composer
On this page you can find all versions of the php package markhj/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package collection
This Collection library is best thought of as an "upgraded object-oriented array".
This package provides features to write more beautiful and readable code.
Install
You can install this package to your PHP project with:
You will need Composer 2.
Example
Methods
There are examples of some of the methods further down.
Associative collection
Associative collections provide all the same functionality as the non-associative, but uses key/value pairing similar to PHP's associative arrays.
``
Language features
Typecasting
When you typecast the collection to string, it will return the JSON structure of the collection.
Iteration
You can use the ` operator on the collection instance.
Example use of other methods
map
The ` method iterates over every element of the collection, and assigns the returned value of the handler to the element in the collection. ``
forEach
The ` method is in many ways similar to the map method, but it will not assign the returned value to the element in the collection. Instead you can carry out actions which aren't directly related to the collection itself. ``
sort
The ` method sorts the collection elements by a defined handler (callable) ``
filter
The ` method removes unwanted elements from the collection. ``
retrieve
The ` method creates a new (cloned) collection based on requested elements. This allows you to extract a filtered subset of elements, without affecting the original collection itself. ``
has
The method returns true if the target item is found in the collection. `` The
method can also compare objects.
hasAnyOf
``
hasAllOf
``
merge
With the ` method you can merge another collection into the current. ``
You can use the ` to protect the values of the existing collection. ``
These methods are closely related to and
.
Inheritance
You can use the "out of the box" collection objects provied in the package, but in many instances it makes sense to write custom collections which inherit the Collection. This allows you to expand with relevant methods.
Validate
When you create new collections you might want to restrict it to certain data types. For this, you can override the method
By default this method always returns true - meaning it accepts any type.
You can for example limit to strings:
Or to certain class names:
If a wrong data type is passed the ` is thrown.
Examples
For example, you can create a collection of date/time objects, where you add methods to retrieve the earliest and latest instances.
Another example would be a collection of payment methods, where you add methods relevant to your business logic. It could be a boolean method to check for expired credit cards, a method to get all credit cards, or a method to retrieve default payment method.
Options
Graceful handling of missing keys
When you're working on a class inheriting the Collection object, you can set the property to true, to enable graceful handling of trying to access a collection element that doesn't exist. In normal circumstances, trying to access an element that doesn't exist will throw
. If graceful handling is enabled and you try access a non-existent key
` will be returned.