1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-25 15:00:58 +01:00
phorge-phorge/externals/httpful/src/Httpful/Handlers
epriestley 23786784ef Add Balanced Payments API
Summary: Adds the Balanced PHP API to externals/. Ref T2787.

Test Plan: Used in next diff.

Reviewers: btrahan, chad

Reviewed By: chad

CC: aran, aurelijus

Maniphest Tasks: T2787

Differential Revision: https://secure.phabricator.com/D5764
2013-04-25 09:47:30 -07:00
..
CsvHandler.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00
FormHandler.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00
JsonHandler.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00
MimeHandlerAdapter.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00
README.md Add Balanced Payments API 2013-04-25 09:47:30 -07:00
XHtmlHandler.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00
XmlHandler.php Add Balanced Payments API 2013-04-25 09:47:30 -07:00

Handlers

Handlers are simple classes that are used to parse response bodies and serialize request payloads. All Handlers must extend the MimeHandlerAdapter class and implement two methods: serialize($payload) and parse($response). Let's build a very basic Handler to register for the text/csv mime type.

<?php

class SimpleCsvHandler extends \Httpful\Handlers\MimeHandlerAdapter
{
    /**
     * Takes a response body, and turns it into 
     * a two dimensional array.
     *
     * @param string $body
     * @return mixed
     */
    public function parse($body)
    {
        return str_getcsv($body);
    }

    /**
     * Takes a two dimensional array and turns it
     * into a serialized string to include as the 
     * body of a request
     *
     * @param mixed $payload
     * @return string
     */
    public function serialize($payload)
    {
        $serialized = '';
        foreach ($payload as $line) {
            $serialized .= '"' . implode('","', $line) . '"' . "\n";
        }
        return $serialized;
    }
}

Finally, you must register this handler for a particular mime type.

Httpful::register('text/csv', new SimpleCsvHandler());

After this registering the handler in your source code, by default, any responses with a mime type of text/csv should be parsed by this handler.