dcsg/php-immutable-collections
Composer 安装命令:
composer require dcsg/php-immutable-collections
包简介
Minimalist and Typed Immutable Collections
README 文档
README
A library that provides a set of minimalist, typed and piped Immutable Collections for PHP.
Requirements
PHP > 7.2
Installation
composer require dcsg/php-immutable-collections
What Problems does this solve?
The lack of Generics in PHP does not allow to have typed collections in PHP.
This Library provides two abstract Immutable Collections:
- Immutable List Collection - ImmutableCollection
- Immutable Set Collection - SetImmutableCollection
Why Typed Immutable Collections
Typed Collections
Since PHP does not have Generics like Java has, it's not possible to have native typed collections. The Collections available in this Library are the foundation for you to create your own Typed Collections, you just need to extend them.
Typed Immutable Collections for DDD (Domain Driven Design)
DDD is all about having your code speaking the business language, the called Ubiquitous Language. Without Collections in PHP this is very hard to achieve using Arrays because you cannot add behavior to them. So what usually happens you add that behavior to your entity but it shouldn't be there. Another problem is the mutability of Arrays, VOs (Value Objects) MUST be always immutable and that's impossible with Arrays and you should always guarantee that the elements of that Array are all of the same type.
Collections vs Arrays
CollectionsandArraysboth represent a Group of Elements.Collectionsare not natively supported by PHP whileArraysare.Arraysis THE data structure of PHP and is used for almost everything.Arraysdon't allow you to add new behavior whileCollectionsallow it.Arraysdon't allow you to define a type for its elements whileCollectionsallow it.
Other PHP Collections
There are other Collections for PHP out there, naming a few we have Doctrine Collections and Illuminate Collections. Those collections they solve different problems, are tailored to their specific use case, and their APIs are extensive and more important those Collections are Mutable. These combinations make it difficult to use those Collections for more simple use cases. That's why the Collections we provide here, have a very small API and don't even expose the Iterator API. This way you have the possibility to use them and extend it's behavior tailored for your needs.
Features
- Static construction for pipe usage.
- Util methods like
isEmpty,count,toArray,contains,get,map,filter,slice,merge,reverse,reduce,first,last,head,tail.
Basic usage
Creating a Typed List Collection
<?php declare(strict_types=1); use DCSG\ImmutableCollections\ImmutableCollection; final class MyStringCollection extends ImmutableCollection { protected function validateItems(array $elements): void { foreach ($elements as $element) { if (!\is_string($element)) { throw new InvalidArgumentException('Element is not a String.'); } } } } $collection = MyStringCollection::create(['foo', 'bar']); echo $collection->count(); // 2 $slicedCollection = $collection->slice(0, 1); // MyStringCollection { $elements=['foo']}
Creating a Typed Set Collection
<?php declare(strict_types=1); use DCSG\ImmutableCollections\SetImmutableCollection; final class MyStringSetCollection extends SetImmutableCollection { protected function validateItems(array $elements): void { foreach ($elements as $element) { if (!\is_string($element)) { throw new InvalidArgumentException('Element is not a String.'); } } } } $collection = MyStringSetCollection::create(['foo', 'bar']); echo $collection->count(); // 2 $slicedCollection = $collection->tail(); // MyStringSetCollection { $elements=['bar']} $collection = MyStringSetCollection::create(['foo', 'bar', 'foo']); // Throws InvalidArgumentException
Examples
We provide two simple examples for better understanding. One related to invoices and another one regarding Legs of a Cargo Ship.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE OF CONDUCT for details.
Security
If you discover any security related issues, please email hi@dcsg.me instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
dcsg/php-immutable-collections 适用场景与选型建议
dcsg/php-immutable-collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.16k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2019 年 01 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pipe」 「collection」 「immutable」 「immutable-collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dcsg/php-immutable-collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcsg/php-immutable-collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dcsg/php-immutable-collections 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
Functional programming utilities for composing, decorating, and controlling function execution
CLI tooling with zero configuration required.
php proc_open wrapper class for shell command process management.
PIPE - ORM en Español.
This package provides type-safe extension of the laravel collection, forcing a single type of object.
统计信息
- 总下载量: 31.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-25