Exemplo n.º 1
0
 /**
  * Returned normalized data
  *
  * @param Collection $object object to normalize
  * @param mixed $format
  * @param array $context
  * @return array
  */
 public function normalize($object, $format = null, array $context = array())
 {
     $result = array();
     foreach ($object as $item) {
         $serializedItem = $this->serializer->normalize($item, $format, $context);
         $result[] = $serializedItem;
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = array())
 {
     $targetClass = $this->getTargetClassName();
     if (!$object instanceof $targetClass) {
         return null;
     }
     $fieldRules = $this->getProcessedFieldRules();
     if (isset($context['mode']) && $context['mode'] == self::SHORT_MODE && $this->primaryField) {
         return $this->getPropertyAccessor()->getValue($object, $this->primaryField['denormalizeName']);
     }
     $result = array();
     foreach ($fieldRules as $field) {
         if (!$field['normalize']) {
             continue;
         }
         $value = $this->getPropertyAccessor()->getValue($object, $field['denormalizeName']);
         if (isset($field['type']) && $value !== null) {
             $value = $this->serializer->normalize($value, $format, array_merge($context, $field['context']));
         }
         $result[$field['normalizeName']] = $value;
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function normalize($object, $format = null, array $context = array())
 {
     $entityName = ClassUtils::getClass($object);
     $fields = $this->fieldHelper->getFields($entityName, true);
     $result = array();
     $propertyAccessor = PropertyAccess::createPropertyAccessor();
     foreach ($fields as $field) {
         $fieldName = $field['name'];
         // Do not normalize excluded fields
         if ($this->fieldHelper->getConfigValue($entityName, $fieldName, 'excluded')) {
             continue;
         }
         // Do not normalize non identity fields for short mode
         if ($this->getMode($context) == self::SHORT_MODE && !$this->fieldHelper->getConfigValue($entityName, $fieldName, 'identity')) {
             continue;
         }
         $fieldValue = $propertyAccessor->getValue($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_type']) && $this->fieldHelper->hasConfig($field['related_entity_type']) && !$this->hasIdentityFields($field['related_entity_type'])) {
                 continue;
             }
             if ($this->fieldHelper->isRelation($field)) {
                 if ($isFullMode) {
                     $fieldContext['mode'] = self::FULL_MODE;
                 } else {
                     $fieldContext['mode'] = self::SHORT_MODE;
                 }
             }
             $fieldValue = $this->serializer->normalize($fieldValue, $format, $fieldContext);
         }
         $result[$fieldName] = $fieldValue;
     }
     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;
 }