dan-har / presentit-laravel by danhar

Transformers for nested Eloquent models and collection for building API with Laravel framework
27
0
1
Package Data
Maintainer Username: danhar
Maintainer Contact: dan@niddit.com (Dan Har)
Package Create Date: 2017-01-28
Package Last Update: 2017-07-04
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-23 03:12:31
Package Statistics
Total Downloads: 27
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

presentit-laravel

Presentit adapter for laravel framework.

Custom presentations and transformation of nested Eloquent models, models relations and collections.

See full presentit docs here

Docs

Installation

Install using composer

composer require dan-har/presentit-laravel

Add the presentit service provider to the app config file

'providers' => [
    // ...
    Presentit\Laravel\PresentitServiceProvider::class,
]

Transform Eloquent models

Use presentit transformation functionality with any Eloquent model by implementing the Presentable contract and using the PresentsItem trait.

For example, the User model class with the PresentsItem trait

class User extends Authenticatable implements Presentable
{
    use PresentsItem;
    
    //...
}

To transform the user model use the present method to get a Present instance or use the transfrom method to use a transformer.

$user = User::find(1);

$user->present()->with(function(User $user){
    return [
        //...
    ];
});

$user->transform(function(User $user){
    return [
        //...
    ];
});

Instead of closure transformer you can pass a transformer class, see presentit docs or example below.

Transform collections

To transform collections the present and transformWith macros were added to the base collection.

$posts = Collection::make();

$posts->present()->each(function (Post $post) {
    return [
        //...
    ];
});

$posts->transformWith(function (Post $post) {
    return [
        //...
    ];
});

Model relations that returns collections such as HasMany will also have the presentit transformation functionality

The collection presentit api uses the transformWith method because the transform method exists in the base laravel collection.

class Post implements Presentable
{
    use PresentsItem;
    
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

$posts = Posts::find(1);

$posts->comments->transformWith(function (Comment $comment) {
    return [
        //...
    ];
});

Transform nested models and relations

To demonstrate the nested model transformation we will use an example of a Post with comments and on each comment users can write comments. So first we use a transformer class for the Post, Comment and User model

class UserTransformer
{
    public function transform(User $user) {
        return [
            'name' => ucfirst($user->name),
            'profile_image' => $user->profile_image ?: Hidden::key(),
        ];
    }
}


class CommentTransformer
{
    public function transform(Comment $comment)
    {
        return [
            'text' => $comment->text,
            'datetime' => $comment->created_at->toW3cString(),
            'edited_datetime' => $comment->edited_at ? $comment->edited_at : Hidden::key(),
            'user' => $comment->user->transform(UserTransformer::class),
            'comments' => $comment->comments->transformWith(CommentTransformer::class),
        ];
    }
}


class PostTransformer
{
    public function tranfrom(Post $post)
    {
        return [
            'title' => $post->title,
            'text' => $post->text,
            'user' => $post->user->transform(UserTransformer::class),
            'datetime' => $post->created_at->toW3cString(),
            'comments' => $post->comments->transformWith(CommentTransformer::class),
        ];
    }
}

Then to transform a single post use

$post = Post::find(1);

$array = $post->transform(PostTransformer::class)->show();