I've made a custom Doctrine Type in symfony, that extends Doctrine\DBAL\Types\Type
I need to inject symfony's Serializer there, to decode some JSON using symfony's serializer so it can hyrdrate a specific class.
However, I can't override the constructor since it's final, and can't inject SerializerInterface
I've tried to use a setter and inject SerializerInterface
, but it's not working.
class MyCustomDoctrineType extends Type
{
private const NAME = 'my_custom_type';
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getJsonTypeDeclarationSQL($fieldDeclaration);
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
return $value;
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value === '') {
return null;
}
if (is_resource($value)) {
$value = stream_get_contents($value);
}
$this->serializer->deserialize($value, MyClass::class, 'json'); // this is what I want to accomplish
return $value;
}
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
public function getName()
{
return self::NAME;
}
}
Is there any way to do dependency injection there ?