PHP code example of markmercedes / trest

1. Go to this page and download the library: Download markmercedes/trest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

markmercedes / trest example snippets




/**
* You should allways define the constant `TREST_DEFAULT_CACHE_TTL`, this constant the default time that items will be     * cached in seconds, if you don't wanna catch your request define it and provide a value of 0/
*/
define('TREST_DEFAULT_CACHE_TTL', 120);

/**
 * The config named 'default' will be used for the models without the property
 * => protected static $configName = 'StackOverflow';
 */
ConfigFactory::add('default', new Config(array(
    'apiUrl' => 'http://pixelpt-sandwich-api.herokuapp.com/',
    'singleItemNode' => 'sandwich',
    /**
    * Here you can provide a cache adapter to your connection.
    *
    * To create your own cache adapters, implement the interface TRest\Cache\CacheAdapterInterface
    * and provide an instance of your class to the configuration key named cacheAdapter of your connection.
    *
    * Here should be passed an instance of a class that implements the interface TRest\Cache\CacheAdapterInterface.
    *
    */
    'cacheAdapter' => new ClassImplementingCacheAdapterInterface()
)));

$sandwich = Sandwich::findOne($id); // GET http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id

$sandwich->name = 'Double bacon cheese';
$sandwich->price = 9000;

$sandwich->save(); // PUT http://pixelpt-sandwich-api.herokuapp.com/sandwich/$id (title=Double bacon cheese, price=9000)



namespace Entities;

use TRest\Models\Model;

class Sandwich extends Model
{

    /**
    * The name of the resource
    */
    protected static $resource = 'sandwiches';

    /**
    * In the case of this particular API, a single item in returned inside a node named with the resource
    * name singularized
    */
    protected static $singleItemNode = 'sandwich';
    /**
    * In the case of this particular API, a list of items in returned inside a node named with the resource
    * name pluralized
    */
    protected static $listItemNode = 'sandwiches';

    /*
    * Field definition
    */
    public function fields() {
        return array(
            'id' => array(
                'type' => 'integer'
            ),
            'title' => array(
                'type' => 'string'
            ),
            'price' => array(
                'type' => 'integer'
            )
        );
    }

    /**
    * Retlations
    */
    public function relations() {
        return array(
            'ingredients' => array(
                'class' => 'Ingredient',
                'type' => self::HAS_MANY,
                'postOnSave' => true,
                'postSuffix' => '_attributes'
            )
        );
    }
}

class Ingredient extends Model{

    protected static $resource = 'ingredients';

    protected static $singleItemNode = 'ingredient';

    protected static $listItemNode = 'ingredients';

    public function relations() {
        return array(
            'sandwich' => array(
                'class' => 'Sandwich',
                'type' => self::BELONGS_TO
            )
        );
    }

    public function fields() {
        return array(
            'id' => array(
                'type' => 'integer'
            ),
            'name' => array(
                'type' => 'string'
            ),
            'quantity' => array(
                'type' => 'integer'
            )
        );
    }

}

$sandwich = new Sandwich();
$sandwich->title = "mark";
$sandwich->price = 9200;
$sandwich->ingredients = [
    new Ingredient(
        (object)['name' => 'Bacon', 'quantity' => 2]
    ),
    new Ingredient(
        (object)['name' => 'Cheese', 'quantity' => 1]
    )
];
$sandwich->save();
/**
* POST http://pixelpt-sandwich-api.herokuapp.com/
* Results {"id":154,"title":"mark","price":9200,
* "ingredients":[{"id":315,"name":"Bacon","quantity":2},{"id":316,"name":"Cheese","quantity":1}]}
*/

$sandwich->id; // => 154
$sandwich->ingrediends[0]->id; // => 315

$sandwich = Sandwich::findOne($sandwich->id); // GET http://pixelpt-sandwich-api.herokuapp.com/154

$sandwiches = Sandwich::find()-all();