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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package collection

Collection

License

Overview

Models a type-safe, fluent collection API for PHP, unifying arrays, iterators, and generators behind a single Collectible contract. Supports both eager and lazy evaluation pipelines, with chainable operations for mapping, filtering, grouping, reducing, and joining. Designed for predictable memory and CPU profiles in data-intensive workloads.

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.

Extending Collection

Domain collections should extend the Collection class to inherit all collection behavior:

Mapping to and from a source

Collection implements the IterableMappable contract from tiny-blocks/mapper, so a collection maps itself to and from arrays, JSON, and iterables. PHP carries no runtime element type for a collection, so a typed collection declares its element with the #[ElementType] attribute. The mapper builds each element through the declared type. Without the attribute, elements pass through unchanged.

An invoice the collection holds.

The collection declares its element type with the attribute.

The mapper builds the typed collection from a list of rows and serializes it back symmetrically.

toArray and toJson on the collection produce the same portable shape, so a collection serializes without an explicit Mapper at the call site.

Writing

These methods enable adding, removing, and modifying elements in the Collection.

Adding elements

Merging collections

Removing elements

Filtering

These methods enable filtering elements in the Collection based on specific conditions.

Filter by predicate

Ordering

These methods enable sorting elements in the Collection based on the specified order and optional comparator.

Sort by order and custom comparator

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

Check if empty

Retrieve by condition

Retrieve single elements

Retrieve collection segments

Comparing

These methods enable comparing collections to check for equality or to verify element membership.

Check if collection contains element

Compare collections for equality

Aggregation

These methods perform operations that return a single value based on the Collection's content, such as summing or combining elements.

Transforming

These methods allow the Collection's elements to be transformed or converted into different formats.

Applying actions without modifying elements

Grouping elements

Mapping elements

Flattening elements

Convert to array

Convert to JSON

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.

03. What is the difference between eager and lazy evaluation?

Both modes share the same execution model. Transforming operations append a stage to the pipeline at the call site without iterating. Terminal operations run the fused pass over all chained stages.

The difference is what each mode does at creation and after the fused pass completes:

Notation. n = source size at the terminal call. P = total cost of the fused pass, equal to the sum of the per-element contributions of every chained stage. For a pipeline of pure per-element stages, P is O(n · s), where s is the number of stages. Non-linear stages (sort, groupBy) dominate P.

Creation

Method Eager Lazy
createFrom O(n) time, O(n) space. Iterates the input once and stores it.
createFromEmpty O(1) time, O(1) space.
createFromClosure O(n) time, O(n) space. Invokes the factory and stores the result.
createLazyFrom O(1) time, O(1) space. Stores the iterable by reference.
createLazyFromEmpty O(1) time, O(1) space.
createLazyFromClosure O(1) time, O(1) space. Stores the factory without invoking it.

Transforming

Transforming methods append a pipeline stage at the call site and execute only during the fused pass.

Method Call site (both modes) Contribution to the fused pass
add O(1) time, O(1) space. O(m) time, O(m) space, where m is the number of appended elements.
merge O(1) time, O(1) space. O(m) time, O(m) space, where m is the number of elements in the other collection.
remove O(1) time, O(1) space. O(n) time, O(1) space.
removeAll O(1) time, O(1) space. O(n) time, O(1) space.
filter O(1) time, O(1) space. O(n · p) time, O(1) space, where p is the number of predicates.
flatten O(1) time, O(1) space. O(n + s) time, O(1) space, where s is the total number of nested elements.
map O(1) time, O(1) space. O(n · t) time, O(1) space, where t is the number of transformations.
slice O(1) time, O(1) space. O(min(offset + length, n)) time, O(1) space. Short-circuits once the segment is emitted.
groupBy O(1) time, O(1) space. O(n) time, O(n) space. Buffers all groups before emitting. Breaks streaming.
sort O(1) time, O(1) space. O(n log n) time, O(n) space. Buffers all elements before emitting. Breaks streaming.

Terminal

Terminal methods trigger the fused pass. Eager cells show first call / subsequent calls when they differ. Subsequent calls read the cache without re-running the pipeline.

Method Eager Lazy
count Amortized O(P) / O(1). O(P) per call. Must reach the end.
first Amortized O(P) / O(1). O(P_first) per call. Short-circuits at the first element.
last Amortized O(P) / O(1). O(P) per call. Must reach the end.
getBy Amortized O(P) / O(1). O(P_index) per call. Short-circuits at the requested index.
isEmpty Amortized O(P) / O(1). O(P_first) per call. Short-circuits at the first element.
contains O(P + n) / O(n). Short-circuits at the first match. O(P) per call. Short-circuits at the first match.
findBy O(P + n · p) / O(n · p), where p is the number of predicates. Short-circuits at the first match. O(P + p) per emitted element. Short-circuits at the first match.
each O(P + n · a) / O(n · a), where a is the number of actions. O(P + n · a) per call.
equals O(P + n) / O(n). Short-circuits at the first mismatch. O(P + n) per call. Short-circuits at the first mismatch.
joinToString O(P + n) / O(n) time, O(n) space. O(P + n) per call.
reduce O(P + n) / O(n) time, O(1) intermediate space. O(P + n) per call.
toArray O(P + n) / O(n) time, O(n) space. O(P + n) per call.
toJson O(P + n) / O(n) time, O(n) space. O(P + n) per call.

Eager aggregation terminals iterate the cached array without re-running the pipeline. Lazy terminals re-run the pipeline on every call. Eager indexing terminals (count, first, last, getBy, isEmpty) return in O(1) from the cache after the first access.

License

Collection is licensed under MIT.

Contributing

Please follow the contributing guidelines to contribute to the project.


All versions of collection with dependencies

PHP Build Version
Package Version
Requires php Version ^8.5
tiny-blocks/mapper Version ^3.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tiny-blocks/collection contains the following files

Loading the files please wait ...