sharkzt/collections

Generic-like collections for php, inspired by OOP languages structures

0.1.0 2017-10-15 14:38 UTC

This package is not auto-updated.

Last update: 2024-05-26 03:17:03 UTC


README

Collections for php, inspired by OOP generic languages structures

Build Status Coverage Status

Installation

The recommended way to install bundle is through Composer:

$ composer require sharkzt/collections

Usage Examples

ArrayList

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

ArrayList will accept only Article instances as arguments.

//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);

Can add iterable articles, or single invoking add method.

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

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

Possible usage examples.

License

Collections classes are released under the MIT License. See the bundled LICENSE file for details.