/** * Resolve reference data IDs from codes * * @param string $referenceData reference data name * @param mixed $codes a code or an array of code * * @return mixed, a reference data ID or an array of reference data IDs */ public function resolve($referenceData, $codes) { $repository = $this->repositoryResolver->resolve($referenceData); if (is_array($codes)) { $ids = []; foreach ($codes as $code) { $ids[] = $this->resolveOne($repository, $referenceData, $code); } return $ids; } return $this->resolveOne($repository, $referenceData, $codes); }
/** * {@inheritdoc} */ public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'reference data collection'); $this->checkData($attribute, $data); $referenceDataCollection = []; $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); foreach ($data as $referenceDataCode) { $referenceData = $repository->findOneBy(['code' => $referenceDataCode]); if (null === $referenceData) { throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'code', sprintf('No reference data "%s" with code "%s" has been found', $attribute->getReferenceDataName(), $referenceDataCode), 'setter', 'reference data collection', $referenceDataCode); } $referenceDataCollection[] = $referenceData; } $this->setReferenceDataCollection($attribute, $product, $referenceDataCollection, $options['locale'], $options['scope']); }
/** * {@inheritdoc} */ public function denormalize($data, $referenceDataClass, $format = null, array $context = []) { if (null === $this->repositoryResolver || empty($data)) { return null; } if (!$context['value'] instanceof ProductValueInterface) { throw new \InvalidArgumentException('Value is not an instance of Pim\\Component\\Catalog\\Model\\ProductValueInterface.'); } $attribute = $context['value']->getAttribute(); if (null === $attribute) { throw new \InvalidArgumentException('Denormalizer\'s context expected to have an attribute, none found.'); } $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); return $repository->findOneBy(['code' => $data]); }
/** * {@inheritdoc} */ public function setAttributeData(ProductInterface $product, AttributeInterface $attribute, $data, array $options = []) { $this->checkLocaleAndScope($attribute, $options['locale'], $options['scope'], 'reference data'); $this->checkData($attribute, $data); if (empty($data)) { $referenceData = null; } else { $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); $referenceData = $repository->findOneBy(['code' => $data]); if (null === $referenceData) { throw InvalidArgumentException::validEntityCodeExpected($attribute->getCode(), 'code', sprintf('No reference data "%s" with code "%s" has been found', $attribute->getReferenceDataName(), $data), 'setter', 'reference data', $data); } } $this->setReferenceData($attribute, $product, $referenceData, $options['locale'], $options['scope']); }
/** * {@inheritdoc} */ public function denormalize($data, $referenceDataClass, $format = null, array $context = array()) { if (null === $this->repositoryResolver || empty($data)) { return null; } if (false === isset($context['attribute'])) { throw new InvalidParameterException(sprintf('Denormalizer\'s context expected to have an attribute, none found.')); } $attribute = $context['attribute']; if (!$attribute instanceof AttributeInterface) { throw new InvalidParameterException(sprintf('Attribute is not an instance of Pim\\Bundle\\CatalogBundle\\Model\\AttributeInterface.')); } $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); $referenceData = $repository->findOneBy(['code' => $data]); return $referenceData; }
/** * {@inheritdoc} */ public function denormalize($data, $referenceDataClass, $format = null, array $context = []) { $collection = new ArrayCollection(); if (null === $this->repositoryResolver || empty($data)) { return $collection; } if (!$context['value'] instanceof ProductValueInterface) { throw new \InvalidArgumentException('Value is not an instance of Pim\\Component\\Catalog\\Model\\ProductValueInterface.'); } $attribute = $context['value']->getAttribute(); if (null === $attribute) { throw new \InvalidArgumentException('Denormalizer\'s context expected to have an attribute, none found.'); } $repository = $this->repositoryResolver->resolve($attribute->getReferenceDataName()); $codes = explode(',', $data); foreach ($codes as $code) { $referenceData = $repository->findOneBy(['code' => trim($code)]); if (null !== $referenceData) { $collection->add($referenceData); } } return $collection; }