imunhatep/collection 问题修复 & 功能扩展

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

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

imunhatep/collection

Composer 安装命令:

composer require imunhatep/collection

包简介

General-Purpose Collection Library for PHP

README 文档

README

SensioLabsInsight Build Status

PHP Collection library.

Based on J. M. Schmitt work, refactored and updated - now requires PHP 8.0. Updated with several useful methods like ::flatMap(), ::map(), ::foldLeft(), ::exists(), ::headOption(), ::lastOption(), ::tail() and e.t.c. Inspired by Scala collections. Added types.

Installation

PHP Collection can easily be installed via composer (in process)

composer require imunhatep/collection

or add it to your composer.json file.

Note

Collections can be seen as more specialized arrays for which certain contracts are guaranteed.

Supported Collections:

  • Sequence

    • Keys: numerical, consequentially increasing, no gaps
    • Values: anything, duplicates allowed
    • Classes: Sequence, SortedSequence
  • Map

    • Keys: strings or objects, duplicate keys not allowed
    • Values: anything, duplicates allowed
    • Classes: Map, LRUMap
  • Set

    • Keys: not meaningful
    • Values: objects, or scalars, each value is guaranteed to be unique (see Set usage below for details)
    • Classes: Set

General Characteristics:

  • Collections are mutable (new elements may be added, existing elements may be modified or removed). Specialized immutable versions may be added in the future though.
  • Equality comparison between elements are always performed using the shallow comparison operator (===).
  • Sorting algorithms are unstable, that means the order for equal elements is undefined (the default, and only PHP behavior).

Usage

Collection classes provide a rich API.

Sequence

    // Read Operations
    $seq = new Sequence([0, 2, 3, 2]);
    $seq->get(2); // int(3)
    $seq->all(); // [0, 2, 3, 2]

    $seq->head(); // Some(0)
    $seq->tail(); // Some(2)

    // Write Operations
    $seq = new Sequence([1, 5]);
    $seq->get(0); // int(1)
    
    $seq
      ->update(0, 4);
      ->get(0); // int(4)
    
    $seq
      ->remove(0)
      ->get(0); // int(5)

    $seq = new Sequence([1, 4]);
    $seq
      ->add(2)
      ->all(); // [1, 4, 2]
    
    $seq
      ->addAll([4, 5, 2])
      ->all(); // [1, 4, 2, 4, 5, 2]

    // Sort
    $seq = new Sequence([0, 5, 4, 2]);
    $seq
      ->sortWith(fn($a, $b) => ($a - $b));
      ->all(); // [0, 2, 4, 5]

Maps

    // Read Operations
    $map = new Map(['foo' => 'bar', 'baz' => 'boo']);
    $map->get('foo'); // Some('bar')
    $map->get('foo')->get(); // string('bar')
    $map->keys(); // ['foo', 'baz']
    $map->values(); // ['bar', 'boo']

    $map->headOption(); // Some(['key', 'value'])
    $map->head();       // null | ['key', 'value']
    $map->lastOption(); // Some(['key', 'value'])
    $map->last();       // null | ['key', 'value']
    
    $map->headOption()->getOrElse([]); // ['foo', 'bar']
    $map->lastOption()->getOrElse([]); // ['baz', 'boo']
    
    $map->tail();            // ['baz' => 'boo']

    iterator_to_array($map); // ['foo' => 'bar', 'baz' => 'boo']
    $map->all()              // ['foo' => 'bar', 'baz' => 'boo']

    // Write Operations
    $map = new Map();
    $map->set('foo', 'bar');
    $map->setAll(['bar' => 'baz', 'baz' => 'boo']);
    $map->remove('foo');

    // Sort
    $map->sortWith('strcmp');

    // Transformation
    $map->map(fn($k,$v) => ($value * 2));
    
    $map->flatMap(fn($k,$v) => (new Map())->set($k, $v * 2) );
    
    $map->foldLeft(SomeObject $s, fn($s, $k, $v) => $s->add([$k, $v * 2]))

Set

In a Set each value is guaranteed to be unique. The Set class supports objects, and scalars as value. Equality is determined via the following steps.

Equality of Scalars

Scalar are considered equal if ``$a === $b`` is true.
    class DateTimeHashable extends \DateTime implements HashableInterface
    {
        public function hash(): string
        {
            return $this->format("YmdP");
        }
    }
    
    $set = new Set();
    $set->add(new DateTimeHashable('today'));
    $set->add(new DateTimeHashable('today'));

    var_dump(count($set)); // int(1) -> the same date is not added twice

    foreach ($set as $date) {
        var_dump($date);
    }

    $set->all();
    $set->addSet($otherSet);
    $set->addAll($someElements);

    // Traverse
    $set->map(function($x) { return $x*2; });
    $set->flatMap(function($x) { return [$x*2, $x*4]; });

imunhatep/collection 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 69.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 51
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2016-02-29