Commit e865fb96 authored by endi's avatar endi

first commit

parents
/vendor
composer.phar
composer.lock
.DS_Store
\ No newline at end of file
language: php
php:
- 5.3
- 5.4
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
script: phpunit
\ No newline at end of file
## RESTful format response for Laravel
### For Laravel 4, please use the [v1.x branch](https://github.com/teepluss/laravel-restable/tree/v1.x)!
Restable is a useful to create RESTful API response format that support multiple format, such as Json, XML
Serialized, PHP.
### Installation
- [Restable on Packagist](https://packagist.org/packages/teepluss/restable)
- [Restable on GitHub](https://github.com/teepluss/laravel-restable)
To get the lastest version of Theme simply require it in your `composer.json` file.
~~~
"teepluss/restable": "dev-master"
~~~
You'll then need to run `composer install` to download it and have the autoloader updated.
Once Theme is installed you need to register the service provider with the application. Open up `config/app.php` and find the `providers` key.
~~~
'providers' => array(
'Teepluss\Restable\RestableServiceProvider'
)
~~~
Restable also ships with a facade which provides the static syntax for creating collections. You can register the facade in the `aliases` key of your `config/app.php` file.
~~~
'aliases' => array(
'Restable' => 'Teepluss\Restable\Facades\Restable'
)
~~~
Publish config using artisan CLI.
~~~
php artisan vendor:publish
~~~
## Usage
Create reponses format for RESTful.
Example:
~~~php
class ApiBlogsController extends BaseController {
/**
* Checking permission.
*
* @return Response
*/
public function __construct()
{
if ( ! Input::get('secret') == '12345')
{
return Restable::unauthorized()->render();
}
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
// Set default response format.
//Restable::setDefaultFormat('xml');
// Override format response.
//return Restable::listing(Blog::paginate())->to('xml');
//return Restable::listing(Blog::paginate())->toXML();
return Restable::listing(Blog::paginate())->render();
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('api.blogs.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$blog = new Blog;
$validator = Validator::make(Input::all(), array(
'title' => 'required',
'description' => 'required'
));
if ($validator->fails())
{
return Restable::unprocess($validator)->render();
}
$blog->title = Input::get('title');
$blog->description = Input::get('description');
$blog->save();
return Restable::created($blog)->render();
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$blog = Blog::find($id);
if ( ! $blog)
{
return Restable::missing()->render();
}
return Restable::single($blog)->render();
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$blog = Blog::find($id);
if ( ! $blog)
{
return Restable::missing()->render();
}
return View::make('api.blogs.edit', compact('blog'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$blog = Blog::find($id);
if ( ! $blog)
{
return Restable::missing()->render();
}
$validator = Validator::make(Input::all(), array(
'title' => 'required',
'description' => 'required'
));
if ($validator->fails())
{
return Restable::unprocess($validator)->render();
}
$blog->title = Input::get('title');
$blog->description = Input::get('description');
$blog->save();
return Restable::updated($blog)->render();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$blog = Blog::find($id);
if ( ! $blog)
{
return Restable::missing()->render();
}
$blog->delete();
return Restable::deleted()->render();
}
}
~~~
Error cases.
~~~php
// Unauthorized.
Restable::unauthorized()->render();
// Bad request.
Restable::bad()->render();
// Missing, Not found.
Restable::missing()->render();
// Unprocess, Validation Failed.
Restable::unprocess()->render();
// Custom.
Restable::error(null, 429)->render();
~~~
Another success cases.
~~~php
return Restable::success()->render();
~~~
Changing error code.
~~~php
return Restable::code(9001)->bad('message')->render();
~~~
Render to another format.
~~~php
// XML
return Restable::single($data)->render('xml');
// Serialized
return Restable::single($data)->render('serialized');
// PHP
return Restable::single($data)->render('php');
// JSON
return Restable::single($data)->render('json');
// JSONP
return Restable::single($data)->render('json', Input::get('callback'));
// OR
return Restable::single($data)->toJson(Input::get('callback'));
~~~
## Support or Contact
If you have some problem, Contact teepluss@gmail.com
[![Support via PayPal](https://rawgithub.com/chris---/Donation-Badges/master/paypal.jpeg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9GEC8J7FAG6JA)
\ No newline at end of file
{
"name": "teepluss/restable",
"description": "Laravel RESTful API format",
"keywords": ["laravel", "laravel", "restful", "api"],
"homepage": "https://github.com/teepluss/laravel-restable",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Teepluss",
"email": "admin@laravel.in.th"
}
],
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Teepluss\\Restable\\": "src/"
}
},
"minimum-stability": "dev"
}
\ No newline at end of file
<?php
return [
/*
|--------------------------------------------------------------------------
| Retrieves a list of contents
|--------------------------------------------------------------------------
|
| Get multi records.
|
*/
'listing' => [
'response' => ':response',
'header' => 200
],
/*
|--------------------------------------------------------------------------
| Retrieves a specific content
|--------------------------------------------------------------------------
|
| Get single record.
|
*/
'show' => [
'response' => ':response',
'header' => 200
],
/*
|--------------------------------------------------------------------------
| Creates a new content
|--------------------------------------------------------------------------
|
| Insert record.
|
*/
'created' => [
'response' => ':response',
'header' => 201
],
/*
|--------------------------------------------------------------------------
| Updates a specific content
|--------------------------------------------------------------------------
|
| Update record.
|
*/
'updated' => [
'response' => ':response',
'header' => 200
],
/*
|--------------------------------------------------------------------------
| Deletes a specific content
|--------------------------------------------------------------------------
|
| Delete record.
|
*/
'deleted' => [
'response' => null,
'header' => 204
],
/*
|--------------------------------------------------------------------------
| Success
|--------------------------------------------------------------------------
|
| OK.
|
*/
'success' => [
'response' => [
'message' => ':response|OK'
],
'header' => 200
],
/*
|--------------------------------------------------------------------------
| Error 400
|--------------------------------------------------------------------------
|
| Bad Request.
|
*/
'error_400' => [
'response' => [
'code' => 400,
'message' => 'Bad Request',
'description' => ':response|The request was invalid or cannot be otherwise served.'
],
'header' => 400
],
/*
|--------------------------------------------------------------------------
| Error 401
|--------------------------------------------------------------------------
|
| Unauthorized.
|
*/
'error_401' => [
'response' => [
'code' => 401,
'message' => 'Unauthorized',
'description' => ':response|Authentication credentials were missing or incorrect.'
],
'header' => 401
],
/*
|--------------------------------------------------------------------------
| Error 403
|--------------------------------------------------------------------------
|
| Forbidden.
|
*/
'error_403' => [
'response' => [
'code' => 403,
'message' => 'Forbidden',
'description' => ':response|The request is understood, but it has been refused or access is not allowed.'
],
'header' => 403
],
/*
|--------------------------------------------------------------------------
| Error 404
|--------------------------------------------------------------------------
|
| Not found.
|
*/
'error_404' => [
'response' => [
'code' => 404,
'message' => 'Not found',
'description' => ':response|The request was not found.'
],
'header' => 404
],
/*
|--------------------------------------------------------------------------
| Error 405
|--------------------------------------------------------------------------
|
| Method Not Allowed.
|
*/
'error_405' => [
'response' => [
'code' => 405,
'message' => 'Method Not Allowed',
'description' => ':response|Request method is not allowed.'
],
'header' => 405
],
/*
|--------------------------------------------------------------------------
| Error 406
|--------------------------------------------------------------------------
|
| Not Acceptable.
|
*/
'error_406' => [
'response' => [
'code' => 406,
'message' => 'Not Acceptable',
'description' => ':response|Returned when an invalid format is specified in the request.'
],
'header' => 406
],
/*
|--------------------------------------------------------------------------
| Error 410
|--------------------------------------------------------------------------
|
| Gone.
|
*/
'error_410' => [
'response' => [
'code' => 410,
'message' => 'Gone',
'description' => ':response|This resource is gone. Used to indicate that an API endpoint has been turned off.'
],
'header' => 410
],
/*
|--------------------------------------------------------------------------
| Error 422 (Validation error)
|--------------------------------------------------------------------------
|
| Unprocessable Entity.
|
*/
'error_422' => [
'response' => [
'code' => 422,
'message' => 'Unprocessable Entity',
'description' => 'Data is unable to be processed.',
'errors' => ':response'
],
'header' => 422
],
/*
|--------------------------------------------------------------------------
| Error 429
|--------------------------------------------------------------------------
|
| Too Many Requests.
|
*/
'error_429' => [
'response' => [
'code' => 429,
'message' => 'Too Many Requests',
'description' => ':response|Request cannot be served due to the application\'s rate limit having been exhausted for the resource.'
],
'header' => 429
]
];
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
\ No newline at end of file
<?php namespace Terranet\Restable\Contracts;
interface Restable
{
/**
* Response listing.
*
* @param array $messages
* @return string
*/
public function listing($messages);
/**
* Response single.
*
* @param array $messages
* @return string
*/
public function single($messages);
/**
* Response created.
*
* @param array $messages
* @return string
*/
public function created($messages);
/**
* Response updated.
*
* @param array $messages
* @return string
*/
public function updated($messages);
/**
* Response deleted.
*
* @return string
*/
public function deleted();
/**
* Simple response success.
*
* @param mixed $message
* @return string
*/
public function success($message);
/**
* Response error.
*
* @param array $messages
* @param int $type
* @return string
*/
public function error($messages, $type = 400);
/**
* Unauthorized.
*
* @param mixed $description
* @return string
*/
public function unauthorized($description = null);
/**
* Any error return 400 as bad request.
*
* @param mixed $description
* @return string
*/
public function bad($description = null);
/**
* Alias of error 404 response.
*
* @param array $description
* @return string
*/
public function missing($description = null);
/**
* Alias of error 422 response.
*
* @param array $errors
* @return string
*/
public function unprocess($errors);
/**
* Render response with format.
*
* @param string $format
* @return mixed
*/
public function render($format = null, $callback = null);
}
\ No newline at end of file
<?php namespace Terranet\Restable\Contracts;
interface SelfRendered
{
/**
* Response listing.
*
* @param array $messages
* @return string
*/
public function listing($messages);
/**
* Response single.
*
* @param array $messages
* @return string
*/
public function single($messages);
/**
* Response created.
*
* @param array $messages
* @return string
*/
public function created($messages);
/**
* Response updated.
*
* @param array $messages
* @return string
*/
public function updated($messages);
/**
* Response deleted.
*
* @return string
*/
public function deleted();
/**
* Simple response success.
*
* @param mixed $message
* @return string
*/
public function success($message);
/**
* Unauthorized.
*
* @param mixed $description
* @return string
*/
public function unauthorized($description = null);
/**
* Any error return 400 as bad request.
*
* @param mixed $description
* @return string
*/
public function bad($description = null);
/**
* Alias of error 404 response.
*
* @param array $description
* @return string
*/
public function missing($description = null);
/**
* Alias of error 422 response.
*
* @param array $error
* @return string
*/
public function unprocess($error);
}
\ No newline at end of file
<?php namespace Terranet\Restable\Facades;
use Illuminate\Support\Facades\Facade;
class Restable extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'restable';
}
}
\ No newline at end of file
<?php namespace Terranet\Restable;
/**
* Format class
*
* Help convert between various formats such as XML, JSON, CSV, etc.
*
* @author Phil Sturgeon
* @license http://philsturgeon.co.uk/code/dbad-license
*/
class Format
{
// Array to convert
protected $_data = [];
// View filename
protected $_from_type = null;
/**
* Returns an instance of the Format object.
*
* echo $this->format->factory(array('foo' => 'bar'))->to_xml();
*
* @param mixed general date to be converted
* @param string data format the file was provided in
* @return Factory
*/
public static function factory($data, $from_type = null)
{
// Stupid stuff to emulate the "new static()" stuff in this libraries PHP 5.3 equivalent
$class = __CLASS__;
return new $class($data, $from_type);
}
/**
* Do not use this directly, call factory()
*/
public function __construct($data = null, $from_type = null)
{
// If the provided data is already formatted we should probably convert it to an array
if ($from_type !== null)
{
if (method_exists($this, '_from_' . $from_type))
{
$data = call_user_func([$this, '_from_' . $from_type], $data);
}
else
{
throw new Exception('Format class does not support conversion from "' . $from_type . '".');
}
}
$this->_data = $data;
}
// FORMATING OUTPUT ---------------------------------------------------------
public function toArray($data = null)
{
// If not just null, but nothing is provided
if ($data === null and !func_num_args())
{
$data = $this->_data;
}
$array = [];
foreach ((array) $data as $key => $value)
{
if (is_object($value) or is_array($value))
{
$array[$key] = $this->to_array($value);
}
else
{
$array[$key] = $value;
}
}
return $array;
}
// Format XML for output
public function toXML($data = null, $structure = null, $basenode = 'result')
{
if ($data === null and !func_num_args())
{
$data = $this->_data;
}
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set('zend.ze1_compatibility_mode', 0);
}
if ($structure === null)
{
$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
}
// Force it to be something useful
if (!is_array($data) AND !is_object($data))
{
$data = (array) $data;
}
foreach ($data as $key => $value)
{
//change false/true to 0/1
if (is_bool($value))
{
$value = (int) $value;
}
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = (\Str::singular($basenode) != $basenode) ? \Str::singular($basenode) : 'item';
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
// if there is another array found recursively call this function
if (is_array($value) or is_object($value))
{
$node = $structure->addChild($key);
// recursive call.
$this->toXML($value, $node, $key);
}
else
{
// add single node.
$value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");
$structure->addChild($key, $value);
}
}
return $structure->asXML();
}
// Format CSV for output
public function toCSV()
{
$data = $this->_data;
// Multi-dimensional array
if (isset($data[0]) && is_array($data[0]))
{
$headings = array_keys($data[0]);
}
// Single array
else
{
$headings = array_keys($data);
$data = [$data];
}
$output = '"' . implode('","', $headings) . '"' . PHP_EOL;
foreach ($data as &$row)
{
$row = str_replace('"', '""', $row); // Escape dbl quotes per RFC 4180
$output .= '"' . implode('","', $row) . '"' . PHP_EOL;
}
return $output;
}
// Encode as JSON
public function toJson()
{
return json_encode($this->_data);
}
// Encode as Serialized array
public function toSerialized()
{
return serialize($this->_data);
}
// Output as a string representing the PHP structure
public function toPHP()
{
return var_export($this->_data, true);
}
// Format XML for output
protected function _from_xml($string)
{
return $string ? (array) simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : [];
}
// Format CSV for output
// This function is DODGY! Not perfect CSV support but works with my REST_Controller
protected function _from_csv($string)
{
$data = [];
// Splits
$rows = explode("\n", trim($string));
$headings = explode(',', array_shift($rows));
foreach ($rows as $row)
{
// The substr removes " from start and end
$data_fields = explode('","', trim(substr($row, 1, -1)));
if (count($data_fields) == count($headings))
{
$data[] = array_combine($headings, $data_fields);
}
}
return $data;
}
// Encode as JSON
private function _from_json($string)
{
return json_decode(trim($string));
}
// Encode as Serialized array
private function _from_serialize($string)
{
return unserialize(trim($string));
}
}
\ No newline at end of file
<?php namespace Terranet\Restable;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\MessageBag;
use Illuminate\Contracts\Support\MessageProvider AS MessageProviderInterface;
use Illuminate\Support\Facades\Response as LaravelResponse;
class Restable implements Contracts\Restable
{
/**
* Repository config.
*
* @var \Illuminate\Config\Repository
*/
protected $config;
/**
* Format to response.
*
* @var string
*/
protected $format = 'json';
/**
* Laravel response.
*
* @var \Illuminate\Support\Facades\Response
*/
protected $response;
/**
* Converter.
*
* @var \Teepluss\Restable\Format
*/
protected $converter;
/**
* Code number for handle error.
*
* @var integer
*/
protected $code;
/**
* Config codes.
*
* @var array
*/
protected $codes = [];
/**
* API responsed.
*
* @var mixed
*/
protected $returned;
/**
* Constructor.
*
* @param \Illuminate\Config\Repository $config
* @param ResponseFactory $response
* @param \Teepluss\Restable\Format|Format $converter
*/
public function __construct(Repository $config, ResponseFactory $response, Format $converter)
{
$this->config = $config;
$this->response = $response;
$this->converter = $converter;
// Response format.
$this->codes = $this->config->get('restable');
}
/**
* Set default response format.
*
* @param string $format
*/
public function setDefaultFormat($format)
{
$this->format = $format;
}
/**
* Making response.
*
* @param array $data
* @param string $type
* @return \Teepluss\Restable\Restable
*/
protected function make($data, $type)
{
$returned = $this->codes[$type];
// Change object to be array.
if (is_object($data))
{
$data = $data->toArray();
}
// Finding mark point.
if (is_array($returned['response']))
{
array_walk_recursive($returned['response'], function (&$v) use ($data)
{
if (preg_match('/:response/', $v))
{
$v = (!is_null($data) or is_array($data)) ? $data : preg_replace('/:response\|?/', '', $v);
}
});
}
else
{
$returned['response'] = $data;
}
$this->returned = $returned;
return $this;
}
/**
* Change code number.
*
* @param integer $code
* @return \Teepluss\Restable\Restable
*/
public function code($code)
{
$this->code = $code;
return $this;
}
/**
* Response listing.
*
* @param array $messages
* @return string
*/
public function listing($messages)
{
$content = $messages;
return $this->make($content, 'listing');
}
/**
* Response single.
*
* @param array $messages
* @return string
*/
public function single($messages)
{
$content = $messages;
return $this->make($content, 'show');
}
/**
* Response created.
*
* @param array $messages
* @return string
*/
public function created($messages)
{
$content = $messages;
return $this->make($content, 'created');
}
/**
* Response updated.
*
* @param array $messages
* @return string
*/
public function updated($messages)
{
$content = $messages;
return $this->make($content, 'updated');
}
/**
* Response deleted.
*
* @return string
*/
public function deleted()
{
return $this->make(null, 'deleted');
}
/**
* Simple response success.
*
* @param mixed $message
* @return string
*/
public function success($message)
{
$content = $message;
return $this->make($content, 'success');
}
/**
* Response error.
*
* @param array $messages
* @return string
*/
public function error($messages, $type = 400)
{
$alias = 'error_' . $type;
return $this->$alias($messages);
}
/**
* Unauthorized.
*
* @param mixed $description
* @return string
*/
public function unauthorized($description = null)
{
return $this->error($description, 401);
}
/**
* Any error return 400 as bad request.
*
* @param mixed $description
* @return string
*/
public function bad($description = null)
{
return $this->error($description, 400);
}
/**
* Alias of error 404 response.
*
* @param null $description
* @return string
*/
public function missing($description = null)
{
return $this->error($description, 404);
}
/**
* Alias of error 422 response.
*
* @param array $errors
* @return string
*/
public function unprocess($errors)
{
return $this->error($errors, 422);
}
/**
* Validation error.
*
* @param array $messages
* @return string
*/
protected function error_422($messages)
{
// Get message bag from validator.
if ($messages instanceof MessageProviderInterface)
{
$messages = $messages->getMessageBag();
}
// Get validation message bag.
if (!$messages instanceOf MessageBag)
{
$messages = new MessageBag($messages);
}
$content = [];
// Re-format error messages.
foreach ($messages->getMessages() as $field => $message)
{
$error = [
'field' => $field,
'message' => current($message)
];
array_push($content, $error);
}
return $this->make($content, 'error_422');
}
/**
* Render response with format.
*
* @param string $format
* @return mixed
*/
public function render($format = null, $callback = null)
{
$format = ($format) ?: $this->format;
$returned = $this->returned;
// Change error code number.
if ($this->code and isset($returned['response']['code']))
{
$returned['response']['code'] = $this->code;
}
switch ($format)
{
case 'xml' :
if (isset($returned['response']['data']))
{
$returned['response']['entries'] = $returned['response']['data'];
unset($returned['response']['data']);
}
$data = [
'type' => 'application/xml',
'content' => $this->converter->factory($returned['response'])->toXML()
];
break;
case 'php' :
$data = [
'type' => 'text/plain',
'content' => $this->converter->factory($returned['response'])->toPHP()
];
break;
case 'serialized' :
$data = [
'type' => 'text/plain',
'content' => $this->converter->factory($returned['response'])->toSerialized()
];
break;
// In case JSON we don't need to convert anything.
case 'json' :
default :
$response = $this->response->json($returned['response'], $returned['header']);
// Support JSONP Response.
if ($callback)
{
$response = $response->setCallback($callback);
}
return $response;
break;
}
// Making response.
$response = $this->response->make($data['content'], $returned['header']);
//$response = response()
$response->header('Content-Type', $data['type']);
return $response;
}
/**
* Alias of render.
*
* @param string $format
* @return mixed
*/
public function to($format, $callback = null)
{
return $this->render($format, $callback);
}
/**
* Response any error types.
*
* @param string $method
* @param array $arguments
* @return string
*/
public function __call($method, $arguments = [])
{
// Make error response.
if (preg_match('/^error_/', $method))
{
$arguments[] = $method;
return call_user_func_array([$this, 'make'], $arguments);
}
// Return response with specific format.
if (preg_match('/^to(.*)$/', $method, $matches))
{
$format = strtolower($matches[1]);
$callback = $arguments[0];
if (in_array($format, ['json', 'xml', 'php', 'serialized']))
{
return $this->to($format, $callback);
}
}
}
}
\ No newline at end of file
<?php namespace Terranet\Restable;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\ResponseFactory;
class RestableServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$configPath = __DIR__ . '/../config/restable.php';
// Publish config.
$this->publishes([$configPath => config_path('restable.php')], 'config');
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$configPath = __DIR__ . '/../config/restable.php';
// Merge config to allow user overwrite.
$this->mergeConfigFrom($configPath, 'restable');
$this->app['restable'] = $this->app->share(function ($app)
{
$response = new ResponseFactory($app['view'], $app['redirect']);
$converter = new Format;
return new SelfRenderedAdapter(new Restable($app['config'], $response, $converter));
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['restable'];
}
}
\ No newline at end of file
<?php namespace Terranet\Restable;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Support\MessageBag;
use Illuminate\Contracts\Support\MessageProvider AS MessageProviderInterface;
use Illuminate\Support\Facades\Response as LaravelResponse;
class SelfRenderedAdapter implements Contracts\SelfRendered
{
/**
* @var Contracts\Restable
*/
private $restable;
/**
* @param Contracts\Restable $restable
*/
public function __construct(Contracts\Restable $restable)
{
$this->restable = $restable;
}
/**
* Response listing.
*
* @param array $messages
* @return string
*/
public function listing($messages)
{
return $this->restable->listing($messages)->render();
}
/**
* Response single.
*
* @param array $messages
* @return string
*/
public function single($messages)
{
return $this->restable->single($messages)->render();
}
/**
* Response created.
*
* @param array $messages
* @return string
*/
public function created($messages)
{
return $this->restable->created($messages)->render();
}
/**
* Response updated.
*
* @param array $messages
* @return string
*/
public function updated($messages)
{
return $this->restable->updated($messages)->render();
}
/**
* Response deleted.
*
* @return string
*/
public function deleted()
{
return $this->restable->deleted()->render();
}
/**
* Simple response success.
*
* @param mixed $message
* @return string
*/
public function success($message)
{
return $this->restable->success($message)->render();
}
/**
* Unauthorized.
*
* @param mixed $description
* @return string
*/
public function unauthorized($description = null)
{
return $this->restable->unauthorized($description)->render();
}
/**
* Any error return 400 as bad request.
*
* @param mixed $description
* @return string
*/
public function bad($description = null)
{
return $this->restable->bad($description)->render();
}
/**
* Alias of error 404 response.
*
* @param array $description
* @return string
*/
public function missing($description = null)
{
return $this->restable->missing($description)->render();
}
/**
* Alias of error 422 response.
*
* @param array $errors
* @return string
*/
public function unprocess($errors)
{
return $this->restable->unprocess($errors)->render();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment