2017 © Pedro Peláez
 

yii2-extension rest-renderer

For Yii2: Just like Controller::Action but only for having multiple rendering templates in same place for data models.

image

vivre-tech/rest-renderer

For Yii2: Just like Controller::Action but only for having multiple rendering templates in same place for data models.

  • Thursday, December 14, 2017
  • by microThread
  • Repository
  • 2 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 58 % Grown

The README.md

This extension provides the ability to have multiple rendering templates in one place for data models., (*1)

For license information check the LICENSE-file., (*2)

Latest Stable Version Total Downloads Build Status, (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require --prefer-dist vivre-tech/rest-renderer

or add, (*6)

"vivre-tech/rest-renderer": "*"

to the require section of your composer.json., (*7)

Usage

Example:, (*8)

use vivretech\rest\renderer\DataRenderer;


class DummyModelRenderer extends DataRenderer
{

    /**
     * @param array $params
     * @return mixed
     */
    public function renderMain($params = [])
    {
        return [];
    }


    public function renderSummary($model)
    {
        return [
            'name' => $model['name'],
            'price' => $model['price'],
        ];
    }


    public function renderDetailed($model)
    {
        return [
            'id' => $model['id'],
            'name' => $model['name'],
            'price' => $model['price'],
            'created_at' => $model['created_at'],
        ];
    }

}


$render = new DummyModelRenderer();
$productModel = [
    'id' => 1,
    'name' => 'Product 1',
    'price' => 100,
    'created_at' => date('Y-m-d H:i:s')
];


/* Output JSON -> Dummy REST response. */
header("Content-Type: application/json;charset=utf-8");

echo
    json_encode([
        'productSummary' => $render->run('summary', [$productModel]),
        'productDetailed' => $render->run('Detailed', [$productModel]),
    ]);

Response

{
    "productSummary": {
        "name": "Product 1",
        "price": 100
    },
    "productDetailed": {
        "id": 1,
        "name": "Product 1",
        "price": 100,
        "created_at": "2017-12-14 08:25:06"
    }
}

Unit Testing

If you run the following command: composer install in a dev environment then you will find phpunit in /vendor/bin/phpunit., (*9)

In case phpunit in not installed via command composer install, just fallow next steps: 1. run in console/terminal brew install phpunit, (*10)

To test, in the root of the project, base on how phpunit is installed you will have two choices to run: 1. installed via command composer install you will have to execute in console/terminal: vendor/bin/phpunit 2. installed via bre you will have to execute in console/terminal: phpunit, (*11)

The Versions

14/12 2017

dev-master

9999999-dev

For Yii2: Just like Controller::Action but only for having multiple rendering templates in same place for data models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Banciu N. Cristian Mihai

api rest yii2 renderer data-renderer rest-renderer

14/12 2017

1.0.0

1.0.0.0

For Yii2: Just like Controller::Action but only for having multiple rendering templates in same place for data models.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Banciu N. Cristian Mihai

api rest yii2 renderer data-renderer rest-renderer