https://github.com/krakphp/coll
PHP Collections Library
https://github.com/krakphp/coll
Last synced: about 1 year ago
JSON representation
PHP Collections Library
- Host: GitHub
- URL: https://github.com/krakphp/coll
- Owner: krakphp
- License: mit
- Created: 2016-04-25T22:35:56.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-04-25T22:43:15.000Z (about 10 years ago)
- Last Synced: 2025-03-04T09:06:26.752Z (over 1 year ago)
- Language: PHP
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Coll (Collections)
The Collections library is yet another attempt to generate OO collections in php.
Currently, the only collection in here is the Set because that's the only collection PHP actually is missing.
## Installation
```
compose require krak/coll
```
## Usage
### Set
A set represents a set of unique values.
There are two interfaces for the Set type: `ConstSet` and `Set`.
Const sets allow read access only whereas Sets allow read write access.
```
// const methods
public function get($value);
public function has($value);
public function count();
public function getIterator();
// non-const methods
public function add($value);
public function remove($value);
```
```php
add(4);
$s2 = set\union($s1, Set\ArraySet::create([3,4]));
set\is_subset($s2, $s1); // returns true because s1 is a subset of s2
// if you need to store object values
// internally uses SplObjectStorage
$oset = new Set\ObjectSet();
$oset->add(new stdClass());
// if you need to store values that work as array keys
$hset = new Set\HashSet();
$hset->add(['a' => 1]);
// if you need to store any of those types of values
$aset = new Set\AnySet();
set\fill($aset, [1, [1], new stdClass()]);
```
Each is set does also work as an iterable and is countable
```php
union($s2)
->intersect($s3)
->difference($s4)
->equals($s5);
```