/** * {@inheritdoc} */ public function denormalize($data, $class, $format = null, array $context = []) { $productValues = []; foreach ($data as $attFieldName => $dataValue) { $attributeInfos = $this->fieldExtractor->extractColumnInfo($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); }
/** * @param string $column * @param string $value * * @throws \LogicException * * @return array */ protected function convertValue($column, $value) { $attributeFieldInfo = $this->attrFieldExtractor->extractColumnInfo($column); if (null !== $attributeFieldInfo && isset($attributeFieldInfo['attribute'])) { $converter = $this->converterRegistry->getConverter($attributeFieldInfo['attribute']->getAttributeType()); if (null === $converter) { throw new \LogicException(sprintf('No converters found for attribute type "%s"', $attributeFieldInfo['attribute']->getAttributeType())); } return $converter->convert($attributeFieldInfo, $value); } throw new \LogicException(sprintf('Unable to convert the given column "%s"', $column)); }
/** * @param array $row the item to merge * * @return array merged $row */ public function merge(array $row) { $resultRow = []; $collectedMetrics = []; $collectedPrices = []; foreach ($row as $fieldName => $fieldValue) { $attributeInfos = $this->fieldExtractor->extractColumnInfo($fieldName); if (null !== $attributeInfos) { $attribute = $attributeInfos['attribute']; if (AbstractAttributeType::BACKEND_TYPE_METRIC === $attribute->getBackendType()) { $collectedMetrics = $this->collectMetricData($collectedMetrics, $attributeInfos, $fieldValue); } elseif (AbstractAttributeType::BACKEND_TYPE_PRICE === $attribute->getBackendType()) { $collectedPrices = $this->collectPriceData($collectedPrices, $attributeInfos, $fieldValue); } else { $resultRow[$fieldName] = $fieldValue; } } else { $resultRow[$fieldName] = $fieldValue; } } $resultRow = $this->mergeMetricData($resultRow, $collectedMetrics); $resultRow = $this->mergePriceData($resultRow, $collectedPrices); return $resultRow; }
/** * 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->fieldExtractor->extractColumnInfo($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)); } } } }
/** * Extract attribute field name information with attribute code, locale code, scope code * and optionally price currency * * Returned array like: * [ * "attribute" => AttributeInterface, * "locale_code" => <locale_code>|null, * "scope_code" => <scope_code>|null, * "price_currency" => <currency_code> // this key is optional * ] * * Return null if the field name does not match an attribute. * * @param string $fieldName * * @return array|null */ public function extractAttributeFieldNameInfos($fieldName) { return $this->attributeFieldExtractor->extractColumnInfo($fieldName); }