Simple PHP Responder Utility Library for API Development
Begin by installing this package through Composer. Edit your project's composer.json
file to require Nathanmac/Responder
.
"require": {
"Nathanmac/Responder": "2.*"
}
Next, update Composer from the Terminal:
composer update
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.
// 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.
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->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 = 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);
$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);
$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);
$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);
$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);
$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);
$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);
$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);
You can make your own custom responders/formatters by implementing FormatInterface, the below example demostrates the use of a custom responder/formatter.
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
}
}
use Acme\Formatters\CustomFormatter;
$responder = new Responder();
$generated = $responder->generate(['raw' => 'payload', 'data'], new CustomFormatter());
use Acme\Formatters\CustomFormatter;
$responder = new Responder();
$responder->registerFormat('application/x-custom-format', 'Acme\Formatters\CustomFormatter');
$responder->payload('application/x-custom-format');
To test the library itself, run the PHPUnit tests:
phpunit tests/