Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/palmtreephp/collection
:palm_tree: Immutable collections which provide commonly used functionality missing from arrays
https://github.com/palmtreephp/collection
collection php
Last synced: 2 days ago
JSON representation
:palm_tree: Immutable collections which provide commonly used functionality missing from arrays
- Host: GitHub
- URL: https://github.com/palmtreephp/collection
- Owner: palmtreephp
- License: mit
- Created: 2017-06-24T14:09:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-11-17T15:58:25.000Z (12 months ago)
- Last Synced: 2024-11-02T02:15:51.581Z (13 days ago)
- Topics: collection, php
- Language: PHP
- Homepage:
- Size: 279 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :palm_tree: Palmtree Collection
[![License](http://img.shields.io/packagist/l/palmtree/collection.svg)](LICENSE)
[![Build](https://img.shields.io/github/actions/workflow/status/palmtreephp/collection/build.yaml?branch=master)](https://github.com/palmtreephp/collection/actions/workflows/build.yaml)
[![Packagist Version](https://img.shields.io/packagist/v/palmtree/collection)](https://packagist.org/packages/palmtree/collection)Immutable collections which provide commonly used functionality missing from arrays.
## Requirements
* PHP >= 8.1
## Installation
Use composer to add the package to your dependencies:
```bash
composer require palmtree/collection
```## Usage
Collections can be used just like arrays for the most part. They implement [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess), [`Traversable`](https://www.php.net/manual/en/class.traversable) and [`Countable`](https://www.php.net/manual/en/class.countable):
```php
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');
``````php
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](src/Collection.php) to see more.
### Generics
The library supports template annotations for use by static analysers such as Psalm and PHPStan:
```php
/** @var Collection **/
$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](LICENSE)