BaseSerializerHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\CoreBundle\Serializer;
  11. use JMS\Serializer\Context;
  12. use JMS\Serializer\GraphNavigator;
  13. use JMS\Serializer\VisitorInterface;
  14. use Sonata\CoreBundle\Model\ManagerInterface;
  15. /**
  16. * @author Sylvain Deloux <sylvain.deloux@ekino.com>
  17. */
  18. abstract class BaseSerializerHandler implements SerializerHandlerInterface
  19. {
  20. /**
  21. * @var ManagerInterface
  22. */
  23. protected $manager;
  24. /**
  25. * @var string[]
  26. */
  27. protected static $formats;
  28. /**
  29. * @param ManagerInterface $manager
  30. */
  31. public function __construct(ManagerInterface $manager)
  32. {
  33. $this->manager = $manager;
  34. }
  35. /**
  36. * @param string[] $formats
  37. */
  38. final public static function setFormats(array $formats)
  39. {
  40. static::$formats = $formats;
  41. }
  42. /**
  43. * @param string $format
  44. */
  45. final public static function addFormat($format)
  46. {
  47. static::$formats[] = $format;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function getSubscribingMethods()
  53. {
  54. // NEXT_MAJOR : remove this block
  55. if (null === static::$formats) {
  56. static::$formats = array('json', 'xml', 'yml');
  57. @trigger_error(
  58. '$formats has been set to default array("json", "xml", "yml"). Setting $formats to a
  59. default array is deprecated since version 3.0 and will be removed in 4.0. Use SonataCoreBundle
  60. configuration to add default serializer formats.',
  61. E_USER_DEPRECATED
  62. );
  63. }
  64. $type = static::getType();
  65. $methods = array();
  66. foreach (static::$formats as $format) {
  67. $methods[] = array(
  68. 'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
  69. 'format' => $format,
  70. 'type' => $type,
  71. 'method' => 'serializeObjectToId',
  72. );
  73. $methods[] = array(
  74. 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
  75. 'format' => $format,
  76. 'type' => $type,
  77. 'method' => 'deserializeObjectFromId',
  78. );
  79. }
  80. return $methods;
  81. }
  82. /**
  83. * Serialize data object to id.
  84. *
  85. * @param VisitorInterface $visitor
  86. * @param object $data
  87. * @param array $type
  88. * @param Context $context
  89. *
  90. * @return int|null
  91. */
  92. public function serializeObjectToId(VisitorInterface $visitor, $data, array $type, Context $context)
  93. {
  94. $className = $this->manager->getClass();
  95. if ($data instanceof $className) {
  96. return $visitor->visitInteger($data->getId(), $type, $context);
  97. }
  98. return;
  99. }
  100. /**
  101. * Deserialize object from its id.
  102. *
  103. * @param VisitorInterface $visitor
  104. * @param int $data
  105. * @param array $type
  106. *
  107. * @return null|object
  108. */
  109. public function deserializeObjectFromId(VisitorInterface $visitor, $data, array $type)
  110. {
  111. return $this->manager->findOneBy(array('id' => $data));
  112. }
  113. }