2017 © Pedro Peláez
 

library php-restful-api-response

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

image

harryosmar/php-restful-api-response

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

  • Sunday, March 11, 2018
  • by harryosmar
  • Repository
  • 2 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

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

Latest Version Build Status Build Status Scrutinizer Code Quality Code Coverage, (*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
}

Available Response Format

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"
    }
}
431 Request Header Fields Too Large
<?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

The Versions

11/03 2018

dev-master

9999999-dev

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

11/03 2018

v1.1.3

1.1.3.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

11/03 2018

dev-json-encode

dev-json-encode

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

09/03 2018

v1.1.2

1.1.2.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

08/03 2018

v1.1.1

1.1.1.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

08/03 2018

v1.1.0

1.1.0.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

18/02 2018

v1.0.4

1.0.4.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

18/02 2018

v1.0.3

1.0.3.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

15/02 2018

v1.0.2

1.0.2.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

14/02 2018

v1.0.1

1.0.1.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang

14/02 2018

v1.0.0

1.0.0.0

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

  Sources   Download

The Requires

 

The Development Requires

by Harry Sitohang