binary-cats/shard-collections
Composer 安装命令:
composer require binary-cats/shard-collections
包简介
Enhance your Laravel collections with shard()
README 文档
README
Laravel Shard Collections
A Laravel package that provides powerful collection macros for sharding collections into multiple sub-collections based on custom conditions.
Installation
You can install the package via composer:
composer require binary-cats/shard-collections
Usage
This package provides two collection macros: shard() and shardWithKeys(). Both allow you to split a collection into multiple sub-collections based on custom conditions.
The shard() Method
The shard() method splits a collection into multiple sub-collections based on a series of conditions. Each condition is evaluated in order, and items that match a condition are moved to the corresponding sub-collection. Any remaining items are placed in the final sub-collection.
use Illuminate\Support\Collection; $collection = collect([1, 2, 3]); $map = [ fn ($item) => $item > 10, // First condition fn ($item) => $item < 2, // Second condition ]; $output = $collection->shard($map); // Result: // [ // collect([]), // Items > 10 (none) // collect([1]), // Items < 2 // collect([2, 3]) // Remaining items // ]
You can also preserve the original keys by passing true as the second parameter:
$output = $collection->shard($map, true); // Result: // [ // collect([]), // Items > 10 (none) // collect([0 => 1]), // Items < 2 (with original keys) // collect([1 => 2, 2 => 3]) // Remaining items (with original keys) // ]
The remainder is included when it is not empty. You can force remainder by passing the last argument
$output = $collection->shard( map: $map, forceRemainder: true ); // Result: // [ // collect(['one' => 1, 'two' => 2, 'three' => 3]), // Remaining items // collect([]) // Remainder is empty, but is included // ]
The shardWithKeys() Method
The shardWithKeys() method works similarly to shard() but allows you to specify custom keys for each sub-collection. This is particularly useful when you want to give meaningful names to your sharded collections.
$collection = collect([1, 2, 3]); $map = [ 'high' => fn ($item) => $item > 10, // First condition 'low' => fn ($item) => $item < 2, // Second condition ]; $output = $collection->shardWithKeys($map, 'medium'); // Result: // [ // 'high' => collect([]), // Items > 10 (none) // 'low' => collect([1]), // Items < 2 // 'medium' => collect([2, 3]) // Remaining items // ]
You can also preserve the original keys by passing true as the third parameter:
$output = $collection->shardWithKeys($map, 'medium', true); // Result: // [ // 'high' => collect([]), // Items > 10 (none) // 'low' => collect([0 => 1]), // Items < 2 (with original keys) // 'medium' => collect([1 => 2, 2 => 3]) // Remaining items (with original keys) // ]
Working with Associative Collections
Both methods work seamlessly with associative collections:
$collection = collect(['one' => 1, 'two' => 2, 'three' => 3]); $map = [ 'small' => fn ($item) => $item < 2, 'large' => fn ($item) => $item > 10, ]; $output = $collection->shardWithKeys($map, 'medium', true); // Result: // [ // 'small' => collect(['one' => 1]), // 'large' => collect([]), // 'medium' => collect(['two' => 2, 'three' => 3]) // ]
The remainder is included when it is not empty. You can force remainder by passing the last argument
collect(['one' => 1, 'two' => 2, 'three' => 3])->shard( map: [fn ($item) => $item < 10], forceRemainder: true ); // Result: // [ // collect(['one' => 1, 'two' => 2, 'three' => 3]) // Remaining items // collect([]) // Remainder is empty, but is included // ]
Empty Maps
When you provide an empty map, both methods will return a single collection containing all items:
$collection = collect([1, 2, 3]); $output = $collection->shard([]); // Result: [collect([1, 2, 3])] $output = $collection->shardWithKeys([], 'all'); // Result: ['all' => collect([1, 2, 3])]
Testing
composer test
Support us
Binary Cats is a webdesign agency based in Illinois, US.
License
The MIT License (MIT). Please see License File for more information.
binary-cats/shard-collections 适用场景与选型建议
binary-cats/shard-collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「collection」 「shard」 「binary cats」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 binary-cats/shard-collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 binary-cats/shard-collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 binary-cats/shard-collections 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Laravel Package based on Twitter Snowflake ID Generator (64 bit ID).
Traits to build collections of specific objects
Zend Framework 2 Module that provides exception handling functionality
Zend Framework 2 Module for Shard
ItalyStrap Helpers
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-18