igaster/eloquent-decorator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

igaster/eloquent-decorator

Composer 安装命令:

composer require igaster/eloquent-decorator

包简介

A simple Trait that implements the Decorator pattern on Laravel eloquent models.

README 文档

README

Laravel Downloads Build Status License

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:

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...
    }
}

Step 2: Decorating an Eloquent model:

	$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;
}

Need a constructor?

You have to override EloquentDecoratorTrait and create your own factory function. Dont forget to call self::wrap($object) at the end.

统计信息

  • 总下载量: 6.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 4
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固