serafim/gist

This package is abandoned and no longer maintained. The author suggests using the illuminate/collections package instead.

Model json serialize filters for Laravel 4

1.0.0 2014-07-17 10:25 UTC

This package is auto-updated.

Last update: 2020-08-08 22:02:35 UTC


README

Model json serialize filters for Laravel 4

Installation

  1. Add composer requirement:
{
    "require": {
        "serafim/gist": "dev-master"
    }
}
  1. [optional] Add Laravel facade inside app/config/app.php file:
    'aliases' => [
        /* 
          another aliases... 
        */
        
        'Gist' => 'Gist\Support\GistFacade'
    ]

Usage

You can add filter for model or model collection inside your controller.

Method make can take one of the following values:

  • Model instance
  • Model collection
  • External filter class name and one of the preceding claims as the second argument
  • null (empty model finder result)

Included filters

Example:

class UserController extends \BaseController
{
   public function index()
   {
        return Gist::filter(User::all())
            ->only('id', 'login');
            ->set('created', function($model) {
                return [
                    'rfc2822' => $model->created_at->toRFC2822String(),
                    'timestamp' => $model->created_at->timestamp
                ];
            })
            ->rename('login', 'name');
   }
}

Returns:

[{"id": "USER_ID", "name": "USER_LOGIN", "created": {"rfc2822": "CREATED_RFC2822_FORMAT", "timestamp": "CREATED_UNIX_TIMESTAMP"}]

Methods

Method only says that we should return these properties model.

->only((string)$property [, (string)$property])

Method set says that should the specified property should return a function result. Callback $callback takes a single value - ActiveRecord model instance.

->set((string)$property, (callable)$callback)

Method rename renames the specified property.

->rename((string)$source, (string)$target);

External filters

You can create external filters

class UserFilter
{
    use Gist\Traits\FilterTrait;
  
    protected $properties = [
        'id',
        'login' => 'name'
    ];
  
    protected $set = [
        'created' => 'getTime'
    ];
  
    public function getTime($model)
    {   
        return [
            'rfc2822' => $model->created_at->toRFC2822String(),
            'timestamp' => $model->created_at->timestamp
        ];
    }
    
    public function produce($filter)
    {
        return $filter;
    }
}

And use them

class UserController extends \BaseController
{
   public function index()
   {
        return Gist::filter('UserFilter', User::all());
   }
}

Capabilities list

Property $properties indicates which field model (or collection) costs to return.

protected $properties = [
  /* ->only(PROPERTY_NAME) */
  PROPERTY_NAME,
  
  /* ->only(PROPERTY_NAME)->rename(PROPERTY_NAME, PROPERTY_ALIAS) */
  PROPERTY_NAME => PROPERTY_ALIAS 
];

Property $set indicates what value add to the result, which as a result will filter method.

protected $set = [
  PROPERTY_NAME => METHOD_NAME
];

public function METHOD_NAME((Model)$model) 
{
}

Method produce is called automatically and takes one parameter - a pre-existing model filter.

public function produce($filter)
{
    $filter->only(***);
    $filter->rename('some', 'any');
}