abbasghasemi/collection
Composer 安装命令:
composer require abbasghasemi/collection
包简介
A collection of complete tools for working with PHP arrays.
README 文档
README
A PHP library for work with arrays
Powerful
- Support List, Map, Set, Stack, Queue, Object ...
- Functional (indexOf, map, where, reduce, first, firstOrNull firstWhere, ...)
- Type generic documentation
- Collections class for generate & filled List
Collections::filled(int $length, mixed $element) - The ability to enforce maps and lists to accept a specific type
Installation
The preferred of installation is via Composer. Run the following
command to install the package and add it as a requirement to your project's
composer.json:
composer require abbasghasemi/collection
Array example
$list = Collections::filled(10, 'filled'); // ArrayList echo count($list); // 10 echo Collections::toArraySet($list)->size(); // 1 $list->add(1); // This method does not exist! echo $list->first(); // filled $list = new MutableArrayList(/*[default]*/type: 'int'); $list->add(1); // added $list->add(5); // added echo count($list); // 2 echo $list->indexOf(1); // 0 echo $list->indexOf(6); // -1 $list = Collections::generate(10, function ($index){ return $index; }); // ArrayList $list = $list->where(function ($element){ return $element % 2 === 0; }); echo $list->last(); // 8 echo $list; // [0,2,4,6,8]
Map example
$map = new MutableInsensitiveMap(); $map['InSensitive'] = 'Insensitive'; $map['INSENSITIVE'] = 'Insensitive'; echo $map['insensitive']; // Insensitive echo $map->size(); // 1 $object = new MutableArrayList(); // or other object $map2 = new MutableObjectMap(keyType: $object::class, valueType: $map::class); $map2[$object] = $map; echo intval($map2[$object] === $map); // 1 $map2->values()->first()['INSENSITIVE'] = 10; echo $map['InSensitive']; // 10 $map2->keys()->first()->add('first'); $map2->keys()->first()->add('last'); echo $object->join(','); // first,last
Arrays class
| Class | ArrayList | MutableArrayList | ArraySet | MutableArraySet |
|---|---|---|---|---|
| Editable | ❌ | ✅ | ❌ | ✅ |
| Repeatable | ✅ | ✅ | ❌ | ❌ |
| Iterator | ✅ | ✅ | ✅ | ✅ |
| Countable | ✅ | ✅ | ✅ | ✅ |
Class methods
| Method | ArrayList | MutableArrayList | ArraySet | MutableArraySet |
|---|---|---|---|---|
| forward | ✅ | ✅ | ✅ | ✅ |
| back | ✅ | ✅ | ✅ | ✅ |
| size | ✅ | ✅ | ✅ | ✅ |
| isEmpty | ✅ | ✅ | ✅ | ✅ |
| isNotEmpty | ✅ | ✅ | ✅ | ✅ |
| contains | ✅ | ✅ | ✅ | ✅ |
| toArray | ✅ | ✅ | ✅ | ✅ |
| forEach | ✅ | ✅ | ✅ | ✅ |
| first | ✅ | ✅ | ✅ | ✅ |
| firstOrNull | ✅ | ✅ | ✅ | ✅ |
| last | ✅ | ✅ | ✅ | ✅ |
| lastOrNull | ✅ | ✅ | ✅ | ✅ |
| get | ✅ | ✅ | ✅ | ✅ |
| getRange | ✅ | ✅ | ✅ | ✅ |
| take | ✅ | ✅ | ✅ | ✅ |
| firstWhere | ✅ | ✅ | ✅ | ✅ |
| where | ✅ | ✅ | ✅ | ✅ |
| lastWhere | ✅ | ✅ | ✅ | ✅ |
| reduce | ✅ | ✅ | ✅ | ✅ |
| indexOf | ✅ | ✅ | ✅ | ✅ |
| lastIndexOf | ✅ | ✅ | ✅ | ✅ |
| indexWhere | ✅ | ✅ | ✅ | ✅ |
| lastIndexWhere | ✅ | ✅ | ✅ | ✅ |
| join | ✅ | ✅ | ✅ | ✅ |
| fillRange | ❌ | ✅ | ❌ | ❌ |
| update | ❌ | ✅ | ❌ | ✅ |
| reversed | ❌ | ✅ | ❌ | ✅ |
| shuffle | ❌ | ✅ | ❌ | ✅ |
| add | ❌ | ✅ | ❌ | ✅ |
| addAll | ❌ | ✅ | ❌ | ✅ |
| insert | ❌ | ✅ | ❌ | ✅ |
| insertAll | ❌ | ✅ | ❌ | ✅ |
| remove | ❌ | ✅ | ❌ | ✅ |
| removeFirst | ❌ | ✅ | ❌ | ✅ |
| removeLast | ❌ | ✅ | ❌ | ✅ |
| removeRange | ❌ | ✅ | ❌ | ✅ |
| removeWhere | ❌ | ✅ | ❌ | ✅ |
| sort | ❌ | ✅ | ❌ | ✅ |
| clear | ❌ | ✅ | ❌ | ✅ |
Maps class
| Class | StringMap | MutableStringMap | InsensitiveMap | MutableInsensitiveMap | ObjectMap | MutableObjectMap |
|---|---|---|---|---|---|---|
| Editable | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| Repeatable | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Iterator | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Countable | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Insensitive | ❌ | ❌ | ✅ | ✅ | - | - |
| Object key | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
Class methods
| Method | StringMap | MutableStringMap | InsensitiveMap | MutableInsensitiveMap | ObjectMap | MutableObjectMap |
|---|---|---|---|---|---|---|
| size | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| isEmpty | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| isNotEmpty | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| forEach | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| containsKey | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| containsValue | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| keys | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| values | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| mutableValues | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| entries | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| entryKey | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| get | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| get syntax [key] | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| toMap | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
| put | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| set syntax [key] = value | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| putIfAbsent | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| putAll | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| update | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| updateKey | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| replace | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| merge | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| remove | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
| removeWhere | ❌ | ✅ | ❌ | ✅ | ❌ | ✅ |
Collections class
| Static method | Description |
|---|---|
| filled | Collections::filled(int $length, mixed $element, ?string $type = null): ArrayList |
| generate | Collections::generate(int $length, callback $callback, ?string $type = null): ArrayList |
| of | Collections::of(mixed ...$elements): ArrayList |
| typeOf | Collections::typeOf(string $type, mixed ...$elements): ArrayList |
| mutableFilled | Collections::filled(int $length, mixed $element, ?string $type = null): MutableArrayList |
| mutableGenerate | Collections::generate(int $length, callback $callback, ?string $type = null): MutableArrayList |
| mutableOf | Collections::of(mixed ...$elements): MutableArrayList |
| mutableTypeOf | Collections::mutableTypeOf(string $type, mixed ...$elements): MutableArrayList |
| toArrayList | Collections::toArrayList(Collection $collection): ArrayList |
| toMutableArrayList | Collections::toMutableArrayList(Collection $collection): MutableArrayList |
| toArraySet | Collections::toArraySet(Collection $collection): ArraySet |
| toMutableArraySet | Collections::toMutableArraySet(Collection $collection): MutableArraySet |
| sortAscending | Collections::sortAscending(MutableCollection $collection): void |
| sortDescending | Collections::sortDescending(MutableCollection $collection): void |
| equals | Collections::equals(?ObjectC $a, ?ObjectC $b): bool |
| hashCode | Collections::hashCode(mixed $value): int |
| toString | Collections::toString(mixed $value): string |
See also easy-data-model
Creates a data model from array data.
Author & support
This library was created by Abbas Ghasemi.
You can report issues at the GitHub Issue Tracker.
abbasghasemi/collection 适用场景与选型建议
abbasghasemi/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「map」 「serializable」 「set」 「list」 「collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 abbasghasemi/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 abbasghasemi/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 abbasghasemi/collection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Maps in minutes. Powered by the Google Maps API.
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
PHP AMQP Binding Library
Laravel Query Builder (Eloquent) serialization
A Laravel package to monitor queue jobs.
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-17