Пример #1
0
 /**
  * Sets the attribute
  *
  * @param AbstractAttribute $attribute
  *
  * @throws ColumnLabelException
  */
 public function setAttribute(AbstractAttribute $attribute = null)
 {
     $this->attribute = $attribute;
     if (null === $attribute) {
         $this->locale = null;
         $this->scope = null;
         $this->suffixes = $this->rawSuffixes;
         $this->propertyPath = lcfirst(Inflector::classify($this->name));
     } else {
         $this->propertyPath = $attribute->getBackendType();
         $suffixes = $this->rawSuffixes;
         if ($attribute->isLocalizable()) {
             if (count($suffixes)) {
                 $this->locale = array_shift($suffixes);
             } else {
                 throw new ColumnLabelException('The column "%column%" must contain a locale code', array('%column%' => $this->label));
             }
         }
         if ($attribute->isScopable()) {
             if (count($suffixes)) {
                 $this->scope = array_shift($suffixes);
             } else {
                 throw new ColumnLabelException('The column "%column%" must contain a scope code', array('%column%' => $this->label));
             }
         }
         $this->suffixes = $suffixes;
     }
 }
 /**
  * Test related method
  */
 public function testConstruct()
 {
     $this->assertEntity($this->attribute);
     $this->assertEmptyCollection($this->attribute->getOptions());
     $this->assertNull($this->attribute->getAvailableLocales());
     $this->assertEmptyCollection($this->attribute->getTranslations());
     $this->assertFalse($this->attribute->isRequired());
     $this->assertFalse($this->attribute->isUnique());
     $this->assertNull($this->attribute->getDefaultValue());
     $this->assertFalse($this->attribute->isLocalizable());
     $this->assertFalse($this->attribute->isScopable());
     $this->assertFalse($this->attribute->isUseableAsGridColumn());
     $this->assertFalse($this->attribute->isUseableAsGridFilter());
     $this->assertNull($this->attribute->isDecimalsAllowed());
     $this->assertNull($this->attribute->isNegativeAllowed());
 }
 function let(SerializerInterface $serializer, AbstractAttribute $simpleAttribute)
 {
     $serializer->implement('Symfony\\Component\\Serializer\\Normalizer\\NormalizerInterface');
     $this->setSerializer($serializer);
     $simpleAttribute->isLocalizable()->willReturn(false);
     $simpleAttribute->isScopable()->willReturn(false);
     $simpleAttribute->getCode()->willReturn('simple');
 }
 function it_adds_a_order_by_on_an_attribute_value_in_the_query(Builder $queryBuilder, AbstractAttribute $sku)
 {
     $sku->getCode()->willReturn('sku');
     $sku->isLocalizable()->willReturn(false);
     $sku->isScopable()->willReturn(false);
     $queryBuilder->sort('normalizedData.sku', 'desc')->willReturn($queryBuilder);
     $this->addAttributeSorter($sku, 'desc');
 }
 function it_adds_a_less_than_or_equals_filter_in_the_query(Builder $queryBuilder, AbstractAttribute $price)
 {
     $price->getCode()->willReturn('price');
     $price->isLocalizable()->willReturn(true);
     $price->isScopable()->willReturn(true);
     $queryBuilder->field('normalizedData.price-en_US-mobile.EUR.data')->willReturn($queryBuilder);
     $queryBuilder->lte(22.5)->willReturn($queryBuilder);
     $this->addAttributeFilter($price, '<=', '22.5 EUR');
 }
 function it_adds_a_in_filter_on_an_attribute_value_in_the_query($qb, AbstractAttribute $color)
 {
     $color->getCode()->willReturn('color');
     $color->isLocalizable()->willReturn(true);
     $color->isScopable()->willReturn(false);
     $qb->field('normalizedData.color-en_US.id')->willReturn($qb);
     $qb->in([1, 2])->willReturn($qb);
     $this->addAttributeFilter($color, 'IN', [1, 2]);
 }
 function it_adds_a_like_filter_on_an_attribute_value_in_the_query(Builder $queryBuilder, AbstractAttribute $sku)
 {
     $sku->getCode()->willReturn('sku');
     $sku->isLocalizable()->willReturn(false);
     $sku->isScopable()->willReturn(false);
     $queryBuilder->field('normalizedData.sku')->willReturn($queryBuilder);
     $queryBuilder->equals('my-sku')->willReturn($queryBuilder);
     $this->addAttributeFilter($sku, 'LIKE', 'my-sku');
 }
 function it_adds_a_less_than_or_equals_filter_in_the_query(Builder $queryBuilder, AbstractAttribute $metric)
 {
     $metric->getCode()->willReturn('weight');
     $metric->isLocalizable()->willReturn(true);
     $metric->isScopable()->willReturn(true);
     $queryBuilder->field('normalizedData.weight-en_US-mobile.baseData')->willReturn($queryBuilder);
     $queryBuilder->lte(22.5)->willReturn($queryBuilder);
     $this->addAttributeFilter($metric, '<=', '22.5');
 }
 function it_returns_attribute_informations_from_field_name_with_price_attribute($managerRegistry, AttributeRepository $repository, AbstractAttribute $attribute)
 {
     $attribute->getCode()->willReturn('foo');
     $attribute->isLocalizable()->willReturn(false);
     $attribute->isScopable()->willReturn(false);
     $attribute->getBackendType()->willReturn('prices');
     $repository->findByReference('foo')->willReturn($attribute);
     $managerRegistry->getRepository(self::ATTRIBUTE_CLASS)->willReturn($repository);
     $this->extractAttributeFieldNameInfos('foo-USD')->shouldReturn(['attribute' => $attribute, 'locale_code' => null, 'scope_code' => null, 'price_currency' => 'USD']);
 }
 /**
  * Get all locale codes
  * @param AbstractAttribute $attribute
  *
  * @return array
  */
 public function getLocaleCodes(AbstractAttribute $attribute = null)
 {
     $localeCodes = [];
     if (null === $attribute || $attribute->isLocalizable()) {
         foreach ($this->getLocales() as $locale) {
             $localeCodes[] = $locale->getCode();
         }
     }
     return $localeCodes;
 }
 function it_normalizes_value_with_decimal_support_backend(ProductValueInterface $value, AbstractAttribute $attribute)
 {
     $attribute->getCode()->willReturn('code');
     $attribute->getBackendType()->willReturn('decimal');
     $attribute->isLocalizable()->willReturn(false);
     $attribute->isScopable()->willReturn(false);
     $value->getData()->willReturn('42.42');
     $value->getAttribute()->willReturn($attribute);
     $this->normalize($value, 'mongodb_json', [])->shouldReturn(['code' => 42.42]);
 }
Пример #12
0
 /**
  * Prepare join to attribute condition with current locale and scope criterias
  *
  * @param AbstractAttribute $attribute the attribute
  * @param string            $joinAlias the value join alias
  *
  * @return string
  */
 public function prepareCondition(AbstractAttribute $attribute, $joinAlias)
 {
     $condition = $joinAlias . '.attribute = ' . $attribute->getId();
     if ($attribute->isLocalizable()) {
         $condition .= ' AND ' . $joinAlias . '.locale = ' . $this->qb->expr()->literal($this->context->getLocaleCode());
     }
     if ($attribute->isScopable()) {
         $condition .= ' AND ' . $joinAlias . '.scope = ' . $this->qb->expr()->literal($this->context->getScopeCode());
     }
     return $condition;
 }
 function it_adds_a_like_filter_in_the_query(QueryBuilder $queryBuilder, AbstractAttribute $sku)
 {
     $sku->getId()->willReturn(42);
     $sku->getCode()->willReturn('sku');
     $sku->getBackendType()->willReturn('varchar');
     $sku->isLocalizable()->willReturn(false);
     $sku->isScopable()->willReturn(false);
     $queryBuilder->expr()->willReturn(new Expr());
     $queryBuilder->getRootAlias()->willReturn('p');
     $condition = "filtersku1.attribute = 42 AND filtersku1.varchar LIKE 'My Sku'";
     $queryBuilder->innerJoin('p.values', 'filtersku1', 'WITH', $condition)->shouldBeCalled();
     $this->addAttributeFilter($sku, 'LIKE', 'My Sku');
 }
 function it_adds_a_not_between_filter_on_an_attribute_value_in_the_query($queryBuilder, AbstractAttribute $date)
 {
     $date->getCode()->willReturn('release_date');
     $date->isLocalizable()->willReturn(true);
     $date->isScopable()->willReturn(true);
     $queryBuilder->expr()->willReturn($queryBuilder);
     $queryBuilder->addAnd($queryBuilder)->willReturn($queryBuilder);
     $queryBuilder->addOr($queryBuilder)->willReturn($queryBuilder);
     $queryBuilder->addOr($queryBuilder)->willReturn($queryBuilder);
     $queryBuilder->lte(strtotime('2014-03-15'))->willReturn($queryBuilder);
     $queryBuilder->gte(strtotime('2014-03-20 23:59:59'))->willReturn($queryBuilder);
     $this->addAttributeFilter($date, ['from' => '<', 'to' => '>'], ['from' => '2014-03-15', 'to' => '2014-03-20']);
 }
 /**
  * Extract informations from an attribute and exploded field name
  * This method is used from extractAttributeFieldNameInfos and can be redefine to add new rules
  *
  * @param AbstractAttribute $attribute
  * @param array             $explodedFieldName
  *
  * @return array
  */
 protected function extractAttributeInfos(AbstractAttribute $attribute, array $explodedFieldName)
 {
     if ($attribute->isLocalizable() && $attribute->isScopable()) {
         $localeCode = $explodedFieldName[1];
         $scopeCode = $explodedFieldName[2];
         $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[3] : null;
     } elseif ($attribute->isLocalizable()) {
         $localeCode = $explodedFieldName[1];
         $scopeCode = null;
         $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[2] : null;
     } elseif ($attribute->isScopable()) {
         $localeCode = null;
         $scopeCode = $explodedFieldName[1];
         $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[2] : null;
     } else {
         $localeCode = null;
         $scopeCode = null;
         $priceCurrency = $attribute->getBackendType() === 'prices' ? $explodedFieldName[1] : null;
     }
     $priceArray = null === $priceCurrency ? [] : ['price_currency' => $priceCurrency];
     return ['attribute' => $attribute, 'locale_code' => $localeCode, 'scope_code' => $scopeCode] + $priceArray;
 }
 /**
  * @param AbstractAttribute $attribute
  *
  * @return array
  */
 protected function getAttributeClasses(AbstractAttribute $attribute)
 {
     $classes = array();
     if ($attribute->isScopable()) {
         $classes['scopable'] = true;
     }
     if ($attribute->isLocalizable()) {
         $classes['localizable'] = true;
     }
     if ('pim_catalog_price_collection' === $attribute->getAttributeType()) {
         $classes['currency'] = true;
     }
     return $classes;
 }
 function it_adds_a_sorter_in_the_query(QueryBuilder $queryBuilder, AbstractAttribute $sku)
 {
     $sku->getId()->willReturn(42);
     $sku->getCode()->willReturn('sku');
     $sku->getBackendType()->willReturn('varchar');
     $sku->isLocalizable()->willReturn(false);
     $sku->isScopable()->willReturn(false);
     $queryBuilder->expr()->willReturn(new Expr());
     $queryBuilder->getRootAlias()->willReturn('p');
     $queryBuilder->getDQLPart('join')->willReturn([]);
     $queryBuilder->resetDQLPart('join')->shouldBeCalled();
     $condition = "sorterVsku1.attribute = 42";
     $queryBuilder->leftJoin('p.values', 'sorterVsku1', 'WITH', $condition)->shouldBeCalled();
     $queryBuilder->addOrderBy('sorterVsku1.varchar', 'DESC')->shouldBeCalled();
     $this->addAttributeSorter($sku, 'DESC');
 }
 /**
  * Provides the potential column keys for this attribute
  *
  * @param AbstractAttribute $attribute
  *
  * @return array
  */
 protected function getAttributeKeys(AbstractAttribute $attribute)
 {
     $keys = [];
     $keys[] = $attribute->getCode();
     $updatedKeys = [];
     if ($attribute->isScopable() && $attribute->isLocalizable()) {
         foreach ($this->getLocales() as $locale) {
             foreach ($this->getChannels() as $channel) {
                 foreach ($keys as $baseKey) {
                     $key = $baseKey . '-' . $locale->getCode() . '-' . $channel->getCode();
                     $updatedKeys[] = $key;
                 }
             }
         }
         $keys = $updatedKeys;
     } elseif ($attribute->isScopable() && !$attribute->isLocalizable()) {
         foreach ($this->getChannels() as $channel) {
             foreach ($keys as $baseKey) {
                 $key = $baseKey . '-' . $channel->getCode();
                 $updatedKeys[] = $key;
             }
         }
         $keys = $updatedKeys;
     } elseif (!$attribute->isScopable() && $attribute->isLocalizable()) {
         foreach ($this->getLocales() as $locale) {
             foreach ($keys as $baseKey) {
                 $key = $baseKey . '-' . $locale->getCode();
                 $updatedKeys[] = $key;
             }
         }
         $keys = $updatedKeys;
     }
     switch ($attribute->getBackendType()) {
         case 'prices':
             $updatedKeys = [];
             foreach ($keys as $key) {
                 foreach ($this->getCurrencies() as $currency) {
                     $updatedKeys[] = $key . '-' . $currency->getCode();
                 }
             }
             $keys = $updatedKeys;
             break;
         case 'metric':
             $updatedKeys = [];
             foreach ($keys as $key) {
                 $updatedKeys[] = $key;
                 $updatedKeys[] = $key . '-' . self::METRIC_UNIT;
             }
             $keys = $updatedKeys;
             break;
     }
     return $keys;
 }
 function it_normalizes_product_with_a_multiselect_value($filter, $serializer, ProductInterface $product, AbstractAttribute $skuAttribute, AbstractAttribute $colorsAttribute, AbstractProductValue $sku, AbstractProductValue $colors, AttributeOption $red, AttributeOption $blue, Collection $values, Family $family)
 {
     $family->getCode()->willReturn('shoes');
     $skuAttribute->getCode()->willReturn('sku');
     $skuAttribute->getAttributeType()->willReturn('pim_catalog_identifier');
     $skuAttribute->isLocalizable()->willReturn(false);
     $skuAttribute->isScopable()->willReturn(false);
     $sku->getAttribute()->willReturn($skuAttribute);
     $sku->getData()->willReturn('sku-001');
     $colorsAttribute->getCode()->willReturn('colors');
     $colorsAttribute->isLocalizable()->willReturn(false);
     $colorsAttribute->isScopable()->willReturn(false);
     $colors->getAttribute()->willReturn($colorsAttribute);
     $colors->getData()->willReturn([$red, $blue]);
     $product->getIdentifier()->willReturn($sku);
     $product->getFamily()->willReturn($family);
     $product->isEnabled()->willReturn(true);
     $product->getGroupCodes()->willReturn('');
     $product->getCategoryCodes()->willReturn('');
     $product->getAssociations()->willReturn([]);
     $product->getValues()->willReturn($values);
     $filter->filter($values, ['identifier' => $sku, 'scopeCode' => null, 'localeCodes' => []])->willReturn([$sku, $colors]);
     $serializer->normalize($sku, 'flat', Argument::any())->willReturn(['sku' => 'sku-001']);
     $serializer->normalize($colors, 'flat', Argument::any())->willReturn(['colors' => 'red, blue']);
     $this->normalize($product, 'flat', [])->shouldReturn(['sku' => 'sku-001', 'family' => 'shoes', 'groups' => '', 'categories' => '', 'colors' => 'red, blue', 'enabled' => 1]);
 }
 /**
  * Does the attribute scope match with attributeScope on prestashop ?
  *
  * @param AbstractAttribute $attribute
  * @param string            $attributeScope
  *
  * @return boolean
  */
 protected function scopeMatches(AbstractAttribute $attribute, $attributeScope)
 {
     return $attributeScope !== self::GLOBAL_SCOPE && $attribute->isLocalizable() || $attributeScope === self::GLOBAL_SCOPE && !$attribute->isLocalizable();
 }
 /**
  * Get normalized default value for attribute
  * @param AbstractAttribute $attribute
  * @param string            $defaultLocale
  * @param array             $magentoAttributes
  * @param array             $magentoAttributesOptions
  * @param MappingCollection $attributeMapping
  *
  * @return string
  */
 protected function getNormalizedDefaultValue(AbstractAttribute $attribute, $defaultLocale, array $magentoAttributes, array $magentoAttributesOptions, MappingCollection $attributeMapping)
 {
     $attributeCode = strtolower($attributeMapping->getTarget($attribute->getCode()));
     $context = ['identifier' => null, 'scopeCode' => null, 'localeCode' => $defaultLocale, 'onlyLocalized' => false, 'magentoAttributes' => [$attributeCode => ['scope' => !$attribute->isLocalizable() ? ProductValueNormalizer::GLOBAL_SCOPE : '']], 'magentoAttributesOptions' => $magentoAttributesOptions, 'attributeCodeMapping' => $attributeMapping, 'currencyCode' => ''];
     if ($attribute->getDefaultValue() instanceof ProductValueInterface) {
         return reset($this->productValueNormalizer->normalize($attribute->getDefaultValue(), 'MagentoArray', $context));
     } elseif ($attribute->getDefaultValue() instanceof AttributeOption) {
         $productValue = $this->productValueManager->createProductValueForDefaultOption($attribute);
         $normalizedOption = $this->productValueNormalizer->normalize($productValue, 'MagentoArray', $context);
         return null != $normalizedOption ? reset($normalizedOption) : null;
     } else {
         return null !== $attribute->getDefaultValue() ? (string) $attribute->getDefaultValue() : '';
     }
 }
 /**
  * Get the name of a normalized data field
  *
  * @param AbstractAttribute $attribute
  * @param Channel           $channel
  * @param Locale            $locale
  *
  * @return string
  */
 protected function getNormalizedFieldName(AbstractAttribute $attribute, Channel $channel, Locale $locale)
 {
     $suffix = '';
     if ($attribute->isLocalizable()) {
         $suffix = sprintf('-%s', $locale->getCode());
     }
     if ($attribute->isScopable()) {
         $suffix .= sprintf('-%s', $channel->getCode());
     }
     return $attribute->getCode() . $suffix;
 }
 /**
  * Normalize the field name from attribute and catalog context
  *
  * @param AbstractAttribute $attribute
  * @param CatalogContext    $context
  *
  * @return string
  */
 public static function getNormalizedValueFieldFromAttribute(AbstractAttribute $attribute, CatalogContext $context)
 {
     return self::getNormalizedValueField($attribute->getCode(), $attribute->isLocalizable(), $attribute->isScopable(), $attribute->isLocalizable() ? $context->getLocaleCode() : null, $attribute->isScopable() ? $context->getScopeCode() : null);
 }
 function it_generates_attribute_indexes_when_saving_filterable_scopable_and_localizable_attribute($collection, $namingUtility, AbstractAttribute $description)
 {
     $description->getCode()->willReturn('description');
     $description->getBackendType()->willReturn('varchar');
     $description->isLocalizable()->willReturn(true);
     $description->isScopable()->willReturn(true);
     $description->isUseableAsGridFilter()->willReturn(true);
     $description->getAttributeType()->willReturn('pim_catalog_simpleselect');
     $namingUtility->getAttributeNormFields($description)->willReturn(['normalizedData.description-en_US-ecommerce', 'normalizedData.description-de_DE-ecommerce', 'normalizedData.description-en_US-mobile']);
     $this->ensureIndexesFromAttribute($description);
 }
 /**
  * Get normalized scope for attribute.
  *
  * @param AbstractAttribute $attribute
  *
  * @return string
  */
 protected function getNormalizedScope(AbstractAttribute $attribute)
 {
     return $attribute->isLocalizable() ? self::STORE_SCOPE : self::GLOBAL_SCOPE;
 }
 /**
  * @param AbstractAttribute $attribute
  *
  * @return boolean
  */
 protected function isLocalizable(AbstractAttribute $attribute = null)
 {
     return $attribute && $attribute->isLocalizable();
 }
 /**
  * @param AbstractAttribute $attribute
  * @param string            $locale
  * @param string            $scope
  *
  * @return string
  */
 protected function getValueCode(AbstractAttribute $attribute, $locale, $scope)
 {
     $valueCode = $attribute->getCode();
     if ($attribute->isLocalizable()) {
         $valueCode .= '_' . $locale;
     }
     if ($attribute->isScopable()) {
         $valueCode .= '_' . $scope;
     }
     return $valueCode;
 }