- php >= 7.0
- composer https://getcomposer.org/download/
- 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
- add this lines to your composer.jsonfile
{
    "require": {
        "harryosmar/php-restful-api-response": "^1.1"
    }
}
- then run composer installorcomposer update
Simple example how to use
<?php
use PhpRestfulApiResponse\Response;
$response = new Response();
echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 200); //response code 200
response
{
    "status": "created",
    "id": 1
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 201); //response code 201response
{
    "status": "created",
    "id": 1
}For this sample, we use class Book as an item
<?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
{
    "data":
    {
        "title": "how to be a ninja",
        "author":
        {
            "name": "harry",
            "email": "harryosmarsitohang@gmail.com"
        },
        "year": 2017,
        "price": 100000
    }
}<?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
{
    "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
    }]
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withError(['error' => 'something is wrong, please try again'], 500);response 500
{
    "error": "something is wrong, please try again"
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();response 403
{
    "error":
    {
        "http_code": 403,
        "phrase": "Forbidden"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorInternalError();response 500
{
    "error":
    {
        "http_code": 500,
        "phrase": "Internal Server Error"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();response 404
{
    "error":
    {
        "http_code": 404,
        "phrase": "Not Found"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnauthorized();response 401
{
    "error":
    {
        "http_code": 401,
        "phrase": "Unauthorized"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorWrongArgs([
   'username' => 'required',
   'password' => 'required'
]);response 400
{
    "error":
    {
        "http_code": 400,
        "phrase": "Bad Request",
        "message":
        {
            "username": "required",
            "password": "required"
        }
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorGone();response 410
{
    "error":
    {
        "http_code": 410,
        "phrase": "Gone"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorMethodNotAllowed();response 405
{
    "error":
    {
        "http_code": 405,
        "phrase": "Method Not Allowed"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnwillingToProcess();response 431
{
    "error":
    {
        "http_code": 431,
        "phrase": "Request Header Fields Too Large"
    }
}<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnprocessable();response 422
{
    "error":
    {
        "http_code": 422,
        "phrase": "Unprocessable Entity"
    }
}composer test
- 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


