From 6e0b217e8568fffcf73a5dcf2625a6a8c363ba48 Mon Sep 17 00:00:00 2001 From: Jake Zatecky Date: Thu, 20 Aug 2015 14:47:47 -0400 Subject: [PATCH] Fix issue with JSON payload always having objects instead of arrays Resolves #1 --- src/WorkEtcClient/HttpfulClient.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/WorkEtcClient/HttpfulClient.php b/src/WorkEtcClient/HttpfulClient.php index 9536028..91145be 100644 --- a/src/WorkEtcClient/HttpfulClient.php +++ b/src/WorkEtcClient/HttpfulClient.php @@ -39,17 +39,36 @@ public function post($endpoint, array $parameters = []) { $request = Request::post($endpoint) ->expects('application/json') - ->body(json_encode($parameters, JSON_FORCE_OBJECT)); + ->body($this->jsonEncode($parameters)); return $this->send($request); } + /** + * Encode the given array to JSON. If the array would not translate to an + * object, force it. + * + * WORK[etc] expects an object, even if that object would be empty. + * + * @param array $array + * + * @return string + */ + protected function jsonEncode(array $array) + { + if (empty($array)) { + return '{}'; + } + + return json_encode($array); + } + /** * Send the request and check for errors. * * @param \Httpful\Request $request * - * @return \Httpful\Response + * @return array */ protected function send(Request $request) {