mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Removed JsBaseEngineHelper::$useNative. Removed json_encode() emulation from JsBaseEngineHelper::object(). Removed unnessecary call to get_object_vars() in JsBaseEngineHelper::object(). Removed unnessecary tests for equalness of emulation and native json_encode(). Added test for correct encoding of objects to JsHelper test case. Fixes #704
This commit is contained in:
parent
1523ff6874
commit
bea666bb12
2 changed files with 13 additions and 117 deletions
|
@ -27,13 +27,6 @@ App::uses('AppHelper', 'View/Helper');
|
|||
* @package cake.view.helpers
|
||||
*/
|
||||
abstract class JsBaseEngineHelper extends AppHelper {
|
||||
/**
|
||||
* Determines whether native JSON extension is used for encoding. Set by object constructor.
|
||||
*
|
||||
* @var boolean
|
||||
* @access public
|
||||
*/
|
||||
public $useNative = false;
|
||||
|
||||
/**
|
||||
* The js snippet for the current selection.
|
||||
|
@ -76,7 +69,6 @@ abstract class JsBaseEngineHelper extends AppHelper {
|
|||
*/
|
||||
function __construct($View, $settings = array()) {
|
||||
parent::__construct($View, $settings);
|
||||
$this->useNative = function_exists('json_encode');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -154,50 +146,7 @@ abstract class JsBaseEngineHelper extends AppHelper {
|
|||
);
|
||||
$options = array_merge($defaultOptions, $options);
|
||||
|
||||
if (is_object($data)) {
|
||||
$data = get_object_vars($data);
|
||||
}
|
||||
|
||||
$out = $keys = array();
|
||||
$numeric = true;
|
||||
|
||||
if ($this->useNative && function_exists('json_encode')) {
|
||||
$rt = json_encode($data);
|
||||
} else {
|
||||
if (is_null($data)) {
|
||||
return 'null';
|
||||
}
|
||||
if (is_bool($data)) {
|
||||
return $data ? 'true' : 'false';
|
||||
}
|
||||
if (is_array($data)) {
|
||||
$keys = array_keys($data);
|
||||
}
|
||||
|
||||
if (!empty($keys)) {
|
||||
$numeric = (array_values($keys) === array_keys(array_values($keys)));
|
||||
}
|
||||
|
||||
foreach ($data as $key => $val) {
|
||||
if (is_array($val) || is_object($val)) {
|
||||
$val = $this->object($val);
|
||||
} else {
|
||||
$val = $this->value($val);
|
||||
}
|
||||
if (!$numeric) {
|
||||
$val = '"' . $this->value($key, false) . '":' . $val;
|
||||
}
|
||||
$out[] = $val;
|
||||
}
|
||||
|
||||
if (!$numeric) {
|
||||
$rt = '{' . join(',', $out) . '}';
|
||||
} else {
|
||||
$rt = '[' . join(',', $out) . ']';
|
||||
}
|
||||
}
|
||||
$rt = $options['prefix'] . $rt . $options['postfix'];
|
||||
return $rt;
|
||||
return $options['prefix'] . json_encode($data) . $options['postfix'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -25,6 +25,12 @@ App::uses('FormHelper', 'View/Helper');
|
|||
App::uses('View', 'View');
|
||||
App::uses('ClassRegistry', 'Utility');
|
||||
|
||||
class JsEncodingObject {
|
||||
protected $_title = 'Old thing';
|
||||
|
||||
private $__noshow = 'Never ever';
|
||||
}
|
||||
|
||||
class OptionEngineHelper extends JsBaseEngineHelper {
|
||||
protected $_optionMap = array(
|
||||
'request' => array(
|
||||
|
@ -797,13 +803,18 @@ class JsBaseEngineTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
function testObject() {
|
||||
$this->JsEngine->useNative = false;
|
||||
|
||||
$object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));
|
||||
$result = $this->JsEngine->object($object);
|
||||
$expected = '{"title":"New thing","indexes":[5,6,7,8]}';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$object = new JsEncodingObject();
|
||||
$object->title = 'New thing';
|
||||
$object->indexes = array(5,6,7,8);
|
||||
$result = $this->JsEngine->object($object);
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $this->JsEngine->object(array('default' => 0));
|
||||
$expected = '{"default":0}';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
@ -842,70 +853,6 @@ class JsBaseEngineTest extends CakeTestCase {
|
|||
$this->assertNoPattern('/.POSTFIX./', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test compatibility of JsBaseEngineHelper::object() vs. json_encode()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testObjectAgainstJsonEncode() {
|
||||
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
|
||||
if ($skip) {
|
||||
return;
|
||||
}
|
||||
$this->JsEngine->useNative = false;
|
||||
$data = array();
|
||||
$data['mystring'] = "simple string";
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
$data['mystring'] = "strïng with spécial chârs";
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
$data['mystring'] = "a two lines\nstring";
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
$data['mystring'] = "a \t tabbed \t string";
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
$data['mystring'] = "a \"double-quoted\" string";
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
$data['mystring'] = 'a \\"double-quoted\\" string';
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
unset($data['mystring']);
|
||||
$data[3] = array(1, 2, 3);
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
|
||||
unset($data[3]);
|
||||
$data = array('mystring' => null, 'bool' => false, 'array' => array(1, 44, 66));
|
||||
$this->assertEqual(json_encode($data), $this->JsEngine->object($data));
|
||||
}
|
||||
|
||||
/**
|
||||
* test that JSON made with JsBaseEngineHelper::object() against json_decode()
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testObjectAgainstJsonDecode() {
|
||||
$skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s');
|
||||
if ($skip) {
|
||||
return;
|
||||
}
|
||||
$this->JsEngine->useNative = false;
|
||||
|
||||
$data = array("simple string");
|
||||
$result = $this->JsEngine->object($data);
|
||||
$this->assertEqual(json_decode($result), $data);
|
||||
|
||||
$data = array('my "string"');
|
||||
$result = $this->JsEngine->object($data);
|
||||
$this->assertEqual(json_decode($result), $data);
|
||||
|
||||
$data = array('my \\"string\\"');
|
||||
$result = $this->JsEngine->object($data);
|
||||
$this->assertEqual(json_decode($result), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* test Mapping of options.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue