I know that I can use and configure e.g. the DateTimeNormalizer of Symfony's Serializer like that:
$serializer = new Serializer(array(new DateTimeNormalizer()));
$dateAsString = $serializer->normalize(
new \DateTime('2016/01/01'),
null,
array(DateTimeNormalizer::FORMAT_KEY => 'Y/m')
);
// $dateAsString = '2016/01';
But if I want to use the Serializer without instantiation but with the "full Symfony app" Dependency injection instead like:
//class MyService...
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
//function MyFunction()
{
$json = $this->serializer->serialize($object, 'json');
}
it already comes with the most common Normalizers in a pre-ordered fashion.
Usually this is perfectly fine for my purpose.
But how could I e.g. configure the DateTimeNormalizer::FORMAT_KEY in the injection scenario without creating CustomNormalizers or lots of instantiating?