Browse Source

Added support of JsonSerializable interface to ArrayableTrait (#17888)

tags/2.0.33
Ivan Hermanov 5 years ago committed by GitHub
parent
commit
6e9764b467
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      framework/base/ArrayableTrait.php
  2. 49
      tests/framework/rest/SerializerTest.php

8
framework/base/ArrayableTrait.php

@ -128,12 +128,16 @@ trait ArrayableTrait
if ($recursive) { if ($recursive) {
$nestedFields = $this->extractFieldsFor($fields, $field); $nestedFields = $this->extractFieldsFor($fields, $field);
$nestedExpand = $this->extractFieldsFor($expand, $field); $nestedExpand = $this->extractFieldsFor($expand, $field);
if ($attribute instanceof Arrayable) { if ($attribute instanceof \JsonSerializable) {
$attribute = $attribute->jsonSerialize();
} elseif ($attribute instanceof Arrayable) {
$attribute = $attribute->toArray($nestedFields, $nestedExpand); $attribute = $attribute->toArray($nestedFields, $nestedExpand);
} elseif (is_array($attribute)) { } elseif (is_array($attribute)) {
$attribute = array_map( $attribute = array_map(
function ($item) use ($nestedFields, $nestedExpand) { function ($item) use ($nestedFields, $nestedExpand) {
if ($item instanceof Arrayable) { if ($item instanceof \JsonSerializable) {
return $item->jsonSerialize();
} elseif ($item instanceof Arrayable) {
return $item->toArray($nestedFields, $nestedExpand); return $item->toArray($nestedFields, $nestedExpand);
} }
return $item; return $item;

49
tests/framework/rest/SerializerTest.php

@ -425,6 +425,23 @@ class SerializerTest extends TestCase
$this->assertEquals(['customField' => 'test3/test4'], $serializer->serialize($model)); $this->assertEquals(['customField' => 'test3/test4'], $serializer->serialize($model));
} }
/**
* @see https://github.com/yiisoft/yii2/issues/16334
*/
public function testSerializeArrayableWithJsonSerializableAttribute()
{
$serializer = new Serializer();
$model = new TestModel4();
$this->assertEquals([
'field3' => 'test3',
'field4' => 'test4',
'testModel3' => ['customField' => 'test3/test4'],
'testModelArray' => [['customField' => 'test3/test4']],
],
$serializer->serialize($model));
}
} }
class TestModel extends Model class TestModel extends Model
@ -495,3 +512,35 @@ class TestModel3 extends Model implements \JsonSerializable
]; ];
} }
} }
class TestModel4 extends Model
{
public static $fields = ['field3', 'field4'];
public static $extraFields = [];
public $field3 = 'test3';
public $field4 = 'test4';
public $extraField4 = 'testExtra2';
public function fields()
{
$fields = static::$fields;
$fields['testModel3'] = function() {
return $this->getTestModel3();
};
$fields['testModelArray'] = function() {
return [$this->getTestModel3()];
};
return $fields;
}
public function extraFields()
{
return static::$extraFields;
}
public function getTestModel3()
{
return new TestModel3();
}
}

Loading…
Cancel
Save