mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 22:32:40 +08:00
Added support of JsonSerializable interface to ArrayableTrait (#17888)
This commit is contained in:
@ -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;
|
||||||
|
|||||||
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user