參加以下 DTO 課程:
class UserDTO {
/**
* @param AddressDTO[] $addressBook
*/
public function __construct(
public string $name,
public int $age,
public ?AddressDTO $billingAddress,
public ?AddressDTO $shippingAddress,
public array $addressBook,
) {
}
}
class AddressDTO {
public function __construct(
public string $street,
public string $city,
) {
}
}
我想將它們序列化和反序列化到/從 JSON。
我正在使用以下序列化程式配置:
$encoders = [new JsonEncoder()];
$extractor = new PropertyInfoExtractor([], [
new PhpDocExtractor(),
new ReflectionExtractor(),
]);
$normalizers = [
new ObjectNormalizer(null, null, null, $extractor),
new ArrayDenormalizer(),
];
$serializer = new Serializer($normalizers, $encoders);
但是當序列化/反序列化這個物件時:
$address = new AddressDTO('Rue Paradis', 'Marseille');
$user = new UserDTO('John', 25, $address, null, [$address]);
$jsonContent = $serializer->serialize($user, 'json');
dd($serializer->deserialize($jsonContent, UserDTO::class, 'json'));
我得到以下結果:
UserDTO^ {#54
name: "John"
age: 25
billingAddress: AddressDTO^ {#48
street: "Rue Paradis"
city: "Marseille"
}
shippingAddress: null
addressBook: array:1 [
0 => array:2 [
"street" => "Rue Paradis"
"city" => "Marseille"
]
]
}
當我期望:
UserDTO^ {#54
name: "John"
age: 25
billingAddress: AddressDTO^ {#48
street: "Rue Paradis"
city: "Marseille"
}
shippingAddress: null
addressBook: array:1 [
0 => AddressDTO^ {#48
street: "Rue Paradis"
city: "Marseille"
}
]
}
如您所見,$addressBook反序列化為 的陣列array,而不是 的陣列AddressDTO。我希望從建構式中PhpDocExtractor讀取@param AddressDTO[],但這不起作用。
它僅在我$addressBook使用@var.
有沒有辦法讓它@param在建構式上使用簡單的方法?
(非)作業演示:https : //phpsandbox.io/n/gentle-mountain-mmod-rnmqd
我讀過和嘗試過的:
- 從 docblock 注釋中提取建構式引數的型別
- symfony 反序列化嵌套物件
- 如何在 Symfony Serializer 中反序列化一組物件?
建議的解決方案似乎都不適合我。
uj5u.com熱心網友回復:
顯然問題是PhpDocExtractor沒有從建構式中提取屬性。您需要為此使用特定的建構式:
use Symfony\Component\PropertyInfo;
use Symfony\Component\Serializer;
$phpDocExtractor = new PropertyInfo\Extractor\PhpDocExtractor();
$typeExtractor = new PropertyInfo\PropertyInfoExtractor(
typeExtractors: [ new PropertyInfo\Extractor\ConstructorExtractor([$phpDocExtractor]), $phpDocExtractor,]
);
$serializer = new Serializer\Serializer(
normalizers: [
new Serializer\Normalizer\ObjectNormalizer(propertyTypeExtractor: $typeExtractor),
new Serializer\Normalizer\ArrayDenormalizer(),
],
encoders: ['json' => new Serializer\Encoder\JsonEncoder()]
);
有了這個,你會得到想要的結果。我花了一點時間才弄清楚。多個非規范化器/提取器鏈總是讓我著迷。
或者,對于更復雜的作業系統專用情況,您可以創建自己的自定義非規范化器:
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareTrait
class UserDenormalizer
implements DenormalizerInterface, DenormalizerAwareInterface
{
use DenormalizerAwareTrait;
public function denormalize($data, string $type, string $format = null, array $context = [])
{
$addressBook = array_map(fn($address) => $this->denormalizer->denormalize($address, AddressDTO::class), $data['addressBook']);
return new UserDTO(
name: $data['name'],
age: $data['age'],
billingAddress: $this->denormalizer->denormalize($data['billingAddress'], AddressDTO::class),
shippingAddress: $this->denormalizer->denormalize($data['shippingAddress'], AddressDTO::class),
addressBook: $addressBook
);
}
public function supportsDenormalization($data, string $type, string $format = null)
{
return $type === UserDTO::class;
}
}
設定會變成這樣:
$extractor = new PropertyInfoExtractor([], [
new PhpDocExtractor(),
new ReflectionExtractor(),
]);
$userDenormalizer = new UserDenormalizer();
$normalizers = [
$userDenormalizer,
new ObjectNormalizer(null, null, null, $extractor),
new ArrayDenormalizer(),
];
$serializer = new Serializer($normalizers, [new JsonEncoder()]);
$userDenormalizer->setDenormalizer($serializer);
輸出變成你所期望的:
^ UserDTO^ {#39
name: "John"
age: 25
billingAddress: AddressDTO^ {#45
street: "Rue Paradis"
city: "Marseille"
}
shippingAddress: null
addressBook: array:2 [
0 => AddressDTO^ {#46
street: "Rue Paradis"
city: "Marseille"
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/401787.html
