wmsamolet / php-object-collections
PHP object collections - strongly typed collections for objects and more
1.8.0
2021-11-21 10:41 UTC
Requires
- php: >=7.2.0
Requires (Dev)
- phpunit/phpunit: ^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0
- squizlabs/php_codesniffer: ^2.0 || ^3.0
README
Strongly typed collections for objects and more
Advantages:
- Storage in collections of strictly specified objects
- Ability to specify ANY Traversable object as data (useful for storing ORM Iterators, for example https://www.doctrine-project.org/projects/doctrine-orm/en/2.10/tutorials/pagination.html)
- Ability to work with a collection as an array
- Possibility of pagination (in batches) using ->batch(...), ->slice(...), ->page(...)
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