Is is possible in Symfony Serializer
to deserialize an array of objects in a property? I have a Boss
class with the $Npc = []
property tha
I found a way to do this :). I installed the Symfony PropertyAccess package through Composer. With this package, you can add adders, removers and hassers. This way Symfony Serializer will automaticly fill the array with the correct objects. Example:
private $npcs = [];
public function addNpc(Npc $npc): void
{
$this->npcs[] = $npc;
}
public function hasNpcs(): bool
{
return count($this->npcs) > 0
}
etc.
This way you can use the ObjectNormalizer with:
$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
Edit: At least since v3.4 you have to create a remover method as well. Otherwise it just won't work (no error or warning).