定制 bzrk/php-stream 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bzrk/php-stream

最新稳定版本:2.00

Composer 安装命令:

composer require bzrk/php-stream

包简介

a library to simplify handling(filter, map, ...) collections in php

README 文档

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

统计信息

  • 总下载量: 9.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-23