diff --git a/src/Model/Behavior/JsonableBehavior.php b/src/Model/Behavior/JsonableBehavior.php index 852babdc4..0d85a2d57 100644 --- a/src/Model/Behavior/JsonableBehavior.php +++ b/src/Model/Behavior/JsonableBehavior.php @@ -58,7 +58,7 @@ class JsonableBehavior extends Behavior { 'depth' => 512, ], 'decodeParams' => [ // params for json_decode - 'assoc' => false, // useful when working with multidimensional arrays + 'assoc' => true, // useful when working with multidimensional arrays 'depth' => 512, 'options' => 0 ] @@ -185,6 +185,7 @@ public function _encode($val) { } } } + if (is_array($val)) { // Depth param added in PHP 5.5 if (version_compare(PHP_VERSION, '5.5.0', '>=')) { @@ -193,6 +194,7 @@ public function _encode($val) { $val = json_encode($val, $this->_config['encodeParams']['options']); } } + return $val; } @@ -209,7 +211,9 @@ public function _decode($val) { if ($decoded === false) { return false; } - $decoded = (array)$decoded; + if ($this->_config['decodeParams']['assoc']) { + $decoded = (array)$decoded; + } if ($this->_config['output'] === 'param') { $decoded = $this->_toParam($decoded); } elseif ($this->_config['output'] === 'list') { diff --git a/src/Model/Behavior/SluggedBehavior.php b/src/Model/Behavior/SluggedBehavior.php index 43a3ad703..a7d1551f9 100644 --- a/src/Model/Behavior/SluggedBehavior.php +++ b/src/Model/Behavior/SluggedBehavior.php @@ -236,6 +236,8 @@ public function needsSlugUpdate($entity, $deep = false) { * @return void */ public function _slug(Entity $entity) { + extract($this->_config); + $overwrite = $this->config['overwrite']; if (!$overwrite && !$entity->get($overwriteField)) { $overwrite = true; diff --git a/src/Utility/Number.php b/src/Utility/Number.php index da593fc79..761b53db1 100644 --- a/src/Utility/Number.php +++ b/src/Utility/Number.php @@ -108,6 +108,17 @@ public static function _format($number, array $formatOptions = []) { return $sign . parent::format($number, $options); } + /** + * Format + * + * Additional options + * - signed + * - positive + * + * @param $number + * @param array $options + * @return string + */ public static function format($number, array $options = []) { $defaults = [ 'positive' => '+', 'signed' => false diff --git a/tests/TestApp/Model/Table/JsonableCommentsTable.php b/tests/TestApp/Model/Table/JsonableCommentsTable.php new file mode 100644 index 000000000..ffb0db99a --- /dev/null +++ b/tests/TestApp/Model/Table/JsonableCommentsTable.php @@ -0,0 +1,12 @@ +Comments = TableRegistry::get('JsonableComments'); $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]); } @@ -202,11 +204,11 @@ public function testEncodeParams() { $expected = []; $this->assertEquals($expected, $res['details']); - $this->skipIf(true, 'FIXME!'); + $this->Comments->truncate(); // Test encode depth = 2 $this->Comments->removeBehavior('Jsonable'); - $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2]]); + $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2], 'decodeParams' => ['assoc' => false]]); $data = [ 'comment' => 'blabla', @@ -218,10 +220,52 @@ public function testEncodeParams() { $this->Comments->save($entity); $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); - debug($res); ob_flush(); $obj = new \stdClass(); - $obj->y = 'z'; - $expected = ['x' => $obj]; + $obj->x = new \stdClass(); + $obj->x->y = 'z'; + $expected = $obj; + $this->assertEquals($expected, $res['details']); + } + + public function testEncodeParamsAssocFalse() { + // $depth param added in 5.5.0 + $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>=')); + + // Test encode depth = 1 + $this->Comments->removeBehavior('Jsonable'); + $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]); + + $data = [ + 'comment' => 'blabla', + 'url' => 'www.dereuromark.de', + 'title' => 'param', + 'details' => ['y' => 'yy'], + ]; + $entity = $this->Comments->newEntity($data); + $this->Comments->save($entity); + + $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); + $obj = new \stdClass(); + $obj->y = 'yy'; + $expected = $obj; + $this->assertEquals($expected, $res['details']); + + $this->Comments->truncate(); + + $this->Comments->removeBehavior('Jsonable'); + $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]); + + $data = [ + 'comment' => 'blabla', + 'url' => 'www.dereuromark.de', + 'title' => 'param', + 'details' => ['y' => ['yy' => 'yyy']], + ]; + $entity = $this->Comments->newEntity($data); + $this->Comments->save($entity); + + $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); + $expected = null; $this->assertEquals($expected, $res['details']); } @@ -232,7 +276,7 @@ public function testEncodeParams() { */ public function testDecodeParams() { $this->Comments->removeBehavior('Jsonable'); - $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details']]); + $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc'=> false]]); $data = [ 'comment' => 'blabla', @@ -246,8 +290,9 @@ public function testDecodeParams() { // Test decode with default params $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); $obj = new \stdClass(); - $obj->y = 'z'; - $expected = ['x' => $obj]; + $obj->x = new \stdClass(); + $obj->x->y = 'z'; + $expected = $obj; $this->assertEquals($expected, $res['details']); // Test decode with assoc = true