Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/dereuromark/cakephp-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Jul 31, 2015
2 parents 90ca62a + d936c9e commit 971ff86
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/Model/Behavior/JsonableBehavior.php
Expand Up @@ -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
]
Expand Down Expand Up @@ -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', '>=')) {
Expand All @@ -193,6 +194,7 @@ public function _encode($val) {
$val = json_encode($val, $this->_config['encodeParams']['options']);
}
}

return $val;
}

Expand All @@ -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') {
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Behavior/SluggedBehavior.php
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/Utility/Number.php
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/TestApp/Model/Table/JsonableCommentsTable.php
@@ -0,0 +1,12 @@
<?php

namespace TestApp\Model\Table;

use Tools\Model\Table\Table;

class JsonableCommentsTable extends Table {

public $validate = [
];

}
61 changes: 53 additions & 8 deletions tests/TestCase/Model/Behavior/JsonableBehaviorTest.php
Expand Up @@ -18,6 +18,8 @@ class JsonableBehaviorTest extends TestCase {
public function setUp() {
parent::setUp();

Configure::write('App.namespace', 'TestApp');

$this->Comments = TableRegistry::get('JsonableComments');
$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
}
Expand Down Expand Up @@ -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',
Expand All @@ -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']);
}

Expand All @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit 971ff86

Please sign in to comment.