PHP code example of worksolutions / php-collections
1. Go to this page and download the library: Download worksolutions/php-collections library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
worksolutions / php-collections example snippets
7.1+
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
// Getting filtered elements
CollectionFactory::from([1, 2, 3])
->stream()
->filter(Predicates::greaterThan(1))
->getCollection(); // Collection [2, 3]
// Print directory files
CollectionFactory::fromIterable(new DirectoryIterator(__DIR__))
->stream()
->each(static function (SplFileInfo $fileInfo) {
echo $fileInfo->getFilename() . "\n";
});
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
foreach ($map as $k => $v) {
var_dump($k); // one | two
var_dump($v); // 1 | 2
}
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->get('one'); // 1
$map->get('three'); // null
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
foreach ($map->keys() as $k) {
var_dump($k); // one | two
}
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
foreach ($map->keys() as $v) {
var_dump($v); // 1 | 2
}
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->remove('one');
foreach ($map->keys() as $v) {
var_dump($v); // 2
}
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->containsKey('one'); // true
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->containsValue(1); // true
$map->containsValue(3); // false
use WS\Utils\Collections\HashMap;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->size(); // 2
$emptyMap = new HashMap();
$map->size(); // 0
use \WS\Utils\Collections\HashMap;
use \WS\Utils\Collections\MapEntry;
$map = new HashMap();
$map->put('one', 1);
$map->put('two', 2);
$map->put('tree', 3);
$map->stream()->each(static function (MapEntry $mapEntry) {
var_export($mapEntry->getKey()); // 'one', 'two', 'three'
var_export($mapEntry->getKey()); // 1 , 2 , 3
});
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::numbers(10)
->stream()
->each(Consumers::dump()); // dumps int(0), int(1), ...
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::from([1 ,2, 3])
->stream()
->each(Consumers::dump()); // dumps int(1), int(2), int(3)
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
use WS\Utils\Collections\Functions\Converters;
CollectionFactory::fromIterable(new DirectoryIterator(__DIR__))
->stream()
->map(Converters::toPropertyValue('filename'))
->each(Consumers::dump()); // Dumps strings with filenames
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::numbers(10, 15)
->stream()
->each(Consumers::dump()); // Dumps [10, 11, 12, 13, 14, 15]
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::generate(3, static function () {
return random_int(0, 10);
})
->stream()
->each(Consumers::dump()); // Dumps for example [9, 7, 2]
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::numbers(10)
->stream()
->each(Consumers::dump()) // dumps each element
->each(static function ($el) { // prints strings 0, 1, 2, 3
echo $el."\n";
})
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::numbers(10)
->stream()
->walk(Consumers::dump(), 5) // dumps only first 5 elements: 0, 1, 2, 3, 4
->walk(static function ($el) { // prints strings 0, 1, 2, 3. Method will be called only 5 times
if ($el === 4) {
return false;
}
echo $el."\n";
})
;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
->filter(static function (int $el): bool {
return $el % 2 === 0;
})
->getCollection() // returns only first 5 elements: 0, 2, 4, 6, 8
;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
->map(static function (int $el): int {
return $el * 10;
})
->getCollection() // returns 0, 10, 20, 30, 40, 50, 60, 70, 80, 90
;
use WS\Utils\Collections\ArrayStack;
use WS\Utils\Collections\Collection;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
// reverse collection
->reorganize(static function (Collection $collection): Collection {
$stack = new ArrayStack();
foreach ($collection as $item) {
$stack->push($item);
}
$reversed = CollectionFactory::empty();
while (!$stack->isEmpty()) {
$reversed->add($stack->pop());
}
return $reversed;
})
->getCollection()
;
use WS\Utils\Collections\Collection;
use WS\Utils\Collections\CollectionFactory;
$sumOfElements = CollectionFactory::numbers(10)
->stream()
// get sum of collection elements
->collect(static function (Collection $collection): int {
$res = 0;
foreach ($collection as $item) {
$res += $item;
}
return $res;
})
;
use WS\Utils\Collections\CollectionFactory;
$sortedCollection = CollectionFactory::generate(10, static function (): int {
return random_int(0, 100);
})
->stream()
// get sorted collection
->sort(static function (int $a, int $b): int {
return $a <=> $b;
})
->getCollection()
;
use WS\Utils\Collections\CollectionFactory;
$sortedDescendentCollection = CollectionFactory::generate(10, static function (): int {
return random_int(0, 100);
})
->stream()
// get sorted collection in the reverse order
->sortDesc(static function (int $a, int $b): int {
return $a <=> $b;
})
->getCollection()
;
use WS\Utils\Collections\CollectionFactory;
class Container {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$sortedCollection = CollectionFactory::generate(10, static function (): Container {
return new Container(random_int(0, 100));
})
->stream()
// get sorted collection
->sortBy(static function (Container $container): int {
return $container->getValue();
})
->getCollection()
;
use WS\Utils\Collections\CollectionFactory;
$sumOfCollection = CollectionFactory::numbers(10)
->stream()
// get sum of collection elements
->reduce(static function (int $el, ?int $carry = null): int {
return $carry + $el;
})
;
use WS\Utils\Collections\Collection;
use WS\Utils\Collections\CollectionFactory;
$randomElementSizeCollection = CollectionFactory::numbers(random_int(0, 20));
$onlyTenElements = $randomElementSizeCollection
->stream()
// get collection elements only 10 items
->when($randomElementSizeCollection->size() > 10)
->limit(10)
->when($randomElementSizeCollection->size() < 10)
->reorganize(static function (Collection $collection) {
for ($i = $collection->size(); $i < 10; $i++ ) {
$collection->add($i);
}
return $collection;
})
;
use WS\Utils\Collections\CollectionFactory;
$collection = CollectionFactory::numbers(20);
$onlyTenElements = $collection
->stream()
// get collection elements only 10 items
->when($collection->size() > 5)
->limit(5)
->always()
->map(static function (int $el): int {
return $el * 10;
})
->getCollection() // [0, 10, 20, 30, 40]
;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
->allMatch(static function (int $el): bool {
return $el >= 1;
}) // false, 0 is less than 1
;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
->anyMatch(static function (int $el): bool {
return $el > 0;
}) // true, [1, 2, 3, 4, 5, 6, 7, 8, 9] are grate than 0
;
use WS\Utils\Collections\CollectionFactory;
CollectionFactory::numbers(10)
->stream()
->findAny() // for example - 5
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::lessOrEqual(2))
->getCollection()
->toArray() // [1, 2, null]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::greaterThan(2))
->getCollection()
->toArray() // [3, 4, 3]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::greaterOrEqual(2))
->getCollection()
->toArray() // [2, 3, 4, 3]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::not(3))
->getCollection()
->toArray() // [1, 2, 4, null]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::in([null, 3]))
->getCollection()
->toArray() // [3, null, 3]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
CollectionFactory::from([1, 2, 3, 4, null, 3])
->stream()
->filter(Predicates::notIn([null, 3]))
->getCollection()
->toArray() // [1, 2, 4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::where('value', 0))
->getCollection()
->isEmpty() // false
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereNot('value', 0))
->getCollection()
->toArray() // [#1, #2, #3, #4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereIn('value', [0, 4, 9]))
->getCollection()
->toArray() // [#0, #4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereIn('value', [0, 4, 9]))
->getCollection()
->toArray() // [#0, #4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereGreaterThan('value', 3))
->getCollection()
->toArray() // [#4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereLessThan('value', 3))
->getCollection()
->toArray() // [#0, #1, #2]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereGreaterOrEqual('value', 3))
->getCollection()
->toArray() // [#3, #4]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Predicates;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
$c = 0;
CollectionFactory::generate(5, static function () use (& $c) {
return new ValueObject($c++);
})
->stream()
->filter(Predicates::whereLessOrEqual('value', 3))
->getCollection()
->toArray() // [#1, #2, #3]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Comparators;
CollectionFactory::generate(5, static function (): int {
return random_int(0, 10);
})
->stream()
->sort(Comparators::scalarComparator())
->getCollection()
->toArray() // sorted value, for example [2, 3, 6, 7, 8]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Comparators;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
CollectionFactory::generate(5, static function () {
return new ValueObject(random_int(0, 10));
})
->stream()
->sort(Comparators::objectPropertyComparator('value'))
->getCollection()
->toArray() // sorted ValueObject objects, for example [#2, #3, #6, #7, #8]
;
use WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Comparators;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
CollectionFactory::generate(5, static function () {
return new ValueObject(random_int(0, 10));
})
->stream()
->sort(Comparators::callbackComparator(static function (ValueObject $valueObject) {
return $valueObject->getValue();
}))
->getCollection()
->toArray() // sorted ValueObject objects, for example [#2, #3, #6, #7, #8]
;
use WS\Utils\Collections\CollectionFactory;
use \WS\Utils\Collections\Functions\Converters;
class ValueObject {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function getValue() {
return $this->value;
}
}
CollectionFactory::generate(5, static function (int $index): ValueObject {
return new ValueObject($index);
})
->stream()
->map(Converters::toPropertyValue('value'))
->getCollection()
->toArray() // [0, 1, 2, 3, 4 ]
;
use WS\Utils\Collections\CollectionFactory;
use \WS\Utils\Collections\Functions\Converters;
class Person {
private $name;
private $surname;
public function __construct(string $name, string $surname)
{
$this->name = $name;
$this->surname = $surname;
}
public function getName(): string
{
return $this->name;
}
public function getSurname(): string
{
return $this->surname;
}
}
CollectionFactory::generate(1, static function (): Person {
return new Person('Ivan', 'Ivanov');
})
->stream()
->map(Converters::toProperties(['name', 'surname']))
->getCollection()
->toArray() // [['name' => 'Ivan', 'surname' => 'Ivanov']]
;
use \WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Reorganizers;
CollectionFactory::numbers(5)
->stream()
->reorganize(Reorganizers::shuffle())
->getCollection()
->toArray() // for example [0, 3, 1, 2, 4]
;
use \WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Reorganizers;
CollectionFactory::numbers(5)
->stream()
->reorganize(Reorganizers::random(2))
->getCollection()
->toArray() // for example [0, 3]
;
use \WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Reorganizers;
CollectionFactory::numbers(10)
->stream()
->reorganize(Reorganizers::chunk(2))
->getCollection()
->toArray() // for example [[0, 1], [2, 3], ...]
;
use \WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Reorganizers;
CollectionFactory::generate(3, static function (int $i): array {
return [$i*2, $i*2 + 1];
}) // [[0, 1], [2, 3], [4, 5]]
->stream()
->reorganize(Reorganizers::collapse())
->getCollection()
->toArray() // for example [0, 1, 2, 3, 4, 5]
;
use \WS\Utils\Collections\CollectionFactory;
use WS\Utils\Collections\Functions\Consumers;
CollectionFactory::numbers(5)
->stream()
->each(Consumers::dump()) // dumps each element of collection
;