DoctrineObjectConstructor.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Serializer\Construction;
  18. use Doctrine\Common\Persistence\ManagerRegistry;
  19. use JMS\Serializer\VisitorInterface;
  20. use JMS\Serializer\Metadata\ClassMetadata;
  21. /**
  22. * Doctrine object constructor for new (or existing) objects during deserialization.
  23. */
  24. class DoctrineObjectConstructor implements ObjectConstructorInterface
  25. {
  26. private $managerRegistry;
  27. private $fallbackConstructor;
  28. /**
  29. * Constructor.
  30. *
  31. * @param ManagerRegistry $managerRegistry Manager registry
  32. * @param ObjectConstructorInterface $fallbackConstructor Fallback object constructor
  33. */
  34. public function __construct(ManagerRegistry $managerRegistry, ObjectConstructorInterface $fallbackConstructor)
  35. {
  36. $this->managerRegistry = $managerRegistry;
  37. $this->fallbackConstructor = $fallbackConstructor;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type)
  43. {
  44. // Locate possible ObjectManager
  45. $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);
  46. if (!$objectManager) {
  47. // No ObjectManager found, proceed with normal deserialization
  48. return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  49. }
  50. // Locate possible ClassMetadata
  51. $classMetadataFactory = $objectManager->getMetadataFactory();
  52. if ($classMetadataFactory->isTransient($metadata->name)) {
  53. // No ClassMetadata found, proceed with normal deserialization
  54. return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  55. }
  56. // Managed entity, check for proxy load
  57. if (!is_array($data)) {
  58. // Single identifier, load proxy
  59. return $objectManager->getReference($metadata->name, $data);
  60. }
  61. // Fallback to default constructor if missing identifier(s)
  62. $classMetadata = $objectManager->getClassMetadata($metadata->name);
  63. $identifierList = array();
  64. foreach ($classMetadata->getIdentifierFieldNames() as $name) {
  65. if ( ! isset($data[$name])) {
  66. return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type);
  67. }
  68. $identifierList[$name] = $data[$name];
  69. }
  70. // Entity update, load it from database
  71. $object = $objectManager->find($metadata->name, $identifierList);
  72. $objectManager->initializeObject($object);
  73. return $object;
  74. }
  75. }