innmind/immutable
Composer 安装命令:
composer require innmind/immutable
包简介
Immutable PHP primitive wrappers
关键字:
README 文档
README
A set of classes to wrap PHP primitives to build immutable data.
Installation
composer require innmind/immutable
Usage
Here are some examples of what you can do:
Sequence
To be used to wrap an ordered list of elements (elements can be of mixed types).
use Innmind\Immutable\Sequence; $seq = Sequence::of(24, 42, 'Hitchhiker', 'Magrathea'); $seq->get(2); // Maybe::just(Hitchhiker) $another = $seq->drop(2); $another->toList(); // [Hitchhiker, Magrathea] $seq->toList(); // [24, 42, Hitchhiker, Magrathea] //---- // this example demonstrates the lazyness capability of the sequence // precisely here it's able to read a file line by line and echo the lines // that are less than 42 characters long (without requiring to load the whole // file in memory) $someFile = fopen('some/file.txt', 'r'); $lines = Sequence::lazy(fn() => yield fgets($someFile)) ->filter(fn($line) => strlen($line) < 42); // at this point no reading to the file has been done because all methods // returning a new instance of a sequence will pipeline the operations to do, // allowing to chain complex logic while accessing the original data once and // without the need to keep the discarded data along the pipeline in memory $lines->foreach(fn($line) => echo($line));
For a complete list of methods check Sequence.
Set
To be used as a collection of unordered elements (elements must be of the same type).
use Innmind\Immutable\Set; $set = Set::of(24, 42); $set->equals(Set::of(24, 42)); // true $set->add(42.0); // psalm will raise an error
For a complete list of methods check Set.
Map
To be used as a collection of key/value pairs (both keys and values must be of the same type).
use Innmind\Immutable\Map; $map = Map::of( [new \stdClass, 42] [$key = new \stdClass, 24] ); $map->size(); // 2, because it's 2 different instances $map->values()->toList(); // [42, 24] $map = $map->put($key, 66); $map->size(); // 2 $map->values()->toList(); // [42, 66]
For a complete list of methods check Map.
Strings
use Innmind\Immutable\Str; $var = Str::of('the hitchhiker\'s guide to the galaxy'); echo $var ->replace('galaxy', '42') // the hitchhiker's guide to the 42 ->drop(18) // guide to the 42 ->toUpper() ->toString(); // outputs: GUIDE TO THE 42 echo $var->toString(); // outputs: the hitchhiker\'s guide to the galaxy
Regular expressions
use Innmind\Immutable\{ RegExp, Str, }; $regexp = RegExp::of('/(?<i>\d+)/'); $regexp->matches(Str::of('foo123bar')); // true $regexp->matches(Str::of('foobar')); // false $regexp->capture(Str::of('foo123bar')); // Map<int|string, Str> with index `i` set to Str::of('123')
BlackBox
This library provides 2 Sets that can be used with innmind/black-box.
You can use them as follow:
use Innmind\BlackBox\{ PHPUnit\BlackBox, Set, }; use Fixtures\Innmind\Immutable; class SomeTest extends \PHPUnit\Framework\TestCase { use BlackBox; public function testSomeProperty() { $this ->forAll( Immutable\Set::of( Set\RealNumbers::any(), ), Immutable\Sequence::of( Set\Uuid::any(), ), ) ->then(function($set, $sequence) { // $set is an instance of \Innmind\Immutable\Set<float> // $sequence is an instance of \Innmind\Immutable\Sequence<string> // write your test here }); } }
innmind/immutable 适用场景与选型建议
innmind/immutable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 227.25k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2016 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wrapper」 「immutable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 innmind/immutable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 innmind/immutable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 innmind/immutable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
Stk Immutable Data objects
MongoDB database access using immutables
gv config package
统计信息
- 总下载量: 227.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 23
- 依赖项目数: 66
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-14