darkdevlab/collection

PHP realization of collections

1.0.0 2020-05-03 16:09 UTC

This package is auto-updated.

Last update: 2024-06-11 00:27:17 UTC


README

This component provides emulation of collection type for work with php

INSTALLATION:

composer require darkdevlab/collection

USAGE

Collection

use DarkDevLab\Collection\Collection;

class User
{
    private $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
        return $this->name;
    }
}

/**
 * @property User[] $container
 * @method User current()
 */
class UserCollection extends Collection
{
    public function addItem(User $user): self
    {
        return parent::add($user);
    }
}

$collection = new UserCollection();

$collection->addItem(new User('John'));
$collection->addItem(new User('John')); // Only one added. Duplication will be skipped
$collection->addItem(new User('Mark'));

foreach ($collection as $user) {
    echo sprintf('Username: %s', $user->getName()), PHP_EOL;
}

Output:

Username: John
Username: Mark

GeneratorCollection

Lazy load collection

use DarkDevLab\Collection\GeneratorCollection;

class UserCollection extends GeneratorCollection
{
    
}

class UserRepository
{
    private $pdo;

    public function findAllByName(string $name): UserCollection
    {
        $stmt = $this->pdo->prepare('SELECT * FROM `user` WHERE name=:name');
        $generator = function () use ($stmt) {
            if ($stmt->execute()) {
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
                    if (!empty($row)) {
                        yield new User($row['name']);
                    }
                }
            } else {
                yield;
            }
        };

        return new UserCollection($generator());
    }
}

$userRepository =  new UserRepository(/* PDO */);
$userCollection = $userRepository->findAllByName('John');

foreach ($userCollection as $user) {
    echo sprintf('Username: %s', $user->getName()), PHP_EOL;
}

Output:

Username: John