2017 © Pedro Peláez
 

library collection

Collection component for Palmtree PHP

image

palmtree/collection

Collection component for Palmtree PHP

  • Wednesday, July 4, 2018
  • by palmtree
  • Repository
  • 1 Watchers
  • 0 Stars
  • 60 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 40 % Grown

The README.md

:palm_tree: Palmtree Collection

License Build Packagist Version, (*1)

Immutable collections which provide commonly used functionality missing from arrays., (*2)

Requirements

  • PHP >= 8.1

Installation

Use composer to add the package to your dependencies:, (*3)

composer require palmtree/collection

Usage

Collections can be used just like arrays for the most part. They implement ArrayAccess, Traversable and Countable:, (*4)

use function Palmtree\Collection\c;

$collection = c(['foo' => 'bar', 'baz' => 'qux']);

$foo = $collection['foo'];
$collection['baz2'] = 'qux';

isset($collection['baz']); // true
unset($collection['baz']);
count($collection);

foreach ($collection as $key => $value) {
    // do stuff with value and/or key
}

// find returns the first matching element
$foo = $collection->find(fn ($v) => $v === 'bar');

// filter returns a new filtered collection
$quxCollection = $collection->filter(fn ($v) => $v === 'qux');
use function Palmtree\Collection\c;

$collection = c([1, 9, 5, 3, 7, 10])
    ->sort()
    ->filter(fn ($i) $i < 10)
    ->values()

// returns true as every element remaining in the collection is odd
$isAllOdd = $collection->every(fn ($i) => $i % 2 !== 0);
// returns false as it was removed in our filter
$collection->contains(10);

// returns true as at least one of the elements is false
c([true, true, false, true, true])->some(fn ($v) => !$v);

Many other methods are provided. Read through the documented source code to see more., (*5)

Generics

The library supports template annotations for use by static analysers such as Psalm and PHPStan:, (*6)

/** @var Collection<int, Foo> **/
$collection = new Collection();
$collection->add(new Foo());

foreach ($collection as $foo) {
    // Psalm/PHPStan and PhpStorm know that $foo is an instance of Foo here
}

License

Released under the MIT license, (*7)

The Versions

04/07 2018

dev-master

9999999-dev

Collection component for Palmtree PHP

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

04/07 2018

v0.9.1

0.9.1.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

04/07 2018

v0.9.0

0.9.0.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

20/04 2018

v0.8.2

0.8.2.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Development Requires

01/11 2017

v0.8.1

0.8.1.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Development Requires

31/10 2017

v0.8.0

0.8.0.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Development Requires

31/10 2017

v0.7.2

0.7.2.0

Collection component for Palmtree PHP

  Sources   Download

MIT

The Development Requires

30/10 2017

v0.6.0

0.6.0.0

Curl component for Palmtree PHP

  Sources   Download

MIT

The Requires

 

20/08 2017

v0.5.0

0.5.0.0

Curl component for Palmtree PHP

  Sources   Download

MIT

11/07 2017

v0.3.0

0.3.0.0

Curl component for Palmtree PHP

  Sources   Download

MIT

24/06 2017

v0.2.2

0.2.2.0

Curl component for Palmtree PHP

  Sources   Download

MIT

24/06 2017

v0.2.1

0.2.1.0

Curl component for Palmtree PHP

  Sources   Download

MIT

The Requires

 

24/06 2017

v0.2.0

0.2.0.0

Curl component for Palmtree PHP

  Sources   Download

MIT

The Requires