class TranslatableNormalizer
Hierarchy
- class \Symfony\Component\Serializer\Normalizer\TranslatableNormalizer implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface
Expanded class hierarchy of TranslatableNormalizer
File
-
vendor/
symfony/ serializer/ Normalizer/ TranslatableNormalizer.php, line 19
Namespace
Symfony\Component\Serializer\NormalizerView source
final class TranslatableNormalizer implements NormalizerInterface {
public const NORMALIZATION_LOCALE_KEY = 'translatable_normalization_locale';
private array $defaultContext = [
self::NORMALIZATION_LOCALE_KEY => null,
];
public function __construct(TranslatorInterface $translator, array $defaultContext = []) {
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
/**
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, ?string $format = null, array $context = []) : string {
if (!$object instanceof TranslatableInterface) {
throw NotNormalizableValueException::createForUnexpectedDataType(\sprintf('The object must implement the "%s".', TranslatableInterface::class), $object, [
TranslatableInterface::class,
]);
}
return $object->trans($this->translator, $context[self::NORMALIZATION_LOCALE_KEY] ?? $this->defaultContext[self::NORMALIZATION_LOCALE_KEY]);
}
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []) : bool {
return $data instanceof TranslatableInterface;
}
public function getSupportedTypes(?string $format) : array {
return [
TranslatableInterface::class => true,
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
TranslatableNormalizer::$defaultContext | private | property | ||
TranslatableNormalizer::getSupportedTypes | public | function | Returns the types potentially supported by this normalizer. | Overrides NormalizerInterface::getSupportedTypes |
TranslatableNormalizer::NORMALIZATION_LOCALE_KEY | public | constant | ||
TranslatableNormalizer::normalize | public | function | Overrides NormalizerInterface::normalize | |
TranslatableNormalizer::supportsNormalization | public | function | Checks whether the given class is supported for normalization by this normalizer. | Overrides NormalizerInterface::supportsNormalization |
TranslatableNormalizer::__construct | public | function |