krlove/collections
Composer 安装命令:
composer require krlove/collections
包简介
Strictly typed data structures for PHP
README 文档
README
Strictly typed data structures for PHP.
Installation
composer require krlove/collections
Sequence
Sequence is an ordered collection of variables of any type.
$sequence = Sequence::of('string'); $sequence->push('Gandalf'); $sequence->push('Bilbo'); $sequence->push('Frodo'); $sequence->remove(1); foreach ($sequence as $index => $value) { echo $index . ': ' . $value . PHP_EOL; }
0: Gandalf
1: Frodo
Map
Map contains key-value pairs, where each key is unique.
$map = Map::of('string', 'array'); $map->set('fruits', ['apple', 'banana', 'pear']); $map->set('vegetables', ['tomato', 'potato', 'onion']); $map->set('berries', ['strawberry', 'blueberry', 'raspberry']); $map->remove('vegetables'); $map->set('berries', ['bilberry']); foreach ($map as $key => $value) { echo $key . ': ' . var_export($value, true) . PHP_EOL; }
fruits: array (
0 => 'apple',
1 => 'banana',
2 => 'pear',
)
berries: array (
0 => 'bilberry',
)
Set
Set is a collection of unique variables of any type.
$set = Set::of('string'); $set->add('Gandalf'); $set->add('Bilbo'); $set->add('Bilbo'); echo var_export($set->toArray(), true) . PHP_EOL;
array (
0 => 'Gandalf',
1 => 'Bilbo',
)
Types
All collections are strictly typed.
$sequence = Sequence::of('int'); $sequence->push('Gandalf');
PHP Fatal error: Uncaught Krlove\Collection\Exception\TypeException: Variable must be of type int, string given
Supported types are:
- null
- bool
- int
- float
- string
- array
- iterable
- callable
- resource
- object
- class (objects of specific class or interface)
- mixed (any type is allowed)
Nullable
Types can be nullable.
$sequence = Sequence::of('?string'); $sequence->push(null);
Freezable
After collection is "frozen", it becomes read-only, no changes are allowed to it. It is impossible to "unfreeze" the collection once it is frozen, but it is possible to copy it.
$sequence = Sequence::of('string'); $sequence->push('Gandalf'); $sequence->freeze(); //$sequence->push('Bilbo'); Fatal error: Uncaught Krlove\Collection\Exception\FrozenException: Sequence is frozen and can not be changed $copy = $sequence->copy(); $copy->push('Bilbo'); foreach ($copy as $index => $value) { echo $index . ': ' . $value . PHP_EOL; }
0: Gandalf
1: Bilbo
Usage
PHP does not support generic types, so it is impossible to define a property as follows.
private Map<int, string> $map; // invalid
Some additional code must be written to ensure collections of proper types are used
class MyClass { /** * @var Map<int, string> */ private Map $map; public function __construct() { $this->map = Map::of('int', 'string'); } /** * @param Map<int, string> $map * @return void */ public function setMap(Map $map): void { if (!$map->isOf('int', 'string')) { // throw exception } $this->map = $map; } }
krlove/collections 适用场景与选型建议
krlove/collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「collections」 「php」 「strict types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 krlove/collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krlove/collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 krlove/collections 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Base object, checking access to undefined properties and methods
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Doctrine Collections adapter for Rekapager pagination library
Strict Typing for local variables in PHP
Library to mimics generic collections
Package to improve Laravel framework with features to support more strict typed Laravel.
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-14