PHP code example of jakubgiminski / collection

1. Go to this page and download the library: Download jakubgiminski/collection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jakubgiminski / collection example snippets


class UserCollection extends Collection
{
    public function __construct(array $users = [])
    {
        parent::__construct(
            $users,
            // Set up typing - every element must be an instance of User
            Type::object(User::class), 
            // Set up a unique index - user id
            new UniqueIndex(function (User $user) {
                return $user->getId();
            })
        );
    }
}

class AddressCollection extends Collection
{
    public function __construct(array $addresses = [])
    {
        parent::__construct(
            $addresses,
            // Set up typing - every element must be an array
            Type::array(),
            // Set up a unique index - all the fields
            new UniqueIndex(function (array $address) {
                return $address['postCode'] . $address['street'] . $address['homeNumber'];
            })
        );
    }
}

Collection::add($element): self;
Collection::addMany(self $collection): void;
Collection::filter(callable $filter): self;
Collection::remove($element): self;
Collection::get($uniqueIndex): self;
Collection::count(): int;
Collection::getElements(): array;
Collection::isTyped(): bool;
Collection::hasUniqueIndex(): bool;
Collection::rewind();
Collection::current();
Collection::key();
Collection::next();
Collection::valid();
Collection::isEmpty(): bool;