Symfony\Component\Serializer\SerializerInterface::normalize PHP Метод

normalize() публичный Метод

Normalizes any data into a set of arrays/scalars
public normalize ( mixed $data, string $format = null ) : array | scalar
$data mixed data to normalize
$format string format name, present to give the option to normalizers to act differently based on formats
Результат array | scalar
    function normalize($data, $format = null);

Usage Example

 /**
  * {@inheritdoc}
  */
 public function normalize($entity, $format = null, array $context = [])
 {
     $data = $entity->getData();
     $fieldName = $this->getFieldValue($entity);
     $result = null;
     if (is_array($data)) {
         $data = new ArrayCollection($data);
     }
     if (is_null($data)) {
         $result = [$fieldName => ''];
     } elseif (is_int($data)) {
         $result = [$fieldName => (string) $data];
     } elseif (is_float($data)) {
         $result = [$fieldName => sprintf(sprintf('%%.%sF', $this->precision), $data)];
     } elseif (is_string($data)) {
         $result = [$fieldName => $data];
     } elseif (is_bool($data)) {
         $result = [$fieldName => (string) (int) $data];
     } elseif (is_object($data)) {
         $context['field_name'] = $fieldName;
         $result = $this->serializer->normalize($data, $format, $context);
     }
     if (null === $result) {
         throw new \RuntimeException(sprintf('Cannot normalize product value "%s" which data is a(n) "%s"', $fieldName, is_object($data) ? get_class($data) : gettype($data)));
     }
     return $result;
 }
All Usage Examples Of Symfony\Component\Serializer\SerializerInterface::normalize