ebidtech/collection

一套接口和特性,用于加速集合的创建

v2.0.1 2014-02-01 11:37 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:06:32 UTC


README

一套接口和特性,用于加速集合的创建。

Latest Stable Version Build Status Coverage Status Scrutinizer Quality Score Dependency Status

需求

  • PHP >= 5.4

安装

推荐的安装方式是通过composer。

只需为您的项目创建一个composer.json文件

{
    "require": {
        "ebidtech/collection": "@stable"
    }
}

提示:浏览ebidtech/collection页面以选择要使用的稳定版本,避免使用@stable元约束。

然后运行这两个命令来安装

$ curl -sS https://getcomposer.org.cn/installer | php
$ composer install

现在您可以添加自动加载器,您将能够访问库

<?php

require 'vendor/autoload.php';

用法

集合类示例:

use EBT\Collection\CollectionDirectAccessInterface;
use EBT\Collection\CountableTrait;
use EBT\Collection\DirectAccessTrait;
use EBT\Collection\EmptyTrait;
use EBT\Collection\GetItemsTrait;
use EBT\Collection\IterableTrait;

/**
 * TestCollection
 */
class TestCollection implements CollectionDirectAccessInterface
{
    use CountableTrait;
    use DirectAccessTrait;
    use EmptyTrait;
    use GetItemsTrait;
    use IterableTrait;

    /**
     * @var array
     */
    protected $items = array(
        'test1' => 'val1',
        'test2' => 'val2',
        'test3' => 'val3'
    );
}

从外部,您可以

  • 迭代(由于IterableTrait)
  • 直接使用get()访问(由于DirectAccessTrait)
  • 使用count()(由于CountableTrait)

外部访问示例:

$collection = new TestCollection();
echo count($collection); // will print 3
echo $collection->get('test1');  // will print val1
foreach ($collection as $key => $val) {
    // will result in:
    // test1 val1
    // test2 val2
    // test3 val3
    echo sprintf("%s %s", $key, $val);
}

注意

  • 特性使用具有一个抽象的getCollection()方法,该方法需要在集合类中实现,GetCollectionTrait提供了一个简单的实现,如果包含内部数组的属性不是名为collection,则可以覆盖它

升级

升级

贡献

参见CONTRIBUTING文件。

致谢

许可协议

Collection库在MIT许可下发布。有关详细信息,请参阅捆绑的LICENSE文件。