bzrk / php-stream
a library to simplify handling(filter, map, ...) collections in php
Installs: 4 637
Dependents: 2
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >= 8.2
Requires (Dev)
- mikey179/vfsstream: >=1.6
- phpstan/phpstan: >=1.10
- phpunit/phpunit: >=11.0
- squizlabs/php_codesniffer: 3.9
README
A library to handle Collections like in Java
Stream Class
- public count(): int
- public map(Closure $call): Stream
- public flatMap(Closure $call): Stream
- public filter(Closure $call): Stream
- public notNull(): Stream
- public notEmpty(): Stream
- public each(Closure $call): void
- public toList(bool $keepKeys = false): array
- public toMap(Closure $key, Closure $value): array
- public first(): mixed
- public limit(int $size): Stream
- public order(Comparator $comparator): Stream
- public run(): Stream
- public skip(int $count): Stream
- public implode(string $separator = ','): string
- public join(string $separator = ','): string --> alias for implode
- public batch(int $count): Stream
- public associateBy(Closure $call): array
- public collect(string $class): Collection
- public toGenerator(Closure $call = null, $default = null): Generator
- public callBack(Closure $call): Stream
Streams Class
- public static of(array|Iterator|File|CsvFile $data): Stream
- public static range(int $start, int $inclusiveEnd): Stream
- public static split(string $pattern, string $source): Stream
Examples
$data = [ (object)["name" => "hallo", "data" => ["a", "b"]], (object)["name" => "hallo", "data" => ["a", "b", "H", "g"]], (object)["name" => "hallo", "data" => ["a", "b", "d"]], ]; $result = Streams::of($data) ->map(fn(object $obj) => (object)["name" => $obj->name, "cnt" => count($obj->data)]) ->filter(fn(object $obj) => $obj->cnt !== 4) ->order(new Comparator(fn(object $a, object $b) => $a->cnt - $b->cnt)) ->map(fn(object $obj) => "$obj->name - $obj->cnt") ->toList(); //Result is ['hallo - 2', 'hallo - 3']
Create a Stream from a Typed Collection
class User { ... } class UserCollection extends Collection { public function __construct(User ...$data) { parent::__construct($data); } public function add(User $value): void { parent::addEntry($value); } } (new UserCollection(new User("name")))->stream()->map(....)
more Examples in the testsuite.
Running Test
composer verify
or
docker-compose up