shrikeh/collections
Composer 安装命令:
composer require shrikeh/collections
包简介
Traits for re-use in immutable collections
关键字:
README 文档
README
Trait-based helper library to take care of the heavy lifting of immutable collections in PHP.
Overview
This low-level library is just time-saving traits for creating immutable collections. As PHP 7 can use return type hinting, I decided I would always return a Collection if there was a possibility of returning 0-n objects from a method, and could then type hint the return value as a Collection, whether empty or not. PHP does not natively support immutable iterators, so I find that whenever I use Domain-Driven Design, and need an iterable list of Value Objects, I have to do the same boilerplate re-work.
This pattern has been successful for me, as I can also strongly type the Collections themselves, so they can only contain objects of a given type. I also generally make them immutable, so they throw a descendent of DomainException if you try to set or unset a value.
So as I use these more often, I split them up into Traits for re-use across my code. Feel free to use for yourself, they're tiny and just take care of boilerplate stuff for me.
Installation
Recommended installation is via the ubiquitous and seminal composer:
composer require --prefer-dist shrikeh/collections
Usage
The library consists of about a dozen traits that aid in matching core PHP and SPL interfaces such as ArrayAccess and OuterIterator. Generally I have an inner "storage", and ensure that access to this directly is removed, including mutable methods such as offSetSet() or offsetUnset(). This ensures that only the values added to the constructor can be iterated over.
As an example, to create a Collection that can only contain SomeObject objects:
<?php namespace Shrikeh\Collection\Examples; use IteratorIterator; use Shrikeh\Collection\Examples\SomeObject; /** * An immutable iterator that can only contain SomeObject objects. */ final class ImmutableSomeObjectCollection extends IteratorIterator { use \Shrikeh\Collection\NamedConstructorsTrait; # Give it named constructors use \Shrikeh\Collection\ImmutableCollectionTrait; # Give it read-only array access use \Shrikeh\Collection\ClosedOuterIteratorTrait; # Close off access to the inner iterator use \Shrikeh\Collection\OuterIteratorTrait; # Give it all the standard read access methods use \Shrikeh\Collection\ObjectStorageTrait; # Set inner storage to SplObjectStorage # Append method is called by ObjectStorageTrait during construction, so we # type hint the relevant class/interface we need... protected function append(SomeObject $object, $key) { $this->getStorage()->attach($object); } }
Please take a look in the examples and the specs for further usage.
Traits
ArrayAccessTrait
Used to easily meet the requirements of the ArrayAccess interface. Proxies to underlying offsetX methods of the storage.
ClosedOuterIteratorTrait
The OuterIterator interface specifies that a class must implement the method getInnerIterator, and the visibility of this method cannot be changed from public. This defeats the point of having an immutable Collection. Therefore, this trait, when applied, causes the class to throw a ClosedOuterIterator exception for this method.
FixedArrayStorageTrait
This provides a public __construct() method consistent with the IteratorIterator family of SPL classes, providing the class with a SplFixedArray storage (SplFixedArray, sadly, can have it's size changed after instanstiation).
ImmutableArrayAccessTrait
This trait "switches off" the offsetSet() and offsetUnset() methods required by the ArrayAccess interface. Attempting to use either will result in a ImmutableCollection exception being thrown.
ImmutableCollectionTrait
This trait is a shorthand for including the ArrayAccessTrait and then overriding the setters with ImmutableArrayAccessTrait.
NamedConstructorsTrait
Provides the named constructors fromTraversable() and fromArray() to a Collection.
ObjectStorageTrait
This provides a public __construct() method consistent with the IteratorIterator family of SPL classes, providing the class with a SplObjectStorage inner storage.
OuterIteratorTrait
If you don't want to extend any of the IteratorIterator family, but do want to implement OuterIterator, this trait provides the necessary methods that proxy to the inner storage.
RequiresOuterIteratorTrait
"Safety catch" trait used by FixedArrayStorageTrait and ObjectStorageTrait to ensure the class using the trait implements OuterIterator. Throws an IncorrectInterface Exception if not.
shrikeh/collections 适用场景与选型建议
shrikeh/collections 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.51k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shrikeh/collections 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shrikeh/collections 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shrikeh/collections 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
CakePHP Bitly Plugin
some useful library of the php
统计信息
- 总下载量: 1.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-11