From dbed48840af37807b33ffa9cf309a43b607b92f5 Mon Sep 17 00:00:00 2001 From: TheNorthMemory Date: Wed, 20 May 2015 12:35:04 +0800 Subject: [PATCH] implement JSON::encode for PHP5.3 & Overtrue\Wechat --- src/Wechat/Utils/JSON.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Wechat/Utils/JSON.php b/src/Wechat/Utils/JSON.php index bdcd5b2a1..7c11d4fb0 100644 --- a/src/Wechat/Utils/JSON.php +++ b/src/Wechat/Utils/JSON.php @@ -14,11 +14,19 @@ namespace Overtrue\Wechat\Utils; +// for PHP5.3, prevent PHP Notice msg assumed +defined('JSON_UNESCAPED_UNICODE') || define('JSON_UNESCAPED_UNICODE', 256); + /** * Unicode2multi characters supported for the wechat server */ class JSON { + /** + * To prevent new operation, under static usage only + */ + protected function __construct() + {} /** * PHP >= 5.3 JSON_UNESCAPED_UNICODE constant supported @@ -33,21 +41,26 @@ class JSON */ public static function encode($value, $options = 0, $depth = 512) { - $depthSupported = version_compare(PHP_VERSION, '5.5.0', '>='); + // multi-characters supported by default + $options |= JSON_UNESCAPED_UNICODE; - $data = $depthSupported ? json_encode($value, $options, $depth) : json_encode($value, $options); + $data = version_compare(PHP_VERSION, '5.5.0', '>=') + ? json_encode($value, $options, $depth) + : json_encode($value, $options); if (JSON_ERROR_NONE !== json_last_error()) { return $data; } - return $depthSupported ? $data : preg_replace_callback("/\\\u([\w]{2})([\w]{2})/iu", function ($pipe) { + return version_compare(PHP_VERSION, '5.4.0', '>=') + ? $data + : preg_replace_callback("/\\\u([0-9a-f]{2})([0-9a-f]{2})/iu", function ($pipe) { return iconv( strncasecmp(PHP_OS, 'WIN', 3) ? 'UCS-2BE' : 'UCS-2', 'UTF-8', chr(hexdec($pipe[1])) . chr(hexdec($pipe[2])) ); - }, $data); + }, $data); } /**