Skip to content
This repository was archived by the owner on May 11, 2024. It is now read-only.

stscoundrel/collection

Folders and files

NameName
Last commit message
Last commit date
Oct 1, 2023
Feb 6, 2022
Mar 20, 2021
Mar 19, 2021
Mar 19, 2021
Mar 19, 2021
Jul 15, 2023
Dec 2, 2021
Jan 1, 2023
Mar 19, 2021
Mar 19, 2021
Mar 2, 2022

Repository files navigation

Collection

Minmalistic typed arrays/collections for PHP.

There are many collection libraries for PHP, but none exactly like I wanted. Most collection libraries add quite a lot of extra functionality I'm not looking for.

What collection does:

  • Simple & lightweight
  • Array syntax
  • Allow typehinting for array of objects

Motivation

Typehinting "array" in PHP is not really descriptive. Sometimes its preferable to use collection-like typed arrays. This library provides common parent to make creating these collections less boilerplatey.

Install

composer require silvanus/collection

Usage

Create your own collection class that extends abstract parent.

<?php

// Parent class.
use Silvanus\Collection\AbstractCollection;

class DummyCollection extends AbstractCollection
{
    /**
     * Provide your class with getType function.
     * Return fully qualified namespace of your tpye. 
     */
    protected function getType() : string {
        return 'Acme\App\DummyEntity';
    }
}

And thats it. Your collection works like an array, but only accepts your defined type.

<?php

// Your collection
use Acme\App\DummyCollection;

// Your object.
use Acme\App\DummyEntity;

$collection = new DummyCollection();

// All good.
$collection[] = new DummyEntity(1);
$collection[] = new DummyEntity(2);

// Exception thrown
$collection[] = new DateTime();

You can also turn an array into a collection.

<?php

// Your collection
use Acme\App\DummyCollection;

// Your object.
use Acme\App\DummyEntity;

$array = array( new DummyEntity(1), new DummyEntity(2), new DummyEntity(3) );

// Will throw exceptions if incorrect types in array.
$collection = new DummyCollection( $array );