/**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = array())
 {
     $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, ['fieldName' => $fieldName]);
                 $value = $this->serializer->denormalize($value, $entityClass, $format, $context);
             }
             $this->fieldHelper->setObjectValue($result, $fieldName, $value);
         }
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = [])
 {
     $entityName = ClassUtils::getClass($object);
     $fields = $this->fieldHelper->getFields($entityName, true);
     $result = [];
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if ($this->isFieldSkippedForNormalization($entityName, $fieldName, $context)) {
             continue;
         }
         $fieldValue = $this->fieldHelper->getObjectValue($object, $fieldName);
         if (is_object($fieldValue)) {
             $fieldContext = $context;
             $fieldContext['fieldName'] = $fieldName;
             if (method_exists($object, 'getId')) {
                 $fieldContext['entityId'] = $object->getId();
             }
             $isFullMode = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full');
             // Do not export relation in short mode if it does not contain identity fields
             if (!$isFullMode && isset($field['related_entity_name']) && $this->fieldHelper->hasConfig($field['related_entity_name']) && !$this->hasIdentityFields($field['related_entity_name'])) {
                 continue;
             }
             if ($this->fieldHelper->isRelation($field)) {
                 if ($isFullMode) {
                     $fieldContext['mode'] = self::FULL_MODE;
                 } else {
                     $fieldContext['mode'] = self::SHORT_MODE;
                 }
             }
             if ($this->fieldHelper->isDateTimeField($field)) {
                 $fieldContext['type'] = $field['type'];
             }
             $fieldValue = $this->serializer->normalize($fieldValue, $format, $fieldContext);
         }
         $result[$fieldName] = $fieldValue;
     }
     return $result;
 }
Beispiel #3
0
 /**
  * @param bool $expected
  * @param array $field
  * @dataProvider dateTimeDataProvider
  */
 public function testIsDateTimeField($expected, array $field)
 {
     $this->assertSame($expected, $this->helper->isDateTimeField($field));
 }