loophp / collection

A (memory) friendly, easy, lazy and modular collection class.
https://loophp-collection.rtfd.io/
MIT License
721 stars 35 forks source link
collection generator immutable iterator yield

![Try!][phpsandbox image] ![Latest Stable Version][latest stable version] ![GitHub stars][github stars] ![Total Downloads][total downloads] ![GitHub Workflow Status][github workflow status] [![Scrutinizer code quality][code quality]][scrutinizer link] ![Type Coverage][type coverage] [![Code Coverage][code coverage]][scrutinizer link] [![Mutation testing badge][mutation badge url]][mutation badge link] ![License][license] ![Donate!][donate github]

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:

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][18] for the full explanation and benefits.

This library has been inspired by:

Features

Installation

composer require loophp/collection

Usage

Check out the usage page for both trivial and more advanced use cases.

Dependencies

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][contributing doc page].

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

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.

[phpsandbox image]: https://img.shields.io/badge/Try%20it-online%20!-brightgreen?style=flat-square

[scrutinizer link]: https://scrutinizer-ci.com/g/loophp/collection/?branch=master

[latest stable version]: https://img.shields.io/packagist/v/loophp/collection.svg?style=flat-square [github stars]: https://img.shields.io/github/stars/loophp/collection.svg?style=flat-square [total downloads]: https://img.shields.io/packagist/dt/loophp/collection.svg?style=flat-square [github workflow status]: https://img.shields.io/github/actions/workflow/status/loophp/collection/tests.yml?branch=master&style=flat-square [code quality]: https://img.shields.io/scrutinizer/quality/g/loophp/collection/master.svg?style=flat-square [type coverage]: https://img.shields.io/badge/dynamic/json?style=flat-square&color=color&label=Type%20coverage&query=message&url=https%3A%2F%2Fshepherd.dev%2Fgithub%2Floophp%2Fcollection%2Fcoverage [code coverage]: https://img.shields.io/scrutinizer/coverage/g/loophp/collection/master.svg?style=flat-square [license]: https://img.shields.io/packagist/l/loophp/collection.svg?style=flat-square [donate github]: https://img.shields.io/badge/Sponsor-Github-brightgreen.svg?style=flat-square [donate paypal]: https://img.shields.io/badge/Sponsor-Paypal-brightgreen.svg?style=flat-square [mutation badge url]: https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Floophp%2Fcollection%2Fmaster [mutation badge link]: https://dashboard.stryker-mutator.io/reports/github.com/loophp/collection/master

[18]: https://loophp-collection.readthedocs.io/en/stable/pages/usage.html#working-with-keys-and-values

[40]: https://www.reddit.com/r/PHP/comments/csxw23/a_stateless_and_modular_collection_class/ [41]: https://www.reddit.com/r/PHP/comments/i2u2le/release_of_version_200_of_loophpcollection/

[31]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

[contributing doc page]: https://loophp-collection.readthedocs.io/en/latest/pages/contributing.html