/**
* {@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;
}