/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     $result = $this->createObject($class);
     $fields = $this->fieldHelper->getFields($class, true);
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if (array_key_exists($fieldName, $data)) {
             $value = $data[$fieldName];
             if ($data[$fieldName] !== null && ($this->fieldHelper->isRelation($field) || $this->fieldHelper->isDateTimeField($field))) {
                 if ($this->fieldHelper->isMultipleRelation($field)) {
                     $entityClass = sprintf('ArrayCollection<%s>', $field['related_entity_name']);
                 } elseif ($this->fieldHelper->isSingleRelation($field)) {
                     $entityClass = $field['related_entity_name'];
                 } else {
                     $entityClass = 'DateTime';
                     $context = array_merge($context, ['type' => $field['type']]);
                 }
                 $context = array_merge($context, ['fieldName' => $fieldName]);
                 $value = $this->serializer->denormalize($value, $entityClass, $format, $context);
             }
             $this->fieldHelper->setObjectValue($result, $fieldName, $value);
         }
     }
     return $result;
 }
Ejemplo n.º 2
0
 /**
  * @param object $basicEntity
  * @param object $importedEntity
  * @param array $excludedProperties
  * @throws InvalidArgumentException
  */
 public function importEntity($basicEntity, $importedEntity, array $excludedProperties = array())
 {
     $basicEntityClass = ClassUtils::getClass($basicEntity);
     if ($basicEntityClass != ClassUtils::getClass($importedEntity)) {
         throw new InvalidArgumentException('Basic and imported entities must be instances of the same class');
     }
     $entityMetadata = $this->getEntityManager($basicEntityClass)->getClassMetadata($basicEntityClass);
     $importedEntityProperties = array_diff(array_merge($entityMetadata->getFieldNames(), $entityMetadata->getAssociationNames()), $excludedProperties);
     foreach ($importedEntityProperties as $propertyName) {
         $importedValue = $this->fieldHelper->getObjectValue($importedEntity, $propertyName);
         $this->fieldHelper->setObjectValue($basicEntity, $propertyName, $importedValue);
     }
 }
Ejemplo n.º 3
0
 /**
  * @param object $object
  * @param string $fieldName
  * @param mixed  $value
  * @param array  $exception
  *
  * @dataProvider objectValueProvider
  */
 public function testGetObjectValue($object, $fieldName, $value, array $exception)
 {
     if ($exception) {
         list($class, $message) = $exception;
         $this->setExpectedException($class, $message);
     }
     $this->assertEquals(null, $this->helper->getObjectValue($object, $fieldName));
     $this->helper->setObjectValue($object, $fieldName, $value);
     $this->assertEquals($value, $this->helper->getObjectValue($object, $fieldName));
 }
Ejemplo n.º 4
0
 /**
  * @param object $entity
  * @param array $searchContext
  * @return null|object
  */
 protected function findExistingEntity($entity, array $searchContext = [])
 {
     $entityName = ClassUtils::getClass($entity);
     $identifier = $this->databaseHelper->getIdentifier($entity);
     $existingEntity = null;
     // find by identifier
     if ($identifier) {
         $existingEntity = $this->databaseHelper->find($entityName, $identifier);
     }
     // find by identity fields
     if (!$existingEntity && (!$searchContext || $this->databaseHelper->getIdentifier(current($searchContext)))) {
         $existingEntity = $this->findExistingEntityByIdentityFields($entity, $searchContext);
     }
     if ($existingEntity && !$identifier) {
         $identifier = $this->databaseHelper->getIdentifier($existingEntity);
         $identifierName = $this->databaseHelper->getIdentifierFieldName($entity);
         $this->fieldHelper->setObjectValue($entity, $identifierName, $identifier);
     }
     return $existingEntity;
 }