rekalogika / doctrine-collections-decorator
Lets you easily create decorator classes to dynamically modify the behaviors of Doctrine Collection objects, including the collection objects used by Doctrine ORM in your entities.
Fund package maintenance!
priyadi
Installs: 4 003
Dependents: 2
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^8.1
- doctrine/collections: ^2.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.18.4 || ^0.19
- symfony/phpunit-bridge: ^6.3 || ^7.0
- symfony/var-dumper: ^6.3 || ^7.0
- vimeo/psalm: ^5.15
This package is auto-updated.
Last update: 2024-12-01 00:21:14 UTC
README
Lets you easily create decorator classes to dynamically modify the behaviors of Doctrine Collection objects, including the collection objects used by Doctrine ORM in your entities.
Motivation
Custom collection classes won't come to Doctrine ORM anytime soon. Therefore, the only way to modify the behavior of a Doctrine collection is to use decorators. However, creating a Collection decorator by hand is a tedious process.
Synopsis
The decorator class extending one of our abstract classes:
use Doctrine\Common\Collections\Collection; use Rekalogika\Collections\Decorator\AbstractCollectionDecorator; /** * @extends AbstractCollectionDecorator<array-key,Book> */ class BookCollection extends AbstractCollectionDecorator { /** * @param Collection<array-key,Book> $collection */ public function __construct(private Collection $collection) { } protected function getWrapped(): Collection { return $this->collection; } // add and override methods here: // ... }
The usage in an entity:
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity()] class BookShelf { /** * @var Collection<array-key,Book> */ #[ORM\OneToMany(targetEntity: Book::class)] private Collection $books; public function __construct() { $this->books = new ArrayCollection(); } public function getBooks(): BookCollection { return new BookCollection($this->bookskkk); } }
Documentation
rekalogika.dev/doctrine-collections-decorator
License
MIT
Contributing
Issues and pull requests should be filed in the GitHub repository rekalogika/doctrine-collections-decorator.