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.
This commit is contained in:
parent
0cbc1df48f
commit
9b297f3d87
7 changed files with 6 additions and 70 deletions
|
@ -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" }';
|
||||
}
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -128,7 +128,7 @@ class Display_PHP extends Display_Common
|
|||
}
|
||||
else
|
||||
{
|
||||
echo Convert::toJSON($this->module_return);
|
||||
echo json_encode($this->module_return);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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']));
|
||||
|
||||
|
|
|
@ -1,40 +1,9 @@
|
|||
<?php
|
||||
|
||||
require_once 'classes/Convert.php';
|
||||
define('JSON_AVAILABLE', true);
|
||||
|
||||
class ConvertTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider providerToJSON
|
||||
*/
|
||||
public function testToJSON($a, $b)
|
||||
{
|
||||
$this->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
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue