2017 © Pedro PelĂĄez
 

library rest-util

Some useful classes for handling certain aspects of a REST(ful) API.

image

tbbc/rest-util

Some useful classes for handling certain aspects of a REST(ful) API.

  • Wednesday, May 28, 2014
  • by tbbc
  • Repository
  • 7 Watchers
  • 16 Stars
  • 45,185 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 8 % Grown

The README.md

The Big Brains Company - Rest Util

Build Status Scrutinizer Quality Score, (*1)

Rest util library for PHP., (*2)

This library implement some useful util classes for resolving recurrent issues in Rest(ful) APIs (i.e: Error handling)., (*3)

Quick Start

Table of contents

  1. Installation
  2. Getting started
  3. Usage
  4. Run the test
  5. Contributing
  6. Requirements
  7. Authors
  8. License

Description

Rest utilitary library for PHP, (*4)

Installation

Using Composer, just $ composer require tbbc/rest-util package or:, (*5)

``` javascript { "require": { "tbbc/rest-util": "dev-master" } }, (*6)


Usage ----- Converting Exception into Error object: 1\. First you have to define an `ExceptionMapping`, for the sake of the example, we use `PHP`, but a `YamlLoader` is also available: ```php // Exception mapping configuration $invalidArgumentExceptionMapping = new ExceptionMapping(array( 'exceptionClassName' => '\InvalidArgumentException', 'factory' => 'default', 'httpStatusCode' => 400, 'errorCode' => 400101, 'errorMessage' => null, 'errorExtendedMessage' => 'Extended message', 'errorMoreInfoUrl' => 'http://api.my.tld/doc/error/400101', ));

2. The ExceptionMapping must be added to ExceptionMap:, (*7)

$exceptionMap = new ExceptionMap();
$exceptionMap->add($invalidArgumentExceptionMapping);

3. We plug the ErrorResolver with an ErrorFactory, a DefaultErrorFactory is bundled within the lib:, (*8)

// Error resolver initialization
$errorResolver       = new ErrorResolver($exceptionMap);
$defaultErrorFactory = new DefaultErrorFactory();
$errorResolver->registerFactory($defaultErrorFactory);

4. Resolve error!, (*9)

$exception = new \InvalidArgumentException('This is an invalid argument exception');
$error     = $errorResolver->resolve($exception);

5. The $error variable is now assigned an ErrorInterface object which implements a toArray() method to allow easy serialization method of your choice:, (*10)

print_r($error->toArray());
/* will output
Array
(
    [http_status_code] => 400
    [code]             => 400101
    [message]          => This is an invalid argument exception.
    [extended_message] => Extended message
    [more_info_url]    => http://api.my.tld/doc/error/400101
)
*/

echo json_encode($error->toArray());
/* And voilĂ ! You get a ready to use json normalized error response body
{
    "http_status_code": 400,
    "code":             400101,
    "message":          "This is an invalid argument exception.",
    "extended_message": "Extended message",
    "more_info_url":    "http:\/\/api.my.tld\/doc\/error\/400101"
}
*/

For more concrete real life usage, take a look at our bundle for integrating the lib into a Symfony application: TbbcRestUtilBundle., (*11)

Run the test

First make sure you have installed all the dependencies, run:, (*12)

$ composer install --dev, (*13)

then, run the test from within the root directory:, (*14)

$ vendor/bin/phpunit, (*15)

Contributing

  1. Take a look at the list of issues.
  2. Fork
  3. Write a test (for either new feature or bug)
  4. Make a PR

Requirements

  • PHP 5.3+

Authors

  • Boris GuĂ©ry - guery.b@gmail.com - http://borisguery.com
  • Benjamin Dulau - benjamin.dulau@gmail.com

License

The Big Brains Company - Rest Util is licensed under the MIT License - see the LICENSE file for details, (*16)

The Big Brains Company - Logo, (*17)

The Versions

28/05 2014

dev-master

9999999-dev

Some useful classes for handling certain aspects of a REST(ful) API.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

api rest restful

28/05 2014

v1.0.0

1.0.0.0

Some useful classes for handling certain aspects of a REST(ful) API.

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

api rest restful