wmsamolet/php-object-collections

PHP object collections - strongly typed collections for objects and more

1.8.0 2021-11-21 10:41 UTC

This package is auto-updated.

Last update: 2024-11-21 17:14:48 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require PHP Version Require

Strongly typed collections for objects and more

Advantages:

Documentation

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require wmsamolet/php-collections

or add

"wmsamolet/php-collections": "^1.0"

to the requirement section of your composer.json file.

Basic usage object collections

<?php

use Wmsamolet\PhpObjectCollections\ObjectCollection;

class TestEntity
{
    /** @var int */
    private $id;
    
    public function getId(): int
    {
        return $this->id;
    }
    
    public function setId(int $id): void 
    {
        $this->id = $id;
    }
}

// Create collection with values 1,2,3
$collection = new ObjectCollection(
    TestEntity::class, 
    [
        (new TestEntity())->setId(1), 
        (new TestEntity())->setId(2), 
        (new TestEntity())->setId(3), 
    ]
);

// Print entities: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';

// If we try to add another collection to the collection
// WE WILL GET AN EXCEPTION!

class TestOtherEntity extends TestEntity
{
}

$collection->add((new TestOtherEntity())->setId(4));

Basic usage object static collections

<?php

use Wmsamolet\PhpObjectCollections\AbstractCollection;

class TestEntity
{
    /** @var int */
    private $id;
    
    public function getId(): int
    {
        return $this->id;
    }
    
    public function setId(int $id): void 
    {
        $this->id = $id;
    }
}

/**
 * Add PhpDoc for IDE autocompletion when working with this collection
 * 
 * @method TestEntity[] getList()
 * @method null|TestEntity get(int $key)
 * @method null|TestEntity getByOffset(int $key)
 */
class ExampleEntityCollection extends AbstractObjectCollection
{
    /**
     * Set collection item as TestEntity object class
     */
    public function collectionObjectClassName(): string
    {
        return TestEntity::class;
    }
}

// Add entities to collection
$collection = new ExampleEntityCollection([
    (new TestEntity())->setId(1),
    (new TestEntity())->setId(2),
    (new TestEntity())->setId(3),
]);

// Print entities: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';

// If we try to add another collection or value with another to the collection
// WE WILL GET AN EXCEPTION!

class TestOtherEntity extends TestEntity
{
}

$collection->add((new TestOtherEntity())->setId(4));
$collection->add(5);

Basic usage typed collections

<?php

use Wmsamolet\PhpObjectCollections\TypedCollection;

// Create collection with values 1,2,3
$collection = new TypedCollection(TypedCollection::TYPE_INTEGER, [1, 2, 3]);

// Print values: 1,2,3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';

// If we try to add value with another type to the collection
// WE WILL GET AN EXCEPTION!
$collection->add('4');
$collection->add([5]);

Basic usage typed static collections

<?php

use Wmsamolet\PhpObjectCollections\AbstractTypedCollection;

/**
 * Add PhpDoc for IDE autocompletion when working with this collection
 * 
 * @method int[] getList()
 * @method null|int get(int $key)
 * @method null|int getByOffset(int $key)
 */
class TestIdCollection extends AbstractTypedCollection
{
    /**
     * Set collection item value type
     */
    public function collectionValueType(): string
    {
        return static::TYPE_INT;
    }
}

// Create collection with ids #1,#2,#3 to collection
$collection = new TestIdCollection([1, 2, 3]);

// Print ids: #1,#2,#3
echo '<pre>';
print_r($collection->getList());
echo '</pre>';

// If we try to add value with another type to the collection
// WE WILL GET AN EXCEPTION!
$collection->add('4');
$collection->add([5]);

License

PHP Object Collections is licensed under the MIT License - see the LICENSE file for details