Package Data | |
---|---|
Maintainer Username: | igaster |
Maintainer Contact: | igasteratos@gmail.com (Giannis Gasteratos) |
Package Create Date: | 2015-12-27 |
Package Last Update: | 2019-10-11 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-08 03:23:26 |
Package Statistics | |
---|---|
Total Downloads: | 6,298 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 5 |
Total Forks: | 2 |
Total Open Issues: | 0 |
A simple Trait that implements the Decorator pattern on Laravel eloquent models.
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:
User
. Decorated Objects: Member, Moderator, SuperUser etcArticle
. Decorated Objects: FeaturedArticle, BlogPost,Edit your project's composer.json
file to require:
"require": {
"igaster/eloquent-decorator": "~1.0"
}
and install with `composer update
Your class should implement some interfaces to mimic the behavior of Eloquent models:
use igaster\EloquentDecorator\EloquentDecoratorTrait;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Queue\QueueableEntity;
use ArrayAccess;
use JsonSerializable;
class SuperUser implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
use EloquentDecoratorTrait;
public $newProperty; // Add a new property
public $overridenPropery; // or Overide a property (it hides it)
public function TruncateDatabase(){
// add your functions...
}
}
$user = User::find(1);
$superUser = SuperUser::wrap($user);
// you can think the SuperUser class as inhereted from the User model. so you can still do:
$superUser->name = "Admin";
$superUser->save();
// But you also have access to SuperUser functions/attributes:
$superUser->TruncateDatabase();
// You can always access to the original (decorated object):
$superUser->object->someProperty;
}
You have to override EloquentDecoratorTrait
and create your own factory function. Dont forget to call self::wrap($object)
at the end.