/**
  * @return array
  */
 public function resolveAttributeColumns()
 {
     if (empty($this->attributesFields)) {
         // TODO: Put a Cursor to avoid a findAll on attributes (╯°□°)╯︵ ┻━┻
         $attributes = $this->attributeRepository->findAll();
         $currencyCodes = $this->currencyRepository->getActivatedCurrencyCodes();
         $values = $this->valuesResolver->resolveEligibleValues($attributes);
         foreach ($values as $value) {
             $field = $this->resolveFlatAttributeName($value['attribute'], $value['locale'], $value['scope']);
             if (AttributeTypes::PRICE_COLLECTION === $value['type']) {
                 $this->attributesFields[] = $field;
                 foreach ($currencyCodes as $currencyCode) {
                     $currencyField = sprintf('%s-%s', $field, $currencyCode);
                     $this->attributesFields[] = $currencyField;
                 }
             } elseif (AttributeTypes::METRIC === $value['type']) {
                 $this->attributesFields[] = $field;
                 $metricField = sprintf('%s-%s', $field, 'unit');
                 $this->attributesFields[] = $metricField;
             } else {
                 $this->attributesFields[] = $field;
             }
         }
     }
     return $this->attributesFields;
 }
 /**
  * @return array
  */
 protected function getCurrencyCodes()
 {
     if (empty($this->currencyCodes)) {
         $this->currencyCodes = $this->currencyRepository->getActivatedCurrencyCodes();
     }
     return $this->currencyCodes;
 }
 /**
  * @return JsonResponse
  */
 public function indexAction()
 {
     $currencies = $this->currencyRepository->getActivatedCurrencies();
     $normalizedCurrencies = [];
     foreach ($currencies as $currency) {
         $normalizedCurrencies[$currency->getCode()] = ['code' => $currency->getCode()];
     }
     return new JsonResponse($normalizedCurrencies);
 }
 /**
  * Prepare attributes list for CSV headers
  *
  * @param array $attributesList
  *
  * @return array
  */
 protected function prepareAttributesList(array $attributesList)
 {
     $scopeCode = $this->catalogContext->getScopeCode();
     $localeCodes = $this->localeRepository->getActivatedLocaleCodes();
     $fieldsList = [];
     foreach ($attributesList as $attribute) {
         $attCode = $attribute->getCode();
         if ($attribute->isLocalizable() && $attribute->isScopable()) {
             foreach ($localeCodes as $localeCode) {
                 $fieldsList[] = sprintf('%s-%s-%s', $attCode, $localeCode, $scopeCode);
             }
         } elseif ($attribute->isLocalizable()) {
             foreach ($localeCodes as $localeCode) {
                 $fieldsList[] = sprintf('%s-%s', $attCode, $localeCode);
             }
         } elseif ($attribute->isScopable()) {
             $fieldsList[] = sprintf('%s-%s', $attCode, $scopeCode);
         } elseif (AttributeTypes::IDENTIFIER === $attribute->getAttributeType()) {
             array_unshift($fieldsList, $attCode);
         } elseif (AttributeTypes::PRICE_COLLECTION === $attribute->getAttributeType()) {
             foreach ($this->currencyRepository->getActivatedCurrencyCodes() as $currencyCode) {
                 $fieldsList[] = sprintf('%s-%s', $attCode, $currencyCode);
             }
         } else {
             $fieldsList[] = $attCode;
         }
     }
     return $fieldsList;
 }
 /**
  * Check if data are valid
  *
  * @param AttributeInterface $attribute
  * @param mixed              $data
  *
  * @return mixed
  */
 protected function checkData(AttributeInterface $attribute, $data)
 {
     if (!is_array($data)) {
         throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'adder', 'prices collection', gettype($data));
     }
     foreach ($data as $price) {
         if (!is_array($price)) {
             throw InvalidArgumentException::arrayOfArraysExpected($attribute->getCode(), 'adder', 'prices collection', gettype($data));
         }
         if (!array_key_exists('data', $price)) {
             throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'adder', 'prices collection', print_r($data, true));
         }
         if (!array_key_exists('currency', $price)) {
             throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'adder', 'prices collection', print_r($data, true));
         }
         if (!is_numeric($price['data']) && null !== $price['data']) {
             throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'adder', 'prices collection', gettype($price['data']));
         }
         if (!in_array($price['currency'], $this->currencyRepository->getActivatedCurrencyCodes())) {
             throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'adder', 'prices collection', $price['currency']);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function addMissingPrices(ProductValueInterface $value)
 {
     if (AttributeTypes::PRICE_COLLECTION === $value->getAttribute()->getAttributeType()) {
         $activeCurrencyCodes = $this->currencyRepository->getActivatedCurrencyCodes();
         $prices = $value->getPrices();
         foreach ($activeCurrencyCodes as $currencyCode) {
             if (null === $value->getPrice($currencyCode)) {
                 $this->addPriceForCurrency($value, $currencyCode);
             }
         }
         foreach ($prices as $price) {
             if (!in_array($price->getCurrency(), $activeCurrencyCodes)) {
                 $value->removePrice($price);
             }
         }
     }
     return $value;
 }
 /**
  * @param AttributeInterface $attribute
  * @param mixed              $data
  */
 protected function checkValue(AttributeInterface $attribute, $data)
 {
     if (!is_array($data)) {
         throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'filter', 'price', gettype($data));
     }
     if (!array_key_exists('data', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'filter', 'price', print_r($data, true));
     }
     if (!array_key_exists('currency', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', print_r($data, true));
     }
     if (null !== $data['data'] && !is_numeric($data['data'])) {
         throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'filter', 'price', gettype($data['data']));
     }
     if (!is_string($data['currency'])) {
         throw InvalidArgumentException::arrayStringKeyExpected($attribute->getCode(), 'currency', 'filter', 'price', gettype($data['currency']));
     }
     if (!in_array($data['currency'], $this->currencyRepository->getActivatedCurrencyCodes())) {
         throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'currency', 'The currency does not exist', 'filter', 'price', $data['currency']);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function configureOptions(OptionsResolver $resolver)
 {
     parent::configureOptions($resolver);
     $currencyChoices = $this->currencyRepository->getActivatedCurrencyCodes();
     $resolver->setDefaults(['data_type' => NumberFilterType::DATA_DECIMAL, 'currency_choices' => array_combine($currencyChoices, $currencyChoices), 'currency_options' => []]);
 }