2017 © Pedro Peláez
 

library collection

General-Purpose Collection Library for PHP

image

imunhatep/collection

General-Purpose Collection Library for PHP

  • Thursday, April 20, 2017
  • by Imunhatep
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,779 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 38 Forks
  • 0 Open issues
  • 23 Versions
  • 13 % Grown

The README.md

PHP Collection

SensioLabsInsight Build Status, (*1)

PHP Collection library., (*2)

Based on J. M. Schmitt work, refactored and updated - now requires PHP 8.0. Updated with several useful methods like ::flatMap(), ::map(), ::foldLeft(), ::exists(), ::headOption(), ::lastOption(), ::tail() and e.t.c. Inspired by Scala collections. Added types., (*3)

Installation

PHP Collection can easily be installed via composer (in process), (*4)

composer require imunhatep/collection

or add it to your composer.json file., (*5)

Note

Collections can be seen as more specialized arrays for which certain contracts are guaranteed., (*6)

Supported Collections:, (*7)

  • Sequence, (*8)

    • Keys: numerical, consequentially increasing, no gaps
    • Values: anything, duplicates allowed
    • Classes: Sequence, SortedSequence
  • Map, (*9)

    • Keys: strings or objects, duplicate keys not allowed
    • Values: anything, duplicates allowed
    • Classes: Map, LRUMap
  • Set, (*10)

    • Keys: not meaningful
    • Values: objects, or scalars, each value is guaranteed to be unique (see Set usage below for details)
    • Classes: Set

General Characteristics:, (*11)

  • Collections are mutable (new elements may be added, existing elements may be modified or removed). Specialized immutable versions may be added in the future though.
  • Equality comparison between elements are always performed using the shallow comparison operator (===).
  • Sorting algorithms are unstable, that means the order for equal elements is undefined (the default, and only PHP behavior).

Usage

Collection classes provide a rich API., (*12)

Sequence


// Read Operations $seq = new Sequence([0, 2, 3, 2]); $seq->get(2); // int(3) $seq->all(); // [0, 2, 3, 2] $seq->head(); // Some(0) $seq->tail(); // Some(2) // Write Operations $seq = new Sequence([1, 5]); $seq->get(0); // int(1) $seq ->update(0, 4); ->get(0); // int(4) $seq ->remove(0) ->get(0); // int(5) $seq = new Sequence([1, 4]); $seq ->add(2) ->all(); // [1, 4, 2] $seq ->addAll([4, 5, 2]) ->all(); // [1, 4, 2, 4, 5, 2] // Sort $seq = new Sequence([0, 5, 4, 2]); $seq ->sortWith(fn($a, $b) => ($a - $b)); ->all(); // [0, 2, 4, 5]

Maps


// Read Operations $map = new Map(['foo' => 'bar', 'baz' => 'boo']); $map->get('foo'); // Some('bar') $map->get('foo')->get(); // string('bar') $map->keys(); // ['foo', 'baz'] $map->values(); // ['bar', 'boo'] $map->headOption(); // Some(['key', 'value']) $map->head(); // null | ['key', 'value'] $map->lastOption(); // Some(['key', 'value']) $map->last(); // null | ['key', 'value'] $map->headOption()->getOrElse([]); // ['foo', 'bar'] $map->lastOption()->getOrElse([]); // ['baz', 'boo'] $map->tail(); // ['baz' => 'boo'] iterator_to_array($map); // ['foo' => 'bar', 'baz' => 'boo'] $map->all() // ['foo' => 'bar', 'baz' => 'boo'] // Write Operations $map = new Map(); $map->set('foo', 'bar'); $map->setAll(['bar' => 'baz', 'baz' => 'boo']); $map->remove('foo'); // Sort $map->sortWith('strcmp'); // Transformation $map->map(fn($k,$v) => ($value * 2)); $map->flatMap(fn($k,$v) => (new Map())->set($k, $v * 2) ); $map->foldLeft(SomeObject $s, fn($s, $k, $v) => $s->add([$k, $v * 2]))

Set

In a Set each value is guaranteed to be unique. The Set class supports objects, and scalars as value. Equality is determined via the following steps., (*13)

Equality of Scalars, (*14)

Scalar are considered equal if ``$a === $b`` is true.
    class DateTimeHashable extends \DateTime implements HashableInterface
    {
        public function hash(): string
        {
            return $this->format("YmdP");
        }
    }

    $set = new Set();
    $set->add(new DateTimeHashable('today'));
    $set->add(new DateTimeHashable('today'));

    var_dump(count($set)); // int(1) -> the same date is not added twice

    foreach ($set as $date) {
        var_dump($date);
    }

    $set->all();
    $set->addSet($otherSet);
    $set->addAll($someElements);

    // Traverse
    $set->map(function($x) { return $x*2; });
    $set->flatMap(function($x) { return [$x*2, $x*4]; });

The Versions

20/04 2017

dev-next

dev-next

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

21/03 2017

dev-master

9999999-dev

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

21/03 2017

1.3.1

1.3.1.0

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

21/03 2017

1.3.0

1.3.0.0

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

21/03 2017

1.2.2

1.2.2.0

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

02/11 2016

1.2.1

1.2.1.0

General-Purpose Collection Library for PHP

  Sources   Download

GPL-3.0

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

02/11 2016

1.2.0

1.2.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

31/10 2016

1.1.0

1.1.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

31/10 2016

1.0.2

1.0.2.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

19/07 2016

1.0.1

1.0.1.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

29/02 2016

1.0.0

1.0.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Artyom Sukharev, Johannes M. Schmitt

collection list sequence map set

23/02 2016

0.5.7

0.5.7.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

18/02 2016

0.5.6

0.5.6.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

17/02 2016

0.5.5

0.5.5.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

03/02 2016

0.5.4

0.5.4.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

19/01 2016

0.5.2

0.5.2.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

02/09 2015

0.5.1

0.5.1.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

02/09 2015

0.5.0

0.5.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

The Development Requires

collection list sequence map set

11/03 2014

0.4.0

0.4.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

collection list sequence map set

13/12 2013

0.3.1

0.3.1.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

collection list sequence map set

16/07 2013

0.3.0

0.3.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

collection list sequence map set

23/01 2013

0.2.0

0.2.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

collection list sequence map set

19/12 2012

0.1.0

0.1.0.0

General-Purpose Collection Library for PHP

  Sources   Download

Apache2

The Requires

 

collection list sequence map set