/**
  * {@inheritdoc}
  */
 public function normalize($entity, $format = null, array $context = [])
 {
     $result = $this->valuesNormalizer->normalize($entity, $format, $context);
     $type = $entity->getAttribute()->getAttributeType();
     $localizer = $this->localizerRegistry->getLocalizer($type);
     if (null !== $localizer) {
         $result['data'] = $localizer->localize($result['data'], $context);
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function convert(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 $i => $data) {
                     $items[$code][$i] = $this->convertAttribute($localizer, $data, $options, $code);
                 }
             }
         }
     }
     return $items;
 }
 /**
  * {@inheritdoc}
  */
 public function convertDefaultToLocalizedValue($code, $data, $options = [])
 {
     $attribute = $this->attributeRepository->findOneBy(['code' => $code]);
     if (null === $attribute) {
         return $data;
     }
     $attributeType = $attribute->getAttributeType();
     if (null === $attributeType) {
         return $data;
     }
     $localizer = $this->localizerRegistry->getLocalizer($attributeType);
     if (null === $localizer) {
         return $data;
     }
     return $localizer->convertDefaultToLocalized($data, $options);
 }
 /**
  * {@inheritdoc}
  */
 public function convertLocalizedToDefaultValues(array $items, array $options = [])
 {
     $this->violations = new ConstraintViolationList();
     $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] = $this->convertLocalizedToDefaultValue($localizer, $data, $options, $this->buildPropertyPath($data, $code));
                 }
             }
         }
     }
     return $items;
 }
 /**
  * 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;
 }
 /**
  * Set data from $actions to the given $product
  *
  * Actions should looks like that
  *
  * $actions =
  * [
  *     [
  *          'field'   => 'group',
  *          'value'   => 'summer_clothes',
  *          'options' => null
  *      ],
  *      [
  *          'field'   => 'category',
  *          'value'   => 'catalog_2013,catalog_2014',
  *          'options' => null
  *      ],
  * ]
  *
  * @param ProductInterface $product
  * @param array            $configuration
  *
  * @throws \LogicException
  *
  * @return ProductInterface $product
  */
 protected function updateProduct(ProductInterface $product, array $configuration)
 {
     $modifiedAttributesNb = 0;
     foreach ($configuration['actions'] as $action) {
         $attribute = $this->attributeRepository->findOneBy(['code' => $action['field']]);
         if (null === $attribute) {
             throw new \LogicException(sprintf('Attribute with code %s does not exist'), $action['field']);
         }
         if ($product->isAttributeEditable($attribute)) {
             $localizer = $this->localizerRegistry->getLocalizer($attribute->getAttributeType());
             if (null !== $localizer) {
                 $action['value'] = $localizer->delocalize($action['value'], ['locale' => $configuration['locale']]);
             }
             $this->propertySetter->setData($product, $action['field'], $action['value'], $action['options']);
             $modifiedAttributesNb++;
         }
     }
     if (0 === $modifiedAttributesNb) {
         $this->stepExecution->incrementSummaryInfo('skipped_products');
         $this->stepExecution->addWarning($this->getName(), 'pim_enrich.mass_edit_action.edit-common-attributes.message.no_valid_attribute', [], $product);
         return null;
     }
     return $product;
 }
 /**
  * Prepare product values
  *
  * @param ProductInterface $product
  * @param array            $actions
  *
  * @return array
  */
 protected function prepareProductValues(ProductInterface $product, array $actions)
 {
     $normalizedValues = json_decode($actions['normalized_values'], true);
     $attributeLocale = $actions['attribute_locale'];
     $filteredValues = [];
     foreach ($normalizedValues as $attributeCode => $values) {
         $attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
         if ($product->isAttributeEditable($attribute)) {
             $values = array_filter($values, function ($value) use($attributeLocale) {
                 return $attributeLocale === $value['locale'] || null === $value['locale'];
             });
             $localizer = $this->localizerRegistry->getLocalizer($attribute->getAttributeType());
             if (null !== $localizer) {
                 $locale = $actions['ui_locale'];
                 $values = array_map(function ($value) use($localizer, $locale) {
                     $value['data'] = $localizer->delocalize($value['data'], ['locale' => $locale]);
                     return $value;
                 }, $values);
             }
             $filteredValues[$attributeCode] = $values;
         }
     }
     return $filteredValues;
 }