star/collection
Composer 安装命令:
composer require star/collection
包简介
Collection utility lib
README 文档
README
Library that offer multiple implementations of collection.
Installation
To install the package using [composer] (https://getcomposer.org/), you just need to add the following lines to your composer.json file.
...
"require": {
"star/collection": "~1.2"
}
...
Supported collection
Enumeration
Wrap an immutable array of values in an object. Ensuring that any value passed to the enumeration is supported by the instance.
$enumeration = new Enumeration(array(1,2,3));
$enumeration->getSelected(); // returns null
$enumeration->select(2);
$enumeration->getSelected(); // returns 2
$enumeration->select('invalid'); // Throw Exception
Typed Collection
Wraps a collection of a specific kind of object (class or interface). If a value not supported by the collection is given, the collection throws exceptions.
Basic usage
$collection = new TypedCollection('\stdClass');
$collection->add(2); // Throw exception
$collection->add(new \stdClass()); // works
$collection = new TypedCollection('\Countable');
$collection->add(2); // Throw exception
$collection->add(new ClassThatImplementsCountable()); // works
Advanced usage
Using composition
Lets say that you want a Car collection, you could just define it using the basic usage, but it would lead to code
duplication. So a good practice would be to define a new class named CarCollection, and use composition instead of
inheritance, and declare it like this:
class CarCollection
{
private $collection;
public function __construct()
{
$this->collection = new TypedCollection('tests\Star\Component\Collection\Example\Car');
}
}
Declaring your collection like this will enable you to encapsulate logic relevant to the car collection at one place, instead of risking to expose the inner implementation to the outside world. That way, you can control what methods are available and avoid duplication.
Using this example, adding a Car to the collection would be easy by implementing the addCar method.
class CarCollection
{
...
public function addCar(Car $car)
{
$this->collection->add($car);
}
...
}
And, if you want to filter all the cars based on their color, you can internally use it like this:
class CarCollection
{
...
public function findAllCarWithColor(Color $color)
{
$closure = function(Car $car) use ($color) {
return $car->getColor() == $color;
};
return $this->collection->filter($closure)->toArray();
}
...
}
The same could also be done for finding cars based on their name:
class CarCollection
{
...
public function findAllWithName($name)
{
$expression = Criteria::expr()->eq('name', $name);
$criteria = new Criteria($expression);
return $this->collection->matching($criteria)->toArray();
}
...
}
From now on, your collection is re-usable, and testable at one place, while avoiding the pitfalls of inheritance.
Using Inheritance
class PassengerCollection extends TypedCollection
{
...
public function findAllWithName($name)
{
$expression = Criteria::expr()->eq('name', $name);
$criteria = new Criteria($expression);
return $this->matching($criteria)->toArray();
}
...
}
Using Doctrine
This class can be used in conjunction with doctrine/dbal as an added functionality, but you need to make sure that the following steps are respected.
-
The custom collection should inherit from the
TypedCollectionwhich implement theDoctrine\Common\Collections\Collection. -
The collection attribute will be replaced by a
Doctrine\ORM\PersistentCollectionwhen hydrated, instead of the CustomCollection, event thought the collection is instanciated in the construct. To bypass this feature, any method that sould return a CustomCollection should be implemented like this:class Entity { // CustomCollection that can be hydrated with a PersistentCollection by Doctrine private $myElements;
public function __construct() { $this->myElements = new CustomCollection('stdClass'); } public function getElements() { // At this point $this->myElements could be a Persistent collection return new CustomCollection($this->myElements->toArray()); }}
star/collection 适用场景与选型建议
star/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 565.56k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2014 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 star/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 star/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 565.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-26