Skip to content

Commit

Permalink
[#14528] - Fixed jsonSerializable for collection and json serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Nov 11, 2019
1 parent 8fd26b8 commit ee479e5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
15 changes: 14 additions & 1 deletion phalcon/Collection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,20 @@ class Collection implements
*/
public function jsonSerialize() -> array
{
return this->data;
var key, value;
array records;

let records = [];

for key, value in this->data {
if typeof value == "object" && method_exists(value, "jsonSerialize") {
let records[key] = value->{"jsonSerialize"}();
} else {
let records[key] = value;
}
}

return records;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions phalcon/Storage/Serializer/Json.zep
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Phalcon\Storage\Serializer;

use InvalidArgumentException;
use JsonSerializable;

class Json extends AbstractSerializer
{
Expand All @@ -19,9 +20,9 @@ class Json extends AbstractSerializer
*/
public function serialize() -> string
{
if typeof this->data == "object" {
if typeof this->data == "object" && !(this->data instanceof JsonSerializable) {
throw new InvalidArgumentException(
"Data for JSON serializer cannot be of type object"
"Data for JSON serializer cannot be of type object without implementing JsonSerializable"
);
}

Expand Down
37 changes: 36 additions & 1 deletion tests/unit/Storage/Serializer/Json/SerializeCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

use Codeception\Example;
use InvalidArgumentException;
use Phalcon\Collection;
use Phalcon\Storage\Serializer\Json;
use UnitTester;
use function json_decode;
use function json_encode;

class SerializeCest
{
Expand All @@ -37,6 +40,35 @@ public function storageSerializerJsonSerialize(UnitTester $I, Example $example)
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Storage\Serializer\Json :: serialize() - object
*
* @author Phalcon Team <team@phalcon.io>
* @since 2019-11-11
*/
public function storageSerializerJsonSerializeObject(UnitTester $I)
{
$I->wantToTest('Storage\Serializer\Json - serialize() - object');

$collection1 = new Collection();
$collection1->set('one', 'two');
$collection2 = new Collection();
$collection2->set('three', 'four');
$collection2->set('object', $collection1);

$serializer = new Json($collection2);

$data = [
'three' => 'four',
'object' => [
'one' => 'two',
],
];
$expected = json_encode($data);
$actual = $serializer->serialize();
$I->assertEquals($expected, $actual);
}

/**
* Tests Phalcon\Storage\Serializer\Json :: serialize() - error
*
Expand All @@ -49,7 +81,7 @@ public function storageSerializerJsonSerializeError(UnitTester $I)

$I->expectThrowable(
new InvalidArgumentException(
'Data for JSON serializer cannot be of type object'
'Data for JSON serializer cannot be of type object without implementing JsonSerializable'
),
function () {
$example = new \stdClass();
Expand All @@ -61,6 +93,9 @@ function () {
);
}

/**
* @return array
*/
private function getExamples(): array
{
return [
Expand Down

0 comments on commit ee479e5

Please sign in to comment.