2017 © Pedro Peláez
 

library responder

Simple PHP Responder Utility Library for API Development

image

nathanmac/responder

Simple PHP Responder Utility Library for API Development

  • Wednesday, July 27, 2016
  • by nathanmac
  • Repository
  • 2 Watchers
  • 1 Stars
  • 125 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 1 Open issues
  • 6 Versions
  • 15 % Grown

The README.md

Responder

Build Status License Code Climate Coverage Status Latest Stable Version SensioLabsInsight, (*1)

Simple PHP Responder Utility Library for API Development, (*2)

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require Nathanmac/Responder., (*3)

"require": {
    "Nathanmac/Responder": "2.*"
}

Next, update Composer from the Terminal:, (*4)

composer update

Laravel Users

If you are a Laravel user, then there is a service provider that you can make use of to automatically prepare the bindings and such., (*5)


// app/config/app.php 'providers' => [ '...', 'Nathanmac\Utilities\Responder\ResponderServiceProvider' ];

When this provider is booted, you'll have access to a helpful Responder facade, which you may use in your controllers., (*6)

public function index()
{
    Responder::json($payload);         // Array > JSON
    Responder::xml($payload);          // Array > XML
    Responder::yaml($payload);         // Array > YAML
    Responder::querystr($payload);     // Array > Query String
    Responder::serialize($payload);    // Array > Serialized Object
    Responder::bson($payload);         // Array > BSON
    Responder::msgpack($payload);      // Array > MessagePack
}

Responder Functions

$responder->json($payload);         // Array > JSON
$responder->xml($payload);          // Array > XML
$responder->yaml($payload);         // Array > YAML
$responder->querystr($payload);     // Array > Query String
$responder->serialize($payload);    // Array > Serialized Object
$responder->bson($payload);         // Array > BSON
$responder->msgpack($payload);      // Array > MessagePack

Respond with Automatic Detection

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header("Content-Type: {$responder->getContentType()}");
print $responder->payload($body);

Respond with JSON

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/json');
print $responder->json($body);

Respond with XML

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/xml; charset=utf-8');
print $responder->xml($body);

Respond with Query String

$responder = new Responder();

$body = array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
);

header('Content-Type: application/x-www-form-urlencoded');
print $responder->querystr($body);

Respond with Serialized Object

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/vnd.php.serialized');
print $responder->serialize($body);

Respond with YAML

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/x-yaml');
print $responder->yaml($body);

Respond with BSON

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/bson');
print $responder->bson($body);

Respond with MessagePack

$responder = new Responder();

$body = array(
    'message' => array(
        'to' => 'Jack Smith',
        'from' => 'Jane Doe',
        'subject' => 'Hello World',
        'body' => 'Hello, whats going on...'
    )
);

header('Content-Type: application/x-msgpack');
print $responder->msgpack($body);

Custom Responders/Formatters

You can make your own custom responders/formatters by implementing FormatInterface, the below example demostrates the use of a custom responder/formatter., (*7)

use Nathanmac\Utilities\Responder\Formats\FormatInterface;

/**
 * Custom Formatter
 */

class CustomFormatter implements FormatInterface {
    /**
     * Generate Payload Data
     *
     * @param array $payload
     *
     * @return string
     *
     * @throws ResponderException
     */
    public function generate($payload)
    {
        $payload; // Raw payload array

        $output = // Process raw payload array to formatted data

        return $output; // return data string
    }
}
Using the CustomFormatter
use Acme\Formatters\CustomFormatter;

$responder = new Responder();
$generated = $responder->generate(['raw' => 'payload', 'data'], new CustomFormatter());
Register the CustomFormatter
use Acme\Formatters\CustomFormatter;

$responder = new Responder();
$responder->registerFormat('application/x-custom-format', 'Acme\Formatters\CustomFormatter');
$responder->payload('application/x-custom-format');

Testing

To test the library itself, run the PHPUnit tests:, (*8)

phpunit tests/

The Versions

27/07 2016

dev-master

9999999-dev https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api json xml formatter responder

27/07 2016

v2.1

2.1.0.0 https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api json xml formatter responder

27/10 2014

v2.0

2.0.0.0 https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

by Nathan Macnamara

api json xml formatter responder

04/08 2014

v1.2

1.2.0.0 https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api json xml formatter responder

05/06 2014

v1.1

1.1.0.0 https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api json xml formatter responder

04/06 2014

v1.0

1.0.0.0 https://github.com/nathanmac/Responder

Simple PHP Responder Utility Library for API Development

  Sources   Download

MIT

The Requires

 

The Development Requires

api json xml formatter responder