/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = array()) { $productValues = []; foreach ($data as $attFieldName => $dataValue) { $attributeInfos = $this->fieldNameBuilder->extractAttributeFieldNameInfos($attFieldName); if (null !== $attributeInfos) { $attribute = $attributeInfos['attribute']; unset($attributeInfos['attribute']); $valueKey = $attribute->getCode(); if ($attribute->isLocalizable()) { $valueKey .= '-' . $attributeInfos['locale_code']; } if ($attribute->isScopable()) { $valueKey .= '-' . $attributeInfos['scope_code']; } if (isset($productValues[$valueKey])) { $value = $productValues[$valueKey]; } else { $value = new $this->valueClass(); $value->setAttribute($attribute); $value->setLocale($attributeInfos['locale_code']); $value->setScope($attributeInfos['scope_code']); } $productValues[$valueKey] = $this->valueDenormalizer->denormalize($dataValue, $this->valueClass, 'csv', ['entity' => $value] + $attributeInfos); } } return new ArrayCollection($productValues); }
/** * Filter empty values that are not used in a template then denormalize the product values objects from CSV fields * * @param array $rawProductValues * @param ProductTemplateInterface $template * * @return ProductValueInterface[] */ protected function denormalizeValuesFromItemData(array $rawProductValues, ProductTemplateInterface $template = null) { $templateCodes = null !== $template ? array_keys($template->getValuesData()) : []; foreach ($rawProductValues as $index => $data) { $attributeInfos = $this->fieldNameBuilder->extractAttributeFieldNameInfos($index); $attribute = $attributeInfos['attribute']; if ('' === trim($data) && !in_array($attribute->getCode(), $templateCodes)) { unset($rawProductValues[$index]); } } return $this->denormalizer->denormalize($rawProductValues, 'ProductValue[]', 'csv'); }
/** * Checks that attributes in the header have existing locale, scope and currency. * * @throws \LogicException */ protected function checkAttributesInHeader() { $channels = $this->channelRepository->getChannelCodes(); $locales = $this->localeRepository->getActivatedLocaleCodes(); $currencies = $this->currencyRepository->getActivatedCurrencyCodes(); foreach ($this->fieldNames as $fieldName) { if (null !== ($info = $this->fieldNameBuilder->extractAttributeFieldNameInfos($fieldName))) { $locale = $info['locale_code']; $channel = $info['scope_code']; $currency = isset($info['price_currency']) ? $info['price_currency'] : null; if (null !== $locale && !in_array($locale, $locales)) { throw new \LogicException(sprintf('Locale %s does not exist.', $locale)); } if (null !== $channel && !in_array($channel, $channels)) { throw new \LogicException(sprintf('Channel %s does not exist.', $channel)); } if (null !== $currency && !in_array($currency, $currencies)) { throw new \LogicException(sprintf('Currency %s does not exist.', $currency)); } } } }