remotelyliving/php-collection 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

remotelyliving/php-collection

Composer 安装命令:

composer require remotelyliving/php-collection

包简介

An immutable collection object for object oriented set operations

README 文档

README

Build Status Total Downloads Coverage Status License Scrutinizer Code Quality

php-collection

A lightweight, immutable collection object for set operations

Use Cases

So there is already a really nice collection library out there. I wanted to write my own for two reasons.

  1. I needed less than half of what it offered.
  2. It's fun! 2.1 Libraries that load themselves globally as level functions are evil

But also,

If you're tired of using arrays everywhere, this might be for you. If you're jealous of JavaScript's really gorgeous array and object API, this might be for you If the mutability and utter chaos of passing an array around scares you, this might be for you

Installation

composer require remotelyliving/php-collection

Usage

Collect::collect

Collections can be sets of numbers, strings, or objects. No arrays allowed. We don't want to get into multi dimensional madness.

use RemotelyLiving\PHPCollection\Collection;

$collectionOfStrings = Collection::collect(['foo', 'bar', 'baz']);
$collectionOfNumbers = Collection::collect([1, 2, 3]);
$collectionOfUsers = Collection::collect([$id1 => $user1, $id2 => $user2, $id4 => $user3]);

// we can iterate over collections
foreach ($collectionOfNumbers as $number) {
    echo (string) $number;
}
// outputs 1, 2, 3

// we can also perform set operations
$collectionOfUsers->contains($user1); // true
$collectionOfUsers->has($id1); // true
$collectionOfUsers->get($id1, new User()); // defaults to a new User if not found
$collectionOfUsers->some(fn(User $user) => $user->isVerified()); // true if some users are verified

$collectionOfUsers->filter(fn(User $user, $index) => $index > 1)
    ->map(fn(User $user) => $user->getName())
    ->first();

$collectionOfNumbers->reverse()
    ->all();

$collectionOfNumbers->each(fn(int $number, int $index) => $number * $index)
    ->unique()
    ->all();

Collection::later

We can create collections that don't don't fill up until we want something from them.

$generator = fn() => yield 1;
$deferredCollect = Collection::later($generator());

Collection::fill

We can create a prefilled collection

$filled = Collection::fill(0, 100, 'hi!');

Collection::fromString

We can create a collection from a delineated string

$fromString = Collection::fromString('1|2|3|4|5', '|');

Collection Set Methods

Now to the good stuff! All evaluation or operating methods that take a callable always pass in the value first, then the key. Below is the whole object API. It provides a lot hopefully without overstepping.

You can loop over it just like an array safely. It's \Traversable and iterable You can also safely serialize() and json_encode() it.

interface CollectionInterface extends \Traversable, \Countable, \IteratorAggregate, \Serializable, \JsonSerializable
{
    public function count(): int;

    public function map(callable $fn): self;

    public function filter(callable $fn): self;

    public function each(callable $fn): self;

    public function reverse(): self;

    /**
     * @return mixed|null
     */
    public function first();

    /**
     * @return mixed|null
     */
    public function last();

    /**
     * @param callable $fn
     * @param null     $initial
     *
     * @return mixed
     */
    public function reduce(callable $fn, $initial = null);

    public function unique(): self;

    public function diff(Collection $collection): self;

    public function merge(Collection $collection): self;

    public function union(Collection $collection): self;

    public function intersect(Collection $collection): self;

    public function sort(callable $comparator = null): self;

    public function kSort(callable $comparator = null): self;

    public function empty(): bool;

    public function all(): array;

    public function deferred(): \Generator;

    public function values(): array;

    public function equals(Collection $collection): bool;

    /**
     * @param string|int $offset
     *
     * @return bool
     */
    public function has($offset): bool;

    /**
     * @param mixed $value
     *
     * @return bool
     */
    public function contains($value): bool;

    public function some(callable $evaluation): bool;

    /**
     * @param string|int $offset
     * @param mixed|null $default
     *
     * @return mixed|null
     */
    public function get($offset, $default = null);

    /**
     * @return mixed
     */
    public function rand();

    /**
     * @param string|int ...$offset
     */
    public function remove(...$offset): self;
}

remotelyliving/php-collection 适用场景与选型建议

remotelyliving/php-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「array」 「map」 「collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 remotelyliving/php-collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 remotelyliving/php-collection 我们能提供哪些服务?
定制开发 / 二次开发

基于 remotelyliving/php-collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-30