A progressive PHP framework for building effecient and scalable server-side applications.
A powerful and easy-to-use library for creating and managing collections of related objects in AssegaiPHP. This library provides a simple and intuitive interface for working with arrays of objects, making it easy to perform common operations such as filtering, mapping, and reducing.
You can install the library using composer by running the following command:
composer require assegaiphp/collections
To create a new collection, you can use the Assegai\Collections\Collection
class. This class provides a simple and intuitive interface for working with arrays of objects, making it easy to perform common operations such as filtering, mapping, and reducing.
Here's an example of how to create a new collection and add some items to it:
use Assegai\Collections\Collection;
$collection = new Collection();
$collection->add(1);
$collection->add(2);
$collection->add(3);
Once you have a collection, you can use the various methods provided by the class to manipulate the items in the collection. For example, you can use the filter
method to filter the items in the collection based on a certain condition:
$filteredCollection = $collection->filter(function($item) {
return $item > 1;
});
The map
method can be used to transform the items in the collection:
$mappedCollection = $collection->map(function($item) {
return $item * 2;
});
You can also use the reduce
method to reduce the collection to a single value:
$sum = $collection->reduce(function($carry, $item) {
return $carry + $item;
}, 0);
The API of the Assegai\Collections\Collection
class is designed to be simple and intuitive. It provides the following methods:
add(mixed $item)
: Add an item to the collectionremove(mixed $item)
: Remove an item from the collectionfilter(callable $callback)
: Filter the items in collection based on a callback function.