Skip to content

php restful api response implement PSR-7: HTTP message interfaces

Notifications You must be signed in to change notification settings

harryosmar/php-restful-api-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php rest-api response implement PSR-7: HTTP message interfaces

Latest Version Build Status Build Status Scrutinizer Code Quality Code Coverage

Requirements

Features

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

<?php

use PhpRestfulApiResponse\Response;

$response = new Response();

echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 200); //response code 200

response

{
    "status": "created",
    "id": 1
}

Available Response Format

With Array
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->withArray([
    'status' => 'created',
    'id' => 1
], 201); //response code 201

response

{
    "status": "created",
    "id": 1
}
With Item

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
    }
}
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

{
    "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

{
    "error": "something is wrong, please try again"
}
403 Forbidden
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();

response 403

{
    "error":
    {
        "http_code": 403,
        "phrase": "Forbidden"
    }
}
500 Internal Server Error
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorInternalError();

response 500

{
    "error":
    {
        "http_code": 500,
        "phrase": "Internal Server Error"
    }
}
404 Not Found
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorNotFound();

response 404

{
    "error":
    {
        "http_code": 404,
        "phrase": "Not Found"
    }
}
401 Unauthorized
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnauthorized();

response 401

{
    "error":
    {
        "http_code": 401,
        "phrase": "Unauthorized"
    }
}
400 Bad Request
<?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"
        }
    }
}
410 Gone
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorGone();

response 410

{
    "error":
    {
        "http_code": 410,
        "phrase": "Gone"
    }
}
405 Method Not Allowed
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorMethodNotAllowed();

response 405

{
    "error":
    {
        "http_code": 405,
        "phrase": "Method Not Allowed"
    }
}
431 Request Header Fields Too Large
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnwillingToProcess();

response 431

{
    "error":
    {
        "http_code": 431,
        "phrase": "Request Header Fields Too Large"
    }
}
422 Unprocessable Entity
<?php
/** @var \PhpRestfulApiResponse\Response $response */
echo $response->errorUnprocessable();

response 422

{
    "error":
    {
        "http_code": 422,
        "phrase": "Unprocessable Entity"
    }
}

How To Run The Test

composer test

How To Contribute