fix normalization for docgen

This commit is contained in:
Julien Fastré 2022-03-14 21:27:21 +01:00
parent c61577edda
commit 90bebbad7d
2 changed files with 19 additions and 3 deletions

View File

@ -40,6 +40,7 @@ class UserJob
* @var array|string[]A
* @ORM\Column(name="label", type="json")
* @Serializer\Groups({"read", "docgen:read"})
* @Serializer\Context({"is-translatable": true}, groups={"docgen:read"})
*/
protected array $label = [];

View File

@ -16,10 +16,11 @@ use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberUtil;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterface
class PhonenumberNormalizer implements ContextAwareNormalizerInterface, DenormalizerInterface
{
private string $defaultCarrierCode;
@ -53,6 +54,10 @@ class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterfac
public function normalize($object, ?string $format = null, array $context = []): string
{
if ($format === 'docgen' && null === $object) {
return '';
}
return $this->phoneNumberUtil->formatOutOfCountryCallingNumber($object, $this->defaultCarrierCode);
}
@ -61,8 +66,18 @@ class PhonenumberNormalizer implements NormalizerInterface, DenormalizerInterfac
return 'libphonenumber\PhoneNumber' === $type;
}
public function supportsNormalization($data, ?string $format = null)
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
return $data instanceof PhoneNumber;
if ($data instanceof PhoneNumber && $format === 'json') {
return true;
}
if ($format === 'docgen' && (
$data instanceof PhoneNumber || PhoneNumber::class === ($context['docgen:expects'] ?? null)
)) {
return true;
}
return false;
}
}