harryosmar/php-restful-api-response

There is no license information available for the latest version (v1.1.4) of this package.

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

v1.1.4 2019-01-22 23:37 UTC

This package is not auto-updated.

Last update: 2024-04-28 02:27:13 UTC


README

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