You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentNormalizer.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace PhpBenchmarksSymfony\RestApiBundle\Normalizer;
  3. use PhpBenchmarksRestData\Comment;
  4. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  5. use Symfony\Component\Serializer\SerializerAwareInterface;
  6. use Symfony\Component\Serializer\SerializerInterface;
  7. use Symfony\Component\Translation\TranslatorInterface;
  8. class CommentNormalizer implements NormalizerInterface, SerializerAwareInterface
  9. {
  10. /** @var SerializerInterface */
  11. protected $serializer;
  12. /** @var TranslatorInterface */
  13. protected $translator;
  14. public function __construct(TranslatorInterface $translator)
  15. {
  16. $this->translator = $translator;
  17. }
  18. /** @param SerializerInterface $serializer */
  19. public function setSerializer(SerializerInterface $serializer)
  20. {
  21. $this->serializer = $serializer;
  22. }
  23. /**
  24. * @param mixed $data
  25. * @param ?string $format
  26. * @return bool
  27. */
  28. public function supportsNormalization($data, $format = null)
  29. {
  30. return $data instanceof Comment;
  31. }
  32. /**
  33. * @param Comment $object
  34. * @param ?string $format
  35. * @param array $context
  36. * @return array
  37. */
  38. public function normalize($object, $format = null, array $context = [])
  39. {
  40. return [
  41. 'id' => $object->getId(),
  42. 'message' => $object->getMessage(),
  43. 'translated' => $this->translator->trans('translated.2000', [], 'phpbenchmarks'),
  44. 'type' => $this->serializer->normalize($object->getType(), $format, $context)
  45. ];
  46. }
  47. }