drupol/collection

This package is abandoned and no longer maintained. The author suggests using the loophp/collection package instead.

A (memory) friendly, easy, lazy and modular collection class.

2.0.4 2020-08-08 06:54 UTC

README

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Donate! Donate!

PHP Collection

Description

Collection is a functional utility library for PHP greater than 7.1.3.

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:

Except 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 let you use any kind of type for keys: integer, string, objects, arrays, ... 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, and it might be leading to some confusions, carefully read this example for the full explanation.

This library has been inspired by:

Installation

composer require loophp/collection

Documentation

On top of a complete documented code, the package include a full documentation that gets automatically compiled and published upon each commit at https://loophp-collection.rtfd.io.

The API will give you a pretty good idea of the existing methods and what you can do with it.

Do not forget to check out the usage and examples pages.

I'm doing my best to keep the documentation up to date, if you found something odd, please let me know in the issue queue.

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections (currently 14) 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 analysers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.

PHP Insights is also launched in Github actions just for information. (example of PHP Insights report, you must be logged on Github to see it)

Contributing

Feel free to contribute by sending Github pull requests. I'm quite reactive :-)

If you can't contribute to the code, you can also sponsor me on Github or Paypal.

On the internet