Symfony 序列化程序组件 AbstractNormalizer::CALLBACKS 反规范化
Symfony Serializer Component AbstractNormalizer::CALLBACKS denormalize
我正在尝试将序列化与回调一起使用,如 https://symfony.com/doc/current/components/serializer.html#using-callbacks-to-serialize-properties-with-object-instances 中那样。但它似乎没有进入回调。我可以忽略带有 'IGNORED_ATTRIBUTES' 的属性就好了,只是 CALLBACKS 不起作用。我可能做错了什么?
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
dump('foo');
return 'faa';
};
$defaultContext = [
AbstractNormalizer::CALLBACKS => [
'order_date' => $dateCallback,
]
];
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
$serializer = new Serializer([$normalizer], []);
$order = $serializer->denormalize($data, Orderform::class, 'array');
数据是一个简单的数组。
$data = ['order_date' => '2020-07-07',
'order_number' => '123'];
我希望调用 $dateCallback。但它似乎并没有这样做。 Orderform 实体正在填充,但没有填充我期望从回调中获得的值。
我也尝试用 json 和 xml 来做这一切,因为数组没有出现在文档中(但它可以工作,除了回调)
Symfony 4.4
我没有调试你的代码,而是基于 docs 制作了一个反序列化示例的工作版本,希望它能作为指南:
<?php
include 'vendor/autoload.php';
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Serializer;
//
//Inializing the Serializer, enconders and callbacks
//
$encoders = [new XmlEncoder(), new JsonEncoder()];
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
return $innerObject instanceof \DateTime ? $innerObject->format('Y-m-d H:i:sP') : '';
};
$defaultContext = [
AbstractNormalizer::CALLBACKS => [
'createdAt' => $dateCallback,
]
];
$normalizer = [new ObjectNormalizer(null, null, null, null, null, null, $defaultContext)];
$serializer = new Serializer($normalizer, $encoders);
//
//Creating Object Person that will be serialized
//
$person = new Person();
$person->setName('foo');
$person->setAge(99);
$person->setSportsperson(false);
$person->setCreatedAt(new \DateTime());
$arrayContent = $serializer->serialize($person, 'json');
// $arrayContent contains ["name" => "foo","age" => 99,"sportsperson" => false,"createdAt" => null]
//print_r($arrayContent); // or return it in a Response
// Lets deserialize it
$desrializedPerson = $serializer->deserialize($arrayContent, Person::class, 'json');
var_dump($desrializedPerson);
//
// Foo Bar stuff
//
// Entity Person declaration
class Person
{
private $age;
private $name;
private $sportsperson;
private $createdAt;
// Getters
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
public function getCreatedAt()
{
return $this->createdAt;
}
// Issers
public function isSportsperson()
{
return $this->sportsperson;
}
// Setters
public function setName($name)
{
$this->name = $name;
}
public function setAge($age)
{
$this->age = $age;
}
public function setSportsperson($sportsperson)
{
$this->sportsperson = $sportsperson;
}
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
}
我正在尝试将序列化与回调一起使用,如 https://symfony.com/doc/current/components/serializer.html#using-callbacks-to-serialize-properties-with-object-instances 中那样。但它似乎没有进入回调。我可以忽略带有 'IGNORED_ATTRIBUTES' 的属性就好了,只是 CALLBACKS 不起作用。我可能做错了什么?
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
dump('foo');
return 'faa';
};
$defaultContext = [
AbstractNormalizer::CALLBACKS => [
'order_date' => $dateCallback,
]
];
$normalizer = new ObjectNormalizer(null, null, null, null, null, null, $defaultContext);
$serializer = new Serializer([$normalizer], []);
$order = $serializer->denormalize($data, Orderform::class, 'array');
数据是一个简单的数组。
$data = ['order_date' => '2020-07-07',
'order_number' => '123'];
我希望调用 $dateCallback。但它似乎并没有这样做。 Orderform 实体正在填充,但没有填充我期望从回调中获得的值。 我也尝试用 json 和 xml 来做这一切,因为数组没有出现在文档中(但它可以工作,除了回调)
Symfony 4.4
我没有调试你的代码,而是基于 docs 制作了一个反序列化示例的工作版本,希望它能作为指南:
<?php
include 'vendor/autoload.php';
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Serializer;
//
//Inializing the Serializer, enconders and callbacks
//
$encoders = [new XmlEncoder(), new JsonEncoder()];
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
return $innerObject instanceof \DateTime ? $innerObject->format('Y-m-d H:i:sP') : '';
};
$defaultContext = [
AbstractNormalizer::CALLBACKS => [
'createdAt' => $dateCallback,
]
];
$normalizer = [new ObjectNormalizer(null, null, null, null, null, null, $defaultContext)];
$serializer = new Serializer($normalizer, $encoders);
//
//Creating Object Person that will be serialized
//
$person = new Person();
$person->setName('foo');
$person->setAge(99);
$person->setSportsperson(false);
$person->setCreatedAt(new \DateTime());
$arrayContent = $serializer->serialize($person, 'json');
// $arrayContent contains ["name" => "foo","age" => 99,"sportsperson" => false,"createdAt" => null]
//print_r($arrayContent); // or return it in a Response
// Lets deserialize it
$desrializedPerson = $serializer->deserialize($arrayContent, Person::class, 'json');
var_dump($desrializedPerson);
//
// Foo Bar stuff
//
// Entity Person declaration
class Person
{
private $age;
private $name;
private $sportsperson;
private $createdAt;
// Getters
public function getName()
{
return $this->name;
}
public function getAge()
{
return $this->age;
}
public function getCreatedAt()
{
return $this->createdAt;
}
// Issers
public function isSportsperson()
{
return $this->sportsperson;
}
// Setters
public function setName($name)
{
$this->name = $name;
}
public function setAge($age)
{
$this->age = $age;
}
public function setSportsperson($sportsperson)
{
$this->sportsperson = $sportsperson;
}
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
}
}