public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
 {
     if ($context instanceof \Lemon\RestBundle\Serializer\DeserializationContext && $context->shouldUseDoctrineConstructor()) {
         $this->useDoctrine();
     }
     return $this->objectConstructor->construct($visitor, $metadata, $data, $type, $context);
 }
 /**
  * {@inheritdoc}
  * @throws RuntimeException
  */
 public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
 {
     // Locate possible ObjectManager
     $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);
     if (!$objectManager) {
         // No ObjectManager found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Locate possible ClassMetadata
     $classMetadataFactory = $objectManager->getMetadataFactory();
     if ($classMetadataFactory->isTransient($metadata->name)) {
         // No ClassMetadata found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     try {
         $id = $this->resolveIdentifier($metadata, $data, $objectManager);
     } catch (\Exception $e) {
         $id = null;
     }
     if ($id && ($entity = $objectManager->find($metadata->name, $id))) {
         /* @var $objectManager EntityManager */
         if ($this->isNewIdRegistered($metadata->name, $id)) {
             throw new RuntimeException("Not a unique entity '{$metadata->name}' with identifier '{$id}''");
         }
         $this->registerNewId($metadata->name, $id);
         return $entity;
     }
     // No ClassMetadata found, proceed with normal deserialization
     return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
 }
 /**
  * @param VisitorInterface       $visitor
  * @param ClassMetadata          $metadata
  * @param mixed                  $data
  * @param array                  $type
  * @param DeserializationContext $context
  *
  * @return null|object
  */
 public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
 {
     // Locate possible ObjectManager
     $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);
     if (!$objectManager) {
         // No ObjectManager found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Locate possible ClassMetadata
     $classMetadataFactory = $objectManager->getMetadataFactory();
     if ($classMetadataFactory->isTransient($metadata->name)) {
         // No ClassMetadata found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Transform SimpleXMLElement to array
     if ($data instanceof \SimpleXMLElement) {
         $data = json_decode(json_encode($data), true);
     }
     // Managed entity, check for proxy load
     if (!is_array($data)) {
         // Single identifier, load proxy
         return $objectManager->getReference($metadata->name, $data);
     }
     // Fallback to default constructor if missing identifier(s)
     $classMetadata = $objectManager->getClassMetadata($metadata->name);
     $identifierList = array();
     foreach ($classMetadata->getIdentifierFieldNames() as $name) {
         if (array_key_exists($name, $data)) {
             $identifierList[$name] = $data[$name];
         }
     }
     $object = null;
     if (count($identifierList)) {
         // Entity update, try to load it from database
         $object = $objectManager->find($metadata->name, $identifierList);
     }
     if (!is_object($object)) {
         // Entity with that identifier didn't exist, create a new Entity
         $reflection = new \ReflectionClass($metadata->name);
         $object = $reflection->newInstanceArgs($identifierList);
     }
     $objectManager->initializeObject($object);
     return $object;
 }
 /**
  * Constructs a new object.
  *
  * @param VisitorInterface       $visitor
  * @param ClassMetadata          $metadata
  * @param mixed                  $data
  * @param array                  $type ["name" => string, "params" => array]
  * @param DeserializationContext $context
  *
  * @return object
  */
 public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context)
 {
     // Locate possible ObjectManager
     $objectManager = $this->managerRegistry->getManagerForClass($metadata->name);
     if (!$objectManager) {
         // No ObjectManager found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Locate possible ClassMetadata
     $classMetadataFactory = $objectManager->getMetadataFactory();
     if ($classMetadataFactory->isTransient($metadata->name)) {
         // No ClassMetadata found, proceed with normal deserialization
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Managed entity, check for proxy load
     if (!is_array($data)) {
         // Single identifier, load proxy
         return $objectManager->getReference($metadata->name, $data);
     }
     // Fallback to default constructor if missing identifier(s)
     $classMetadata = $objectManager->getClassMetadata($metadata->name);
     $identifierList = array();
     foreach ($classMetadata->getIdentifierFieldNames() as $name) {
         if (!array_key_exists($name, $data)) {
             return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
         }
         if ($data[$name] != 0) {
             $identifierList[$name] = $data[$name];
         }
     }
     if (!count($identifierList)) {
         return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
     }
     // Entity update, load it from database
     $object = $objectManager->find($metadata->name, $identifierList);
     $objectManager->initializeObject($object);
     return $object;
 }