/**
  * {@inheritdoc}
  */
 public function normalize($entity, $format = null, array $context = [])
 {
     $data = $entity->getData();
     $fieldName = $this->getFieldName($entity);
     if ($this->filterLocaleSpecific($entity)) {
         return [];
     }
     $result = null;
     if (is_array($data)) {
         $data = new ArrayCollection($data);
     }
     $type = $entity->getAttribute()->getAttributeType();
     $backendType = $entity->getAttribute()->getBackendType();
     if (AttributeTypes::BOOLEAN === $type) {
         $result = [$fieldName => (string) (int) $data];
     } elseif (is_null($data)) {
         $result = [$fieldName => ''];
         if ('metric' === $backendType) {
             $result[$fieldName . '-unit'] = '';
         }
     } elseif (is_int($data)) {
         $result = [$fieldName => (string) $data];
     } elseif (is_float($data) || 'decimal' === $entity->getAttribute()->getBackendType()) {
         $pattern = $entity->getAttribute()->isDecimalsAllowed() ? sprintf('%%.%sF', $this->precision) : '%d';
         $result = [$fieldName => sprintf($pattern, $data)];
     } elseif (is_string($data)) {
         $result = [$fieldName => $data];
     } elseif (is_object($data)) {
         // TODO: Find a way to have proper currency-suffixed keys for normalized price data
         // even when an empty collection is passed
         if ('prices' === $backendType && $data instanceof Collection && $data->isEmpty()) {
             $result = [];
         } elseif ('options' === $backendType && $data instanceof Collection && $data->isEmpty() === false) {
             $data = $this->sortOptions($data);
             $context['field_name'] = $fieldName;
             $result = $this->serializer->normalize($data, $format, $context);
         } else {
             $context['field_name'] = $fieldName;
             if ('metric' === $backendType) {
                 $context['decimals_allowed'] = $entity->getAttribute()->isDecimalsAllowed();
             } elseif ('media' === $backendType) {
                 $context['value'] = $entity;
             }
             $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)));
     }
     $localizer = $this->localizerRegistry->getLocalizer($type);
     if (null !== $localizer) {
         foreach ($result as $field => $data) {
             $result[$field] = $localizer->localize($data, $context);
         }
     }
     return $result;
 }
 /**
  * Change users' data (example: "12,45") into storable data (example: "12.45").
  *
  * @param array  $data
  * @param string $uiLocaleCode
  *
  * @return array
  */
 protected function delocalizeData(array $data, $uiLocaleCode)
 {
     foreach ($data as $code => $values) {
         $attribute = $this->attributeRepository->findOneByIdentifier($code);
         $localizer = $this->localizerRegistry->getLocalizer($attribute->getAttributeType());
         if (null !== $localizer) {
             $values = array_map(function ($value) use($localizer, $uiLocaleCode) {
                 $value['data'] = $localizer->delocalize($value['data'], ['locale' => $uiLocaleCode]);
                 return $value;
             }, $values);
             $data[$code] = $values;
         }
     }
     return $data;
 }
 /**
  * {@inheritdoc}
  *
  * Before:
  * [
  *     "name": [{
  *         "locale": "fr_FR",
  *         "scope":  null,
  *         "data":  "T-shirt super beau",
  *     }],
  *     "price": [
  *          {
  *              "locale": null,
  *              "scope":  ecommerce,
  *              "data":   [
  *                  {"data": 10.78, "currency": "EUR"},
  *                  {"data": 24, "currency": "USD"},
  *                  {"data": 20.75, "currency": "CHF"}
  *              ]
  *          }
  *     ],
  *     "length": [{
  *         "locale": "en_US",
  *         "scope":  "mobile",
  *         "data":   {"data": 10.45, "unit": "CENTIMETER"}
  *     }]
  *     [...]
  *
  * After:
  * [
  *     "name": [{
  *         "locale": "fr_FR",
  *         "scope":  null,
  *         "data":  "T-shirt super beau",
  *     }],
  *     "price": [
  *          {
  *              "locale": null,
  *              "scope":  ecommerce,
  *              "data":   [
  *                  {"data": "10,78", "currency": "EUR"},
  *                  {"data": "24", "currency": "USD"},
  *                  {"data": "20,75", "currency": "CHF"}
  *              ]
  *          }
  *     ],
  *     "length": [{
  *         "locale": "en_US",
  *         "scope":  "mobile",
  *         "data":   {"data": "10,45", "unit": "CENTIMETER"}
  *     }]
  *     [...]
  */
 public function convertToLocalizedFormats(array $items, array $options = [])
 {
     $attributeTypes = $this->attributeRepository->getAttributeTypeByCodes(array_keys($items));
     foreach ($items as $code => $item) {
         if (isset($attributeTypes[$code])) {
             $localizer = $this->localizerRegistry->getLocalizer($attributeTypes[$code]);
             if (null !== $localizer) {
                 foreach ($item as $index => $data) {
                     $items[$code][$index]['data'] = $localizer->localize($data['data'], $options);
                 }
             }
         }
     }
     return $items;
 }