2017 © Pedro Peláez
 

library enumerable

Provides a set of methods for querying objects.

image

roukmoute/enumerable

Provides a set of methods for querying objects.

  • Tuesday, July 17, 2018
  • by roukmoute
  • Repository
  • 2 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

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., (*1)

Installation

Require this package using Composer., (*2)

composer require roukmoute/enumerable, (*3)

Operation Catalog

Here is a catalog of the operations that you often find in collection pipelines :, (*4)

filter

, (*5)

Runs a boolean function on each element and only puts those that pass into the output., (*6)

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

$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

, (*8)

Concatenates collections into a single collection, (*9)

$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 :, (*10)

$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

, (*11)

Remove the contents of the supplied list from the pipeline, (*12)

$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

, (*13)

Removes duplicate elements, (*14)

Returns a new list with any duplicates removed., (*15)

$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

, (*16)

Return a sub-sequence of the list between the given first and last positions., (*17)

If you want some of the list, you can take a slice of the list., (*18)

$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
 */

The Versions

17/07 2018

dev-master

9999999-dev

Provides a set of methods for querying objects.

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Mathias STRASSER