php rest-api response implement PSR-7: HTTP message interfaces
, (*1)
Requirements
- php >= 7.0
- composer https://getcomposer.org/download/
Features
- Implement PSR-7: HTTP message interfaces, extend https://github.com/zendframework/zend-diactoros
- Provides response format collection & item using library http://fractal.thephpleague.com/
- Provides basic errors response
How To Setup
- add this lines to your
composer.json file
{
"require": {
"harryosmar/php-restful-api-response": "^1.1"
}
}
- then run
composer install or composer update
How To Use
Simple example how to use, (*2)
<?php
use PhpRestfulApiResponse\Response;
$response = new Response();
echo $response->withArray([
'status' => 'created',
'id' => 1
], 200); //response code 200
response, (*3)
{
"status": "created",
"id": 1
}
With Array
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withArray([
'status' => 'created',
'id' => 1
], 201); //response code 201
response, (*4)
{
"status": "created",
"id": 1
}
With Item
For this sample, we use class Book as an item, (*5)
<?php
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withItem(
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
201
);
response 201, (*6)
{
"data":
{
"title": "how to be a ninja",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2017,
"price": 100000
}
}
With Collection
<?php
use PhpRestfulApiResponse\Tests\unit\Lib\Book;
/** @var \PhpRestfulApiResponse\Response $response */
$response->withCollection(
[
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a ninja', 100000, 2017),
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a mage', 500000, 2016),
new Book('harry', 'harryosmarsitohang@gmail.com', 'how to be a samurai', 25000, 2000),
],
new \PhpRestfulApiResponse\Tests\unit\Lib\Transformer\Book,
200
);
response 200, (*7)
{
"data": [
{
"title": "how to be a ninja",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2017,
"price": 100000
},
{
"title": "how to be a mage",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2016,
"price": 500000
},
{
"title": "how to be a samurai",
"author":
{
"name": "harry",
"email": "harryosmarsitohang@gmail.com"
},
"year": 2000,
"price": 25000
}]
}
Error
With Error
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withError(['error' => 'something is wrong, please try again'], 500);
response 500, (*8)
{
"error": "something is wrong, please try again"
}
403 Forbidden
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();
response 403, (*9)
{
"error":
{
"http_code": 403,
"phrase": "Forbidden"
}
}
500 Internal Server Error
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorInternalError();
response 500, (*10)
{
"error":
{
"http_code": 500,
"phrase": "Internal Server Error"
}
}
404 Not Found
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();
response 404, (*11)
{
"error":
{
"http_code": 404,
"phrase": "Not Found"
}
}
401 Unauthorized
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnauthorized();
response 401, (*12)
{
"error":
{
"http_code": 401,
"phrase": "Unauthorized"
}
}
400 Bad Request
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorWrongArgs([
'username' => 'required',
'password' => 'required'
]);
response 400, (*13)
{
"error":
{
"http_code": 400,
"phrase": "Bad Request",
"message":
{
"username": "required",
"password": "required"
}
}
}
410 Gone
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorGone();
response 410, (*14)
{
"error":
{
"http_code": 410,
"phrase": "Gone"
}
}
405 Method Not Allowed
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorMethodNotAllowed();
response 405, (*15)
{
"error":
{
"http_code": 405,
"phrase": "Method Not Allowed"
}
}
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnwillingToProcess();
response 431, (*16)
{
"error":
{
"http_code": 431,
"phrase": "Request Header Fields Too Large"
}
}
422 Unprocessable Entity
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnprocessable();
response 422, (*17)
{
"error":
{
"http_code": 422,
"phrase": "Unprocessable Entity"
}
}
How To Run The Test
composer test
How To Contribute
- Fork this repo
- post an issue https://github.com/harryosmar/php-restful-api-response/issues
- create the PR(Pull Request) and wait for the review