mirror of
https://we.phorge.it/source/phorge.git
synced 2025-02-07 04:18:31 +01:00
23786784ef
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
40 lines
No EOL
874 B
PHP
40 lines
No EOL
874 B
PHP
<?php
|
|
/**
|
|
* Mime Type: application/json
|
|
* @author Nathan Good <me@nategood.com>
|
|
*/
|
|
|
|
namespace Httpful\Handlers;
|
|
|
|
class JsonHandler extends MimeHandlerAdapter
|
|
{
|
|
private $decode_as_array = false;
|
|
|
|
public function init(array $args)
|
|
{
|
|
$this->decode_as_array = !!(array_key_exists('decode_as_array', $args) ? $args['decode_as_array'] : false);
|
|
}
|
|
|
|
/**
|
|
* @param string $body
|
|
* @return mixed
|
|
*/
|
|
public function parse($body)
|
|
{
|
|
if (empty($body))
|
|
return null;
|
|
$parsed = json_decode($body, $this->decode_as_array);
|
|
if (is_null($parsed))
|
|
throw new \Exception("Unable to parse response as JSON");
|
|
return $parsed;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $payload
|
|
* @return string
|
|
*/
|
|
public function serialize($payload)
|
|
{
|
|
return json_encode($payload);
|
|
}
|
|
} |