Download the PHP package tiny-blocks/collection without Composer
On this page you can find all versions of the php package tiny-blocks/collection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tiny-blocks/collection
More information about tiny-blocks/collection
Files in tiny-blocks/collection
Package collection
Short Description Provides a flexible and efficient API to manipulate, iterate, and manage collections in a structured and type-safe manner.
License MIT
Homepage https://github.com/tiny-blocks/collection
Informations about the package collection
Collection
- Overview
- Installation
- How to use
- Writing
- Filtering
- Ordering
- Retrieving
- Comparing
- Aggregation
- Transforming
- FAQ
- License
- Contributing
Overview
The Collection
library provides a flexible and efficient API to manipulate, iterate, and manage collections in a
structured and type-safe manner.
It leverages PHP's Generators for optimized memory usage and lazy evaluation, ensuring that large datasets are handled efficiently without loading all elements into memory at once.
The library supports adding, removing, filtering, sorting, and transforming elements.
Installation
How to use
The library exposes the available behaviors through the Collectible
interface and provides utilities to manipulate
collections of various types.
Concrete implementation
The Collection
class implements the Collectible
interface and provides a concrete implementation for handling
collections.
It allows for adding, removing, filtering, and sorting elements, as well as transforming them into different formats like arrays and JSON.
The class is designed to work with generic key-value pairs, ensuring type safety and flexibility for a variety of use cases.
Writing
These methods enable adding, removing, and modifying elements in the Collection.
Adding elements
add
: Adds one or more elements to the Collection.
Removing elements
-
remove
: Removes a specific element from the Collection. -
removeAll
: Removes elements from the Collection.-
With a filter: Removes only the elements that match the provided filter.
- Without a filter: Removes all elements from the Collection.
-
Filtering
These methods enable filtering elements in the Collection based on specific conditions.
Filter by predicate
-
filter
: Filters elements in the Collection.-
With predicates: Filter elements are based on the provided predicates.
- Without predicates: Removes all empty or false values (e.g.,
null
,false
, empty arrays).
-
Ordering
These methods enable sorting elements in the Collection based on the specified order and optional predicates.
Sort by order and custom predicate
-
sort
: Sorts the Collection.By default,
Order::ASCENDING_KEY
is used.Sort the Collection using a custom predicate to determine how elements should be compared.
Retrieving
These methods allow access to elements within the Collection, such as fetching the first or last element, counting the elements, or finding elements that match a specific condition.
Retrieve count
count
: Returns the total number of elements in the Collection.
Retrieve by condition
findBy
: Finds the first element that matches one or more predicates.
Retrieve single elements
-
first
: Retrieves the first element from the Collection or returns a default value if the Collection is empty. -
getBy
: Retrieves an element by its index or returns a default value if the index is out of range. last
: Retrieves the last element from the Collection or returns a default value if the Collection is empty.
Retrieve collection elements
slice
: Extracts a portion of the collection, starting at the specified index and retrieving the specified number of elements. If length is negative, it excludes many elements from the end of the collection. If length is not provided or set to -1, it returns all elements from the specified index to the end of the collection.
Comparing
These methods enable comparing collections to check for equality or to apply other comparison logic.
Check if collection contains element
contains
: Checks if the Collection contains a specific element.
Compare collections for equality
equals
: Compares the current Collection with another collection to check if they are equal.
Aggregation
These methods perform operations that return a single value based on the Collection's content, such as summing or combining elements.
reduce
: Combines all elements in the Collection into a single value using the provided aggregator function and an initial value. This method is helpful for accumulating results, like summing or concatenating values.
Transforming
These methods allow the Collection's elements to be transformed or converted into different formats.
Applying actions without modifying elements
each
: Executes actions on each element in the Collection without modification. The method is helpful for performing side effects, such as logging or adding elements to another collection.
Grouping elements
groupBy
: Groups the elements in the Collection based on the provided grouping criterion.
Mapping elements
map
: Applies transformations to each element in the Collection and returns a new collection with the transformed elements.
Flattening elements
-
flatten
: Flattens a collection by removing any nested collections and returning a single collection with all elements in a single level.This method recursively flattens any iterable elements, combining them into one collection, regardless of their nesting depth.
Convert to array
-
toArray
: Converts the Collection into an array.By default,
PreserveKeys::PRESERVE
is used.
Convert to JSON
-
toJson
: Converts the Collection into a JSON string.By default,
PreserveKeys::PRESERVE
is used.
FAQ
01. Why is my iterator consumed after certain operations?
The Collection
class leverages PHP's Generators to
provide lazy evaluation, meaning elements are only generated as needed.
It cannot be reused once a generator is consumed (i.e., after you iterate over it or apply certain operations).
This behavior is intended to optimize memory usage and performance but can sometimes lead to confusion when reusing an
iterator after operations like count
, toJson
, or toArray
.
02. How does lazy evaluation affect memory usage in Collection?
Lazy evaluation, enabled by PHP's Generators, allows
Collection
to handle large datasets without loading all elements into memory at once.
This results in significant memory savings when working with large datasets or performing complex chained operations.
However, this also means that some operations will consume the generator, and you cannot access the elements unless you
recreate the Collection
.
License
Collection is licensed under MIT.
Contributing
Please follow the contributing guidelines to contribute to the project.