loophp / collection
A (memory) friendly, easy, lazy and modular collection class.
Fund package maintenance!
drupol
Installs: 351 278
Dependents: 10
Suggesters: 1
Security: 0
Stars: 722
Watchers: 13
Forks: 35
Open Issues: 3
Requires
- php: >= 8.1
- loophp/iterators: ^3.2.0
Requires (Dev)
- ext-pcov: *
- amphp/parallel-functions: ^1
- doctrine/collections: ^2
- drupol/php-conventions: ^5
- infection/infection: ^0.27 || ^0.28 || ^0.29
- loophp/phpunit-iterable-assertions: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/php-code-coverage: ^10
- phpunit/phpunit: ^10
- psr/cache: ^2.0 || 3.0
- symfony/cache: ^6
- vimeo/psalm: ^5
- dev-master
- 7.6.1
- 7.6.0
- 7.5.2
- 7.5.1
- 7.5.0
- 7.4.0
- 7.3.0
- 7.2.1
- 7.2.0
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.0
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.1.0
- 5.0.0
- 4.1.0
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.0
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.7.x-dev
- 2.7.4
- 2.7.3
- 2.7.2
- 2.7.1
- 2.7.0
- 2.6.3
- 2.6.2
- 2.6.1
- 2.6.0
- 2.5.5
- 2.5.4
- 2.5.3
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.0
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.1
- 1.1.0
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.0.17
- 0.0.13
- 0.0.12
- 0.0.10
- 0.0.9
- 0.0.8
- 0.0.7
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-refactor-exploring-new-ideas
- dev-sort-improvements
- dev-fix/cache-operation
- dev-no-name-yet
- dev-fix-distinct-bug-infinite-loop
- dev-feat/add-convolve-operation
- dev-feat/add-operate-operation
- dev-add-abstract-class
- dev-documentation-refactoring
- dev-refactor/update-pipe
- dev-ci/tests-on-php-8.1
- dev-feature/add-find-helper
- dev-Radiergummi-feature/add-find-helper
- dev-refactor/remove-static-of-method
- dev-refactor/remove-AbstractOperation
- dev-fix/update-associate-and-flip-operation
- dev-experiment/update-pipe-operation
- dev-refactor/update-Operation-interface-remove-of-method
- dev-sa/fix-map-operation
- dev-feature/add-iterator-factory
- dev-refactor/switch-to-phpunit
- dev-feature/use-loophp-fpt
- dev-feature/higher-order-collection-proxy
- dev-feature/partition-generator-issue
- dev-add-nix-shell-file
- dev-update-annotations-remove-prefix
- dev-aszenz/master
- dev-ci-php-8
This package is auto-updated.
Last update: 2024-12-11 06:29:30 UTC
README
PHP Collection
Description
Collection is a functional utility library for PHP greater than 7.4, including PHP 8.0.
It's similar to other collection libraries based on regular PHP arrays, but with a lazy mechanism under the hood that strives to do as little work as possible while being as flexible as possible.
Functions like array_map()
, array_filter()
and
array_reduce()
are great, but they create new arrays and everything is
eagerly done before going to the next step. Lazy collection leverages PHP's
generators, iterators, and yield statements to allow you to work with very large
data sets while keeping memory usage as low as possible.
For example, imagine your application needs to process a multi-gigabyte log file while taking advantage of this library's methods to parse the file. Instead of reading and storing the entire file into memory at once, this library may be used to keep only a small part of the file in memory at a given time.
On top of this, this library:
- is immutable,
- is extendable,
- extensively uses S.O.L.I.D. principles,
- leverages the power of PHP generators and iterators,
- extensively tested,
- uses strict types,
- framework agnostic.
Except for a few methods, most methods are pure and return a new Collection object.
Also, unlike regular PHP arrays where keys must be either of type int
or
string
, this collection library lets you use any kind of type for keys:
integer
, string
, object
, array
, ... anything! This library could be a
valid replacement for \SplObjectStorage but with much more features. This
way of working opens up new perspectives and another way of handling data, in a
more functional way.
And last but not least, collection keys are preserved throughout most operations; while it might lead to some confusion at first, please carefully read this example for the full explanation and benefits.
This library has been inspired by:
- Laravel Support Package
- DusanKasan/Knapsack
- mtdowling/transducers
- Ruby Array
- Collect.js
- nikic/iter
- Haskell
- Ramda
- Lazy.js
Features
-
Decoupled: Each Collection method is a shortcut to one isolated standard class, each operation has its own responsibility. Usually, the arguments needed are standard PHP variables like
int
,string
,callable
oriterator
. It allows users to use those operations individually, at their own will, to build up something custom. Currently, more than 100 operations are available in this library. This library is an example of what you can do with all those small bricks, but nothing prevents users from using an operation on its own as well. -
It takes function first, data-last: In the following example, multiple operations are created. The data to be operated on is generally supplied at last.
<?php $input = ['foo', 'bar', 'baz']; // Using the Collection library $collection = Collection::fromIterable($input) ->filter(static fn(string $userId): bool => 'foo' !== $userId) ->reverse(); foreach ($collection as $item); // ['baz','bar'] // Using single operations. $pipe = Pipe::of()( Reverse::of(), Filter::of()($filterCallback) ); foreach ($pipe($input) as $item); // ['baz','bar']
More information about this in the Brian Lonsdorf's conference, even if this is for JavaScript, those concepts are common in other programming languages.
In a nutshell, the combination of currying and function-first enables the developer to compose functions with very little code (often in a “point-free” fashion), before finally passing in the relevant user data.
-
Operations are stateless and curried by default: This currying makes it easy to compose functions to create new functions. Because the API is function-first, data-last, you can continue composing and composing until you build up the function you need before dropping in the data. See this Hugh Jackson article describing the advantages of this style.
In the following example, the well-known
flatMap
could be composed of other operations as such:<?php $input = ['foo,bar', 'baz,john']; $flatMap = static fn (callable $callback) => Pipe::of()( Map::of()(static fn(string $name): array => explode(',', $name)), Flatten::of()(1) ); foreach ($flatMap($input) as $item); // ['foo', 'bar', 'baz', 'john']
Installation
composer require loophp/collection
Usage
Check out the usage page for both trivial and more advanced use cases.
Dependencies
- loophp/iterators: A package providing PHP Iterators.
Documentation
On top of well-documented code, the package includes a complete documentation that gets automatically compiled and published upon each commit at https://loophp-collection.rtfd.io.
The Collection Principles will get you started with understanding the elements that are at the core of this package, so you can get the most out of its usage.
The API will give you a pretty good idea of the existing methods and what you can do with them.
We are doing our best to keep the documentation up to date; if you found something odd, please let us know in the issue queue.
Code quality, tests, benchmarks
Every time changes are introduced into the library, Github runs the tests.
The library has tests written with PHPUnit. Feel free to check them out in
the tests/unit/
directory. Run composer phpunit
to trigger the tests.
Before each commit, some inspections are executed with GrumPHP; run
composer grumphp
to check manually.
The quality of the tests is tested with Infection a PHP Mutation testing
framework - run composer infection
to try it.
Static analyzers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.
Contributing
Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end, read more about it in the documentation.
For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.
Sponsor me on Github and/or any of the contributors.
On the internet
- Reddit announcement thread
- Reddit release 2.0.0 thread
- Featured in PHPStorm Annotated August 2020
- Youtube presentation for AFUP Days 2021
Changelog
See CHANGELOG.md for a changelog based on git commits.
For more detailed changelogs, please check the release changelogs.