/**
  * {@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;
 }
 /**
  * @param string $entityName
  * @param bool $fullData
  * @param int $singleRelationDeepLevel
  * @param int $multipleRelationDeepLevel
  * @return array
  */
 protected function getEntityRulesAndBackendHeaders($entityName, $fullData = false, $singleRelationDeepLevel = 0, $multipleRelationDeepLevel = 0)
 {
     // get fields data
     $fields = $this->fieldHelper->getFields($entityName, true);
     $rules = array();
     $backendHeaders = array();
     $defaultOrder = 10000;
     // generate conversion rules and backend header
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'excluded')) {
             continue;
         }
         // get import/export config parameters
         $fieldHeader = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'header', $field['label']);
         $fieldOrder = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'order');
         if ($fieldOrder === null || $fieldOrder === '') {
             $fieldOrder = $defaultOrder;
             $defaultOrder++;
         }
         $fieldOrder = (int) $fieldOrder;
         // process relations
         if ($this->fieldHelper->isRelation($field) && !$this->fieldHelper->processRelationAsScalar($entityName, $fieldName)) {
             $isSingleRelation = $this->fieldHelper->isSingleRelation($field) && $singleRelationDeepLevel > 0;
             $isMultipleRelation = $this->fieldHelper->isMultipleRelation($field) && $multipleRelationDeepLevel > 0;
             // if relation must be included
             if ($fullData && ($isSingleRelation || $isMultipleRelation)) {
                 $relatedEntityName = $field['related_entity_name'];
                 $fieldFullData = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full', false);
                 // process and merge relation rules and backend header for relation
                 list($relationRules, $relationBackendHeaders) = $this->getEntityRulesAndBackendHeaders($relatedEntityName, $fieldFullData, $singleRelationDeepLevel - 1, $multipleRelationDeepLevel - 1);
                 $relationRules = $this->buildRelationRules($relationRules, $isSingleRelation, $isMultipleRelation, $fieldName, $fieldHeader, $fieldOrder);
                 $rules = array_merge($rules, $relationRules);
                 $relationBackendHeaders = $this->buildBackendHeaders($relationBackendHeaders, $isSingleRelation, $isMultipleRelation, $entityName, $fieldName, $fieldOrder);
                 $backendHeaders = array_merge($backendHeaders, $relationBackendHeaders);
             }
         } else {
             // process scalars
             if ($fullData || $this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) {
                 $rules[$fieldHeader] = array('value' => $fieldName, 'order' => $fieldOrder);
                 $backendHeaders[] = $rules[$fieldHeader];
             }
         }
     }
     return array($this->sortData($rules), $this->sortData($backendHeaders));
 }
 /**
  * @param string $entityName
  * @param bool $fullData
  * @param int $singleRelationDeepLevel
  * @param int $multipleRelationDeepLevel
  * @param array $field
  * @param string $fieldHeader
  * @param int $fieldOrder
  *
  * @return array
  */
 protected function getRelatedEntityRulesAndBackendHeaders($entityName, $fullData, $singleRelationDeepLevel, $multipleRelationDeepLevel, $field, $fieldHeader, $fieldOrder, $isIdentifier = false)
 {
     $fieldName = $field['name'];
     $relationRules = [];
     $relationBackendHeaders = [];
     $isSingleRelation = $this->fieldHelper->isSingleRelation($field) && $singleRelationDeepLevel > 0;
     $isMultipleRelation = $this->fieldHelper->isMultipleRelation($field) && $multipleRelationDeepLevel > 0;
     // if relation must be included
     if ($fullData && ($isSingleRelation || $isMultipleRelation)) {
         $relatedEntityName = $field['related_entity_name'];
         $fieldFullData = $this->fieldHelper->getConfigValue($entityName, $fieldName, 'full', false);
         // process and merge relation rules and backend header for relation
         list($relationRules, $relationBackendHeaders) = $this->getEntityRulesAndBackendHeaders($relatedEntityName, $fieldFullData, $singleRelationDeepLevel - 1, $multipleRelationDeepLevel - 1);
         $relationRules = $this->buildRelationRules($relationRules, $isSingleRelation, $isMultipleRelation, $fieldName, $fieldHeader, $fieldOrder);
         $relationBackendHeaders = $this->buildBackendHeaders($relationBackendHeaders, $isSingleRelation, $isMultipleRelation, $entityName, $fieldName, $fieldOrder);
     }
     return [$relationRules, $relationBackendHeaders];
 }
Ejemplo n.º 4
0
 /**
  * @param boolean $expected
  * @param array $field
  * @dataProvider singleRelationDataProvider
  */
 public function testIsSingleRelation($expected, array $field)
 {
     $this->assertSame($expected, $this->helper->isSingleRelation($field));
 }