escapio/php-iterables

Utility functions for working with iterables

v2.2.0 2024-04-30 15:11 UTC

README

Description

PHP-Iterables is a simple utility library for PHP to provide an easy and consistent way of using iterables, no matter which type of iterable you work on: arrays, iterators or generators.

Furthermore, it allows you to chain several iterator functions in a fluent, easier-to-read way.

Features

Check out more detailed examples in the examples file.

This library includes common functions like

$double_iterable = map($iterable_of_numbers, fn($number) => $number * 2);
$filtered_iterable = filter($iterable_of_numbers, fn($number) => $number < 5);
$iterable = function () {
    yield 1;
    yield 2;
}
toArray($iterable); // [1, 2]

The iterable-Builder allow you to combine these functions in a fluent syntax:

(new \Escapio\Iterables\Builder())
  ->from(['Alice', 'Bob', 'Chuck'])
  ->map(strtolower(...))
  ->filter(fn ($name) => $name !== 'chuck')
  ->loop(function ($name) {
    echo $name . PHP_EOL;
  });
  // "alice"
  // "bob"

Installation

Install with composer:

composer require escapio/php-iterables

Contributing

Feel free to create feature request or report bugs via GitHub.

If you like to contribute, make sure that all tests and code style rules are satisfied, otherwise the CI will fail.

Tests

Command for executing the PHPUnit tests:

composer test

Code-Style

This library uses PHP CS Fixer for code formatting. Run the formatter with:

composer code-style:fix

Also see the CODE_OF_CONDUCT.

Changelog

See CHANGELOG.md

The changelog is managed by the composer package marcocesarato/php-conventional-changelog. There are some convenient scripts in the composer.json file to update the CHANGELOG and tag the commit with the current version:

  • composer release:patch
  • composer release:minor
  • composer release:major

License

PHP-Iterables is made available under the MIT License (MIT). Please see License File for more information.