From 9b297f3d877861d2c1de4cb39c762a6d06acee6d Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 24 Dec 2013 13:28:49 -0500 Subject: [PATCH] Dropped Convert::toJSON() In an effort to only maintain compatibility with the latest version of PHP (currently the 5.5 branch) I dropped the sanity checks if `json_encode` was available as it is always available in PHP 5.2+. Dropping this sanity check also allowed me to remove the wrapper function and the `JSON_AVAILABLE` constant. Ideally I'd like to move towards dropping the `Convert` class entirely but will need a way to convert an array to XML as the `RSS` class still leverages it. One thought is to move that code right into the `RSS` class as it never gets used elsewhere because XML is gross. --- classes/Convert.php | 30 ------------------------------ classes/Database/PDO/Common.php | 2 +- classes/Display/JSON.php | 2 +- classes/Display/PHP.php | 2 +- classes/Model.php | 6 +++--- pickles.php | 3 --- tests/classes/ConvertTest.php | 31 ------------------------------- 7 files changed, 6 insertions(+), 70 deletions(-) diff --git a/classes/Convert.php b/classes/Convert.php index 7aaf8b3..9971259 100644 --- a/classes/Convert.php +++ b/classes/Convert.php @@ -117,36 +117,6 @@ class Convert return $xml; } - // }}} - // {{{ To JSON - - /** - * To JSON - * - * Encodes passed variable as JSON. - * - * Requires PHP 5 >= 5.2.0 or PECL json >= 1.2.0 - * - * @link http://json.org/ - * @link http://us.php.net/json_encode - * @link http://pecl.php.net/package/json - * - * @static - * @param mixed $variable variable to convert - * @return JSON encoded string - */ - public static function toJSON($variable) - { - if (JSON_AVAILABLE) - { - return json_encode($variable); - } - else - { - return '{ "status": "error", "message": "json_encode() not found" }'; - } - } - // }}} } diff --git a/classes/Database/PDO/Common.php b/classes/Database/PDO/Common.php index 0c2305d..a75ded8 100644 --- a/classes/Database/PDO/Common.php +++ b/classes/Database/PDO/Common.php @@ -152,7 +152,7 @@ class Database_PDO_Common extends Database_Common if ($input_parameters != null) { - $loggable_query .= ' -- ' . (JSON_AVAILABLE ? json_encode($input_parameters) : serialize($input_parameters)); + $loggable_query .= ' -- ' . json_encode($input_parameters); } Log::query($loggable_query); diff --git a/classes/Display/JSON.php b/classes/Display/JSON.php index 3d616a0..549cca3 100644 --- a/classes/Display/JSON.php +++ b/classes/Display/JSON.php @@ -27,7 +27,7 @@ class Display_JSON extends Display_Common */ public function render() { - echo Convert::toJSON($this->module_return); + echo json_encode($this->module_return); } } diff --git a/classes/Display/PHP.php b/classes/Display/PHP.php index f6f38de..f671751 100644 --- a/classes/Display/PHP.php +++ b/classes/Display/PHP.php @@ -128,7 +128,7 @@ class Display_PHP extends Display_Common } else { - echo Convert::toJSON($this->module_return); + echo json_encode($this->module_return); } } } diff --git a/classes/Model.php b/classes/Model.php index 1db36d0..fee8815 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -1284,7 +1284,7 @@ class Model extends Object if ($field != $this->columns['id']) { $update_fields[] = $field . ' = ?'; - $input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value); + $input_parameters[] = (is_array($value) ? json_encode($value) : $value); } else { @@ -1358,7 +1358,7 @@ class Model extends Object foreach ($record as $variable => $value) { - $input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value); + $input_parameters[] = (is_array($value) ? json_encode($value) : $value); } // @todo Check if the column was passed in @@ -1487,7 +1487,7 @@ class Model extends Object $insert_fields[] = $column; } - $input_parameters[] = (is_array($value) ? (JSON_AVAILABLE ? json_encode($value) : serialize($value)) : $value); + $input_parameters[] = (is_array($value) ? json_encode($value) : $value); } } diff --git a/pickles.php b/pickles.php index f3f03fc..cce0ca0 100644 --- a/pickles.php +++ b/pickles.php @@ -48,9 +48,6 @@ define('DISPLAY_PHP', 'PHP'); define('DISPLAY_RSS', 'RSS'); define('DISPLAY_XML', 'XML'); -// Creates a constant as to whether or not we have JSON available -define('JSON_AVAILABLE', function_exists('json_encode')); - // Creates a variable to flag if we're on the command line define('IS_CLI', !isset($_SERVER['REQUEST_METHOD'])); diff --git a/tests/classes/ConvertTest.php b/tests/classes/ConvertTest.php index 7d724d8..0eebf49 100644 --- a/tests/classes/ConvertTest.php +++ b/tests/classes/ConvertTest.php @@ -1,40 +1,9 @@ assertEquals(Convert::toJSON($a), $b); - } - - public function providerToJSON() - { - $object = (object)'object'; - $object->foo = 'foo'; - $object->bar = 'bar'; - - return array( - array('', '""'), - array('foo', '"foo"'), - array(array('bar'), '["bar"]'), - array(array('foo', 'bar'), '["foo","bar"]'), - array(19810223, '19810223'), - array(array(1981, 02, 23), '[1981,2,23]'), - array(array('foo', 1981), '["foo",1981]'), - array(array('foo', array('bar')), '["foo",["bar"]]'), - array($object, '{"scalar":"object","foo":"foo","bar":"bar"}'), - array(true, 'true'), - array(false, 'false'), - array(null, 'null'), - ); - } - /** * @dataProvider providerArrayToXML */