All Projects → nahid → Presento

nahid / Presento

Presento - Transformer & Presenter Package for PHP

Projects that are alternatives of or similar to Presento

Asr
Stars: ✭ 54 (-23.94%)
Mutual labels:  transformer
Array view
Wrapper for references to array in C++.
Stars: ✭ 58 (-18.31%)
Mutual labels:  array
Indonesian Language Models
Indonesian Language Models and its Usage
Stars: ✭ 64 (-9.86%)
Mutual labels:  transformer
Zarr.js
Javascript implementation of Zarr
Stars: ✭ 54 (-23.94%)
Mutual labels:  array
Cartesian Product
PHP - A simple, low-memory footprint function to generate all combinations from a multi-dimensionnal array.
Stars: ✭ 58 (-18.31%)
Mutual labels:  array
Viewpagertransformer
Viewpager动画,包括渐变,旋转,缩放,3D,立方体等多种酷炫效果动画,实现原理是自定义ViewpagerTransformer,当然你也可以自定义多种动画
Stars: ✭ 62 (-12.68%)
Mutual labels:  transformer
Geeksforgeeks Dsa 2
This repository contains all the assignments and practice questions solved during the Data Structures and Algorithms course in C++ taught by the Geeks For Geeks team.
Stars: ✭ 53 (-25.35%)
Mutual labels:  array
Mixture Of Experts
A Pytorch implementation of Sparsely-Gated Mixture of Experts, for massively increasing the parameter count of language models
Stars: ✭ 68 (-4.23%)
Mutual labels:  transformer
Array Sort
Fast and powerful array sorting. Sort an array of objects by one or more properties. Any number of nested properties or custom comparison functions may be used.
Stars: ✭ 58 (-18.31%)
Mutual labels:  array
Gpt2
PyTorch Implementation of OpenAI GPT-2
Stars: ✭ 64 (-9.86%)
Mutual labels:  transformer
Laravel Graphql
GraphQL implementation with power of Laravel
Stars: ✭ 56 (-21.13%)
Mutual labels:  transformer
Transformer Dynet
An Implementation of Transformer (Attention Is All You Need) in DyNet
Stars: ✭ 57 (-19.72%)
Mutual labels:  transformer
Hibernate Types
The Hibernate Types library gives you extra types that are not supported by the Hibernate ORM core.
Stars: ✭ 1,122 (+1480.28%)
Mutual labels:  array
Vietnamese Electra
Electra pre-trained model using Vietnamese corpus
Stars: ✭ 55 (-22.54%)
Mutual labels:  transformer
Kaggle Quora Insincere Questions Classification
Kaggle新赛(baseline)-基于BERT的fine-tuning方案+基于tensor2tensor的Transformer Encoder方案
Stars: ✭ 66 (-7.04%)
Mutual labels:  transformer
Mojito
微信、bilibili大图、长图、gif、视频、自定义view的转场效果,The transition effect of wechat, bilibili large image, long image, GIF, video and custom view
Stars: ✭ 1,068 (+1404.23%)
Mutual labels:  transformer
Pycuda
CUDA integration for Python, plus shiny features
Stars: ✭ 1,112 (+1466.2%)
Mutual labels:  array
Openasr
A pytorch based end2end speech recognition system.
Stars: ✭ 69 (-2.82%)
Mutual labels:  transformer
Mjextension
A fast, convenient and nonintrusive conversion framework between JSON and model. Your model class doesn't need to extend any base class. You don't need to modify any model file.
Stars: ✭ 8,458 (+11812.68%)
Mutual labels:  array
Deeplearning Nlp Models
A small, interpretable codebase containing the re-implementation of a few "deep" NLP models in PyTorch. Colab notebooks to run with GPUs. Models: word2vec, CNNs, transformer, gpt.
Stars: ✭ 64 (-9.86%)
Mutual labels:  transformer

Presento

A data preparing and presenting package for PHP.

Why Presento?

Presento is a simple but powerful tools for preparing and presenting data. When we build an API based application, we need to transform the data before present it through the response. This package will make this task easier for you.

Not clear enough?

Don't worry, you'll get better idea from the Usage examples.

Requirements

PHP >= 7.0
ext-json

Installation

Install the package using composer:

composer require nahid/presento

Usage

Presento serves two important purposes. one is Presentation and another is Transformation of the data.

Let's see some examples to understand how to use it.

We'll use the following data set to show the examples. Let's say we've this data set fetched from some data source and need to do some transformation or modifications before sending it to the response.

$response = [
    "id" => 123456,
    "name" => "Nahid Bin Azhar",
    "email" => "[email protected]",
    "type" => 1,
    "is_active" => 1,
    "created_at" => "2018-01-02 02:03:04",
    "updated_at" => "2018-01-02 02:03:04",
    "deleted_at" => "2018-01-02 02:03:04",
    "projects" => [
        [
            "id" => 1,
            "name" => "Laravel Talk",
            "url"   => "https://github.com/nahid/talk",
            "license" => "CC0",
            "created_at" => "2016-02-02 02:03:04"
        ],
        [
            "id" => 2,
            "name" => "JsonQ",
            "url"   => "https://github.com/nahid/jsonq",
            "license" => "MIT",
            "created_at" => "2018-01-02 02:03:04"
        ]
    ]
];

Simple Presentation Example

When sending this data to the API response, we only want to send the id, name, email, type, is_active and projects.

We can simply do that by preparing a Presenter for this like following.

// UserPresenter.php

class UserPresenter extends \Nahid\Presento\Presenter
{
    public function present() : array
    {
        return [
            'id',
            'name',
            'email',
            'type',
            'is_active',
            'projects',
        ];
    }
}

And you might already guessed how to use it, right?

$user = new UserPresenter($response);
dump($user->get());

It'd show something like this:

[
    "id" => 123456,
    "name" => "Nahid Bin Azhar",
    "email" => "[email protected]",
    "type" => 1,
    "is_active" => 1,
    "projects" => [
        [
            "id" => 1,
            "name" => "Laravel Talk",
            "url"   => "https://github.com/nahid/talk",
            "license" => "CC0",
            "created_at" => "2016-02-02 02:03:04"
        ],
        [
            "id" => 2,
            "name" => "JsonQ",
            "url"   => "https://github.com/nahid/jsonq",
            "license" => "MIT",
            "created_at" => "2018-01-02 02:03:04"
        ]
    ]
]

Pretty simple, right?

'key' aliasing in Presentation example

Let's say you want to change some of the 'key' to something different, like the id key to user_id . How can you do that?

Just do the following.

// UserPresenter.php
class UserPresenter extends \Nahid\Presento\Presenter
{
    public function present() : array
    {
        return [
            'user_id' => 'id',
            'name',
            'email',
            'type',
            'is_active',
        ];
    }
}

This will format the data like following:

[
    "user_id" => 123456,
    "name" => "Nahid Bin Azhar",
    "email" => "[email protected]",
    "type" => 1,
    "is_active" => 1,
]

Deep traversing in Presentation example

You can easily dive deep and get data from a nested level by using . (dot) notation.

Let's say you want to show the name of the first package as the top_package in your data.

This is how you do it.

// UserPresenter.php
class UserPresenter extends \Nahid\Presento\Presenter
{
    public function present() : array
    {
        return [
            'id',
            'name',
            'email',
            'type',
            'is_active',
            'top_package' => 'projects.0.name',
            'projects',
        ];
    }
}

This will format the data like this:

[
    "id" => 123456,
    "name" => "Nahid Bin Azhar",
    "email" => "[email protected]",
    "type" => 1,
    "is_active" => 1,
    "top_package" => "Laravel Talk",
    "projects" => [
        [
            "id" => 1,
            "name" => "Laravel Talk",
            "url"   => "https://github.com/nahid/talk",
            "license" => "CC0",
            "created_at" => "2016-02-02 02:03:04"
        ],
        [
            "id" => 2,
            "name" => "JsonQ",
            "url"   => "https://github.com/nahid/jsonq",
            "license" => "MIT",
            "created_at" => "2018-01-02 02:03:04"
        ]
    ]
]

Notice the top_package key in the data.

Simple Transformer Example

Let's say our UserPresenter is like this:

// UserPresenter.php
class UserPresenter extends \Nahid\Presento\Presenter
{
    public function present() : array
    {
        return [
            'user_id' => 'id',
            'name',
            'email',
            'type',
            'is_active',
        ];
    }
}

And we want to show the user_id as hashed value instead of an incremental integer value as it is in our database. That means we want to transform the user_id.

To do that we need to create a Transformer Class like this:

// UserTransformer.php
class UserTransformer extends \Nahid\Presento\Transformer
{
    public function getUserIdProperty($value)
    {
        return md5($value);
    }
}

Notice that, as we will transform the user_id property, we named our transformer method as getUserIdProperty. So, if you want to transform the name property too, you need to just create another method in this class named getNameProperty and add the transformation logic inside it.

Now, we need to let know the Presenter how to Transform the data before presenting it.

To do that, we need to add the following method in the UserPresenter class.

// UserPresenter.php
public function transformer()
{
    return UserTransformer::class;
}

So, our final output would be:

[
    "user_id" => "e10adc3949ba59abbe56e057f20f883e",
    "name" => "Nahid Bin Azhar",
    "email" => "[email protected]",
    "type" => 1,
    "is_active" => 1,
]

Ain't it easy, mate?

Nested Presenter Example

You might notice that there is a collection of projects in our data set. If each project is a separate resource, you might have a separate Presenter for that. Like this:

// ProjectPresenter.php
class ProjectPresenter extends \Nahid\Presento\Presenter
{
    public function present() : array
    {
        return [
            'id',
            'name',
            'url',
            'license',
            'created_at',
        ];
    }

    public function transformer()
    {
        return ProjectTransformer::class;
    }
}

Can you use this Presenter for each of the projects in the Users data?

Hell Yeah! Just do this:

// UserPresenter.php
public function present() : array
{
    return [
        'user_id' => 'id',
        'name',
        'email',
        'type',
        'is_active',
        'projects' => [ProjectPresenter::class => ['projects']],
    ];
}

Now, each of the project in the list of projects in Users will be presented as defined in the ProjectPresenter.

Base Data format conversion Example

As you have seen that, the data set we have used till now is a plain Array. But some times it might not be the case. You might need to work with something different, like Eloquent Model of Laravel framework. In that case, you can simply add a method called convert in your Presenter to convert the Base data to an Array format.

Let's see an Example:

// UserPresenter.php
public function convert($data)
{
    if ($data instanceof Model) {
        return $data->toArray();
    }

    return $data;
}

That's it.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].