Skip to content

roukmoute/enumerable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enumerable

Provides a set of methods for querying objects.
Enumerable is a collection library for PHP >= 7.1 that implements most of the sequence operations proposed by collection pipeline object methods.

Installation

Require this package using Composer.

composer require roukmoute/enumerable

Operation Catalog

Here is a catalog of the operations that you often find in collection pipelines :

filter

Runs a boolean function on each element and only puts those that pass into the output.

You can use this function to keep only the items of the input you want to work with.
For example:

$query = (new \Enumerable\Enumerable([0, 30, 20, 15, 90, 85, 40, 75]))->filter(
    function ($number, $index) {
        return $number <= $index * 10;
    }
);

foreach ($query as $number) {
    echo $number . PHP_EOL;
}

/*
 This code produces the following output:

 0
 20
 15
 40
*/

concat

Concatenates collections into a single collection

$query = (new \Enumerable\Enumerable([1, 2, 3]))->concat([4, 5]);

foreach ($query as $name) {
    echo $name . PHP_EOL;
}

// This code produces the following output:
//
// 1
// 2
// 3
// 4
// 5

If you want to concatenate more than two collections :

$query = (new \Enumerable\Enumerable([1, 2, 3]))->concat([4, 5])->concat([6]);

foreach ($query as $name) {
    echo $name . PHP_EOL;
}

// This code produces the following output:
//
// 1
// 2
// 3
// 4
// 5
// 6

difference

Remove the contents of the supplied list from the pipeline

$query = (new \Enumerable\Enumerable([1, 1, 2, 2, 3, 4]))->difference([1, 3]);

foreach ($query as $number) {
    echo $number . PHP_EOL;
}

// This code produces the following output:
//
// 2
// 2
// 4

distinct

Removes duplicate elements

Returns a new list with any duplicates removed.

$query = (new \Enumerable\Enumerable([1, 2, 3, 2, 1]))->distinct();

foreach ($query as $number) {
    echo $number . PHP_EOL;
}

// This code produces the following output:
//
// 1
// 2
// 3

slice

Return a sub-sequence of the list between the given first and last positions.

If you want some of the list, you can take a slice of the list.

$query = (new \Enumerable\Enumerable([1, 2, 3, 4, 5, 6]))->slice(2, 4);

foreach ($query as $number) {
    echo $number . PHP_EOL;
}

/**
 * This code produces the following output:
 * 
 * 3
 * 4
 * 5
 */

About

Provides a set of methods for querying objects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages