Download the PHP package igaster/eloquent-decorator without Composer
On this page you can find all versions of the php package igaster/eloquent-decorator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download igaster/eloquent-decorator
More information about igaster/eloquent-decorator
Files in igaster/eloquent-decorator
Package eloquent-decorator
Short Description A simple Trait that implements the Decorator pattern on Laravel eloquent models.
License MIT
Homepage https://github.com/igaster/eloquent-decorator.git
Informations about the package eloquent-decorator
Description
A simple Trait that implements the Decorator pattern on Laravel eloquent models.
Usage:
- Extend a model without inheritance (Composition vs Inheritance)
- Create multiple variations of a model
Example case study:
You often need multiple versions of each Image. Your base model (Image) handles Database, Relations etc.
Your decorated models (ie thumbnail, profileImage) may apply image manipulations to the original image to create variations. ie they can overide the src() method of the base class to point to a different file. All your decorated versions share the same record in the Database but my alter their representation/behavior!
More ideas:
- Base model:
User
. Decorated Objects: Member, Moderator, SuperUser etc - Base model:
Article
. Decorated Objects: FeaturedArticle, BlogPost,
Installation
Edit your project's composer.json
file to require:
"require": {
"igaster/eloquent-decorator": "~1.0"
}
and install with `composer update
Implement the Decorator Pattern
Step 1: Class Decleration:
Your class should implement some interfaces to mimic the behavior of Eloquent models:
Step 2: Decorating an Eloquent model:
Need a constructor?
You have to override EloquentDecoratorTrait
and create your own factory function. Dont forget to call self::wrap($object)
at the end.