From fc54b09d7a95239f7d38afece393b74050a03126 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Thu, 23 Aug 2018 23:33:48 -0700 Subject: [PATCH] Move json encoder under \Curl\Encoder::encodeJson --- src/Curl/Curl.php | 33 +-------------------------------- src/Curl/Encoder.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 src/Curl/Encoder.php diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 9716f7e..95d52d7 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -151,7 +151,7 @@ class Curl $data instanceof \JsonSerializable ) )) { - $data = \Curl\json_encode($data); + $data = \Curl\Encoder::encodeJson($data); } elseif (is_array($data)) { // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch, // CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are @@ -1760,34 +1760,3 @@ function createHeaderCallback($header_callback_data) { return strlen($header); }; } - -/** - * Json Encode - * - * Wrap json_encode() to throw error when the value being encoded fails. - * - * @param $value - * @param $options - * @param $depth - * - * @return string - * @throws \ErrorException - */ -function json_encode($value, $options = 0, $depth = 512) { - // Make compatible with PHP version both before and after 5.5.0. PHP 5.5.0 added the $depth parameter. - $gte_v550 = version_compare(PHP_VERSION, '5.5.0') >= 0; - if ($gte_v550) { - $value = \json_encode($value, $options, $depth); - } else { - $value = \json_encode($value, $options); - } - if (!(json_last_error() === JSON_ERROR_NONE)) { - if (function_exists('json_last_error_msg')) { - $error_message = 'json_encode error: ' . json_last_error_msg(); - } else { - $error_message = 'json_encode error'; - } - throw new \ErrorException($error_message); - } - return $value; -} diff --git a/src/Curl/Encoder.php b/src/Curl/Encoder.php new file mode 100644 index 0000000..67e3e99 --- /dev/null +++ b/src/Curl/Encoder.php @@ -0,0 +1,42 @@ +