DoctrineObjectConstructor.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Serializer\Construction;
  18. use Doctrine\Common\Persistence\ManagerRegistry;
  19. use JMS\SerializerBundle\Serializer\VisitorInterface;
  20. use JMS\SerializerBundle\Metadata\ClassMetadata;
  21. class DoctrineObjectConstructor implements ObjectConstructorInterface
  22. {
  23. private $managerRegistry;
  24. private $fallbackConstructor;
  25. /**
  26. * Constructor.
  27. *
  28. * @param ManagerRegistry $managerRegistry
  29. * @param ObjectConstructorInterface $fallbackConstructor
  30. */
  31. public function __construct(ManagerRegistry $managerRegistry, ObjectConstructorInterface $fallbackConstructor)
  32. {
  33. $this->managerRegistry = $managerRegistry;
  34. $this->fallbackConstructor = $fallbackConstructor;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type)
  40. {
  41. // Locate possible ObjectManager
  42. $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);
  43. if (!$objectManager) {
  44. // No ObjectManager found, proceed with normal deserialization
  45. return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  46. }
  47. // Locate possible ClassMetadata
  48. $classMetadataFactory = $objectManager->getMetadataFactory();
  49. if ($classMetadataFactory->isTransient($metadata->name)) {
  50. // No ClassMetadata found, proceed with normal deserialization
  51. return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  52. }
  53. // Managed entity, check for proxy load
  54. if (!is_array($data)) {
  55. // Single identifier, load proxy
  56. return $objectManager->getReference($metadata->name, $data);
  57. }
  58. // Entity update, load it from database
  59. $classMetadata = $objectManager->getClassMetadata($metadata->name);
  60. $identifierList = $classMetadata->getIdentifierFieldNames();
  61. $missingIdentifierList = array_filter(
  62. $identifierList,
  63. function ($identifier) use ($data)
  64. {
  65. return !isset($data[$identifier]);
  66. }
  67. );
  68. return (!$missingIdentifierList)
  69. ? $objectManager->find($metadata->name, $data)
  70. : $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  71. }
  72. }