定制 pinkcrab/collection 二次开发

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

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

pinkcrab/collection

Composer 安装命令:

composer require pinkcrab/collection

包简介

A flexible and extendable Collection

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require GitHub contributors GitHub issues

Tests [PHP7.1-8.5]

codecov Scrutinizer Code Quality

Version 1.0.0

This library was extracted from the PinkCrab Plugin Framework (Perique)

Why?

Give access to a basic collection with all expected functionlaity, filtering, mapping, folding, sorting and comparing. But is also extendable for creating custom collections, which can be expanded and typed. A fairly simple, but extendable Collection.

Install

Install via Composer:

composer require pinkcrab/collection

Then include the Composer autoloader in your project:

require_once __DIR__ . '/vendor/autoload.php';

Basic Useage

See ./docs for more details and examples.

$collection = new Collection(['1',2,'3']);
$collection->push(4);
$collection->apply(fn($e) => (string) $e);
var_dump($collection); // ['1','2','3','4'];
$collection = new Collection([1,2,3,4,5,6,7,8,9,10]);
$collection->filter(fn($e) => $e % 2 == 0);
var_dump($collection); // [2,4,6,8,10];

Extendable Traits

The Collection package comes with a few Traits which can be used when creating custom collections. These can either be created on the fly using anonymous classes or through defining them as full classes.

$indexed_collection = new class() extends \PinkCrab\Collection\Collection {
    use \PinkCrab\Collection\Traits\Indexed;
};

$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );


// As a full class.
class Indexed_Collection extends \PinkCrab\Collection\Collection {
    use \PinkCrab\Collection\Traits\Indexed;
};

$indexed_collection = new Indexed_Collection();
$indexed_collection->set( 'key1', 'value1' );
$indexed_collection->has( 'key1' ); //true
var_dump( $indexed_collection );

Typed & Mapped Collections

<?php

class Post_Collection extends Collection {
    // Filter out anything not matching.
    protected function map_construct( array $data ): array {
        return array_filter(fn($e): bool => is_a($data, \WP_Post::class));
    }
}

$posts = Post_Collection::from([$post1, null, $post2, false, WP_Error]);
var_dump($posts->to_array()); // [$post1, $post2];

$collection->each(function($e){
    print $e->post_title . PHP_EOL;
}); 
// Post Title 1
// Post Title 2

License

MIT License http://www.opensource.org/licenses/mit-license.html

Change Log

  • 1.0.0 - Raised supported PHP range to 7.1–8.5 and modernised the tooling chain (PHPStan 2.x + phpstan stubs so traits are actually analysed, PHPUnit up to 9.6, WPCS-based phpcs). Interface implementations (ArrayAccess, Iterator, JsonSerializable) marked with #[\ReturnTypeWillChange] for PHP 8.1+ without dropping PHP 7.x support. Sequence::sum() now filters non-numeric values to stay compatible with PHP 8.3's stricter array_sum(). Corrected phpdoc types on Has_ArrayAccess::offsetSet (nullable offset) and Is_Iterable::key (nullable return). BC break: public method parameters renamed $function/$callable$callback on apply(), each(), filter(), map(), reduce(), sort(), sorted(), group_by() — positional callers unaffected, named-argument callers need to update.
  • 0.2.0 - Added in option callbacks for diff and intersect, complete with helper functions for checking based on object instance or values/type. Also includes the group_by() method.
  • 0.1.0 - Added Has_ArrayAccess and Is_Iterable traits to allow the implementation of the interfaces. Added docs from existing GitBook repo.
  • 0.0.0 - Extracted from the PinkCrab Plugin Framework as a standalone package.

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-07