sharkzt/collections

受面向对象编程语言结构启发的php通用集合

0.1.0 2017-10-15 14:38 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:37:35 UTC


README

集合,受面向对象编程语言结构启发,适用于php

Build Status Coverage Status

安装

推荐通过 Composer 安装此软件包

$ composer require sharkzt/collections

使用示例

ArrayList

//rough example of ArrayListcreation, usually goes in constructor; see examples classes
$user = new User();
$user->articles = new ArrayList(Article::class);  

ArrayList 仅接受 Article 实例作为参数。

//addAll argument can be any iterable form, like ArrayList
$user->articles->addAll([
    (new Article())
        ->setTitle('foo')
        ->setContent('bar'), 
    (new Article())
        ->setTitle('baz')
        ->setContent('qux'),
]);

$singleArticle = (new Article())
    ->setTitle('foo')
    ->setContent('bar');
$user->articles->add($singleArticle);

可以添加可迭代的文章,或调用单个的 add 方法。

if ($user->articles->contains($singleArticle)) {
    $user->articles->remove($singleArticle);    
}

foreach ($user->articles as $article) {
    if ($article->getContent() === 'qux') {
        $article->setTitle('foo');
        break;
    }
}

可能的用法示例。

许可证

Collections 类发布在 MIT 许可证下。有关详细信息,请参阅附带 LICENSE 文件。