2017 © Pedro Peláez
 

library collections-php

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

image

mf/collections-php

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  • Thursday, July 26, 2018
  • by MortalFlesh
  • Repository
  • 2 Watchers
  • 3 Stars
  • 2,988 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 31 Versions
  • 41 % Grown

The README.md

MFCollections for PHP

Latest Stable Version Total Downloads License Tests and linting Coverage Status, (*1)

It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features., (*2)

Table of Contents

Installation

composer require mf/collections-php

Requirements

  • PHP ^8.2

Base Interfaces

Check out Documentation for more details., (*3)

IEnumerable

ICollection

IList

A list is an ordered (_possibly immutable_) series of elements of the same type. - extends ICollection - see Mutable list - see Immutable list, (*4)

IMap

A map is an ordered (_possibly immutable_) series of key values pairs. - extends ICollection, ArrayAccess - see Mutable map - see Immutable map, (*5)

ISeq

A sequence is a logical series of elements all of one type. - extends ICollection - see Immutable seq, (*6)

ITuple

A tuple is a grouping of unnamed but ordered values, possibly of different types. - extends IEnumerable, ArrayAccess, Stringable - see Immutable tuple, (*7)

Mutable Collections

Interfaces

  • Mutable\Generic\ICollection, Mutable\Generic\IList, Mutable\Generic\IMap

Mutable\Generic\ListCollection

  • implements Mutable\Generic\IList
  • is eager as possible

Mutable\Generic\Map

  • implements Mutable\Generic\IMap
  • is eager as possible

Mutable\Generic\PrioritizedCollection

  • implements IEnumerable
  • holds items with generic type by priority
  • is eager as possible

Example of strategies by priority

For case when you want to apply only the first strategy which can do what you want. You can add strategies dynamically and still apply them by priority later., (*8)

// initialization of strategies
/** @phpstan-var PrioritizedCollection<StrategyInterface> $strategies */
$strategies = new PrioritizedCollection();
$strategies->add(new DefaultStrategy(), 1);

// added later
$strategies->add(new SpecialStrategy(), 100);

// find and apply first suitable strategy
/** @var StrategyInterface $strategy */
foreach ($strategies as $strategy) {
    if ($strategy->supports($somethingStrategic)) {
        return $strategy->apply($somethingStrategic);
    }
}

Immutable Collections

  • internal state of Immutable\Collection instance will never change from the outside (it is readonly)
$list = new Immutable\ListCollection();
$listWith1 = $list->add(1);

// $list != $listWith1
echo $list->count();        // 0
echo $listWith1->count();   // 1
  • $list is still an empty Immutable\ListCollection
  • $listWith1 is new instance of Immutable\ListCollection with value 1

Interfaces

  • Immutable\Generic\ICollection, Immutable\Generic\IList, Immutable\Generic\IMap, Immutable\Generic\ISeq, Immutable\ITuple

Immutable\Generic\ListCollection

  • implements Immutable\Generic\IList
  • is eager as possible

Immutable\Generic\Map

  • implements Immutable\Generic\IMap
  • is eager as possible

Immutable\Seq

  • implements Immutable\Generic\ISeq
  • is lazy as possible (_even could be Infinite_)
$seq = Seq::infinite()                         // 1, 2, ...
    ->filter(fn ($i) => $i % 2 === 0)   // 2, 4, ...
    ->skip(2)                           // 6, 8, ...
    ->map(fn ($i) => $i * $i)           // 36, 64, ...
    ->takeWhile(fn ($i) => $i < 100)    // 36, 64
    ->reverse()                         // 64, 36
    ->take(1);                          // 64
// for now the Sequence is still lazy

// this will generate (evaluate) the values
$array = $seq->toArray();               // [64]

Immutable\Generic\KVPair

  • always has a Key and the Value
  • key is restricted to int|string so it may be used in the foreach as a key
  • can contain any values

Immutable\Tuple

  • implements Immutable\ITuple
  • must have at least 2 values (_otherwise it is just a single value_)
  • is eager as possible
  • allows destructuring, matching and parsing/formatting
  • can contain any scalar values and/or arrays
    • in string representation of a Tuple, array values must be separated by ; (_not by ,_)

Parsing

Tuple::parse('(foo, bar)')->toArray();                  // ['foo', 'bar']
Tuple::parse('("foo, bar", boo)')->toArray();           // ['foo, bar', 'boo']
Tuple::parse('(1, "foo, bar", true)')->toArray();       // [1, 'foo, bar', true]
Tuple::parse('(1, [2; 3], [four; "Five"])')->toArray(); // [1, [2, 3], ['four', 'five']]

Matching and comparing

Tuple::from([1, 1])->match('int', 'int');                      // true
Tuple::from([1, 2, 3])->isSame(Tuple::of(1, 2, 3));            // true
Tuple::of(10, 'Foo', null)->match('int', 'string', '?string'); // true
Tuple::of(10, [9, 8])->match('int', 'array');                  // true

Parsing and matching

Tuple::parseMatch('(foo, bar)', 'string', 'string')->toArray();        // ['foo', 'bar']
Tuple::parseMatchTypes('(foo, bar)', ['string', 'string'])->toArray(); // ['foo', 'bar']

// invalid types
Tuple::parseMatch('(foo, bar, 1)', 'string', 'string'); // throws \InvalidArgumentException "Given tuple does NOT match expected types (string, string) - got (string, string, int)"

Formatting

Tuple::from([1, 'foo', null])->toString();          // '(1, "foo", null)'

// for URL
Tuple::from(['foo', 'bar'])->toStringForUrl();      // '(foo,bar)'
Tuple::from(['foo-bar', 'boo'])->toStringForUrl();  // '(foo-bar,bar)'
Tuple::from(['mail', 'a@b.com'])->toStringForUrl(); // '(mail,"a@b.com")'

Destructuring

$tuple  = Tuple::of('first', 2, 3); // ('first', 2, 3)
$first  = $tuple->first();          // 'first'
$second = $tuple->second();         // 2
[$first, $second] = $tuple;         // $first = 'first'; $second = 2
[,, $third]       = $tuple;         // 3

Unpacking

sprintf('Title: %s | Value: %s', ...Tuple::of('foo', 'bar')); // "Title: foo | Value: bar"

Merging

  • merging Tuples will automatically flat them (_see last example below_)
$base  = Tuple::of('one', 'two');                       // ('one', 'two')
$upTo3 = Tuple::merge($base, 'three');                  // ('one', 'two', 'three')
$upTo4 = Tuple::merge($base, '3', 'four');              // ('one', 'two', '3', 'four')
$upTo5 = Tuple::merge($base, ['3', '4'], '5');          // ('one', 'two', ['3', '4'], '5')
$upTo5 = Tuple::merge($base, Tuple::of('3', '4'), '5'); // ('one', 'two', '3', '4', '5')

Merging and matching

$base = Tuple::of('one', 'two');                                    // ('one', 'two')
$upTo3 = Tuple::mergeMatch(['string', 'string', 'int'], $base, 3);  // ('one', 'two', 3)

// invalid types
Tuple::mergeMatch(['string', 'string'], $base, 3); // throws \InvalidArgumentException "Merged tuple does NOT match expected types (string, string) - got (string, string, int)."

Sequences and lazy mapping

  • if your Sequence get mapped and filtered many times (_for readability_), it is not a problem
    • map -> map -> filter -> map -> filter -> map will iterate the collection only once (_for applying all modifiers at once_)
    • this modification is done when another method is triggered, so adding new modifier is an atomic operation
  • all the values are generated on the fly, so it may end on out of memory exception

Plans for next versions

  • use Symfony/Stopwatch in unit tests
  • even better documentation (current)

The Versions

26/07 2018

dev-master

9999999-dev

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

26/07 2018

3.10.0

3.10.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

26/07 2018

dev-feature/prioritized-collection

dev-feature/prioritized-collection

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

25/07 2018

dev-feature/add-enumerable-interface

dev-feature/add-enumerable-interface

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

25/07 2018

dev-feature/change-get-iterator

dev-feature/change-get-iterator

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

24/07 2018

3.9.0

3.9.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

23/07 2018

3.8.0

3.8.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

19/07 2018

3.7.0

3.7.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

17/07 2018

3.6.0

3.6.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

13/07 2018

3.5.1

3.5.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

28/06 2018

3.5.0

3.5.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

28/06 2018

3.4.2

3.4.2.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

28/06 2018

3.4.1

3.4.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

24/06 2018

3.3.0

3.3.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

11/06 2018

3.2.2

3.2.2.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

29/05 2018

3.2.1

3.2.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

29/05 2018

dev-feature/add-seq-annotation

dev-feature/add-seq-annotation

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

29/05 2018

3.2.0

3.2.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

29/05 2018

dev-feature/sequences

dev-feature/sequences

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

05/01 2018

dev-feature/dev-sharing

dev-feature/dev-sharing

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

02/01 2018

3.1.1

3.1.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

20/12 2017

dev-feature/create-by-callback

dev-feature/create-by-callback

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

20/12 2017

3.0.1

3.0.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

14/10 2017

3.0.0

3.0.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

12/09 2017

2.1.0

2.1.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

09/08 2017

dev-feature/lazy-mapping

dev-feature/lazy-mapping

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

06/08 2017

2.0.1

2.0.1.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

05/08 2017

2.0.0

2.0.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

17/06 2017

dev-feature/code-health

dev-feature/code-health

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

15/06 2017

dev-feature/interfaces-improved

dev-feature/interfaces-improved

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec

14/09 2016

1.0.0

1.0.0.0

Collections for PHP - It's basically a syntax sugar over classic array structure, which allows you to use it as classic array, but adds some cool features.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Petr Chromec