/**
  * Set reference data into the product value
  *
  * @param AttributeInterface          $attribute
  * @param ProductInterface            $product
  * @param ReferenceDataInterface|null $referenceData
  * @param string|null                 $locale
  * @param string|null                 $scope
  *
  * @throws \LogicException
  */
 protected function setReferenceData(AttributeInterface $attribute, ProductInterface $product, $referenceData = null, $locale = null, $scope = null)
 {
     $value = $product->getValue($attribute->getCode(), $locale, $scope);
     if (null === $value) {
         $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope);
     }
     $setMethod = MethodNameGuesser::guess('set', $attribute->getReferenceDataName(), true);
     if (!method_exists($value, $setMethod)) {
         throw new \LogicException(sprintf('ProductValue method "%s" is not implemented', $setMethod));
     }
     $value->{$setMethod}($referenceData);
 }
Ejemplo n.º 2
0
 /**
  * @param ReferenceDataInterface $referenceData
  * @param bool                   $fallbackOnCode
  *
  * @return string|null
  */
 public function render(ReferenceDataInterface $referenceData, $fallbackOnCode = true)
 {
     if (null !== ($labelProperty = $referenceData::getLabelProperty())) {
         $getter = MethodNameGuesser::guess('get', $labelProperty);
         $label = $referenceData->{$getter}();
         if (!empty($label)) {
             return $label;
         }
     }
     if ($fallbackOnCode) {
         return sprintf('[%s]', $referenceData->getCode());
     }
     return null;
 }
 /**
  * @param string $referenceData
  *
  * @return array
  */
 protected function getRequiredAccessorForMultipleReferenceData($referenceData)
 {
     $accessors = [];
     $accessors[] = MethodNameGuesser::guess('add', $referenceData, true);
     $accessors[] = MethodNameGuesser::guess('remove', $referenceData, true);
     return $accessors;
 }
 /**
  * Set reference data collection into the product value
  *
  * @param AttributeInterface $attribute
  * @param ProductInterface   $product
  * @param array              $referenceDataCollection
  * @param string|null        $locale
  * @param string|null        $scope
  *
  * @throws \LogicException
  */
 protected function setReferenceDataCollection(AttributeInterface $attribute, ProductInterface $product, array $referenceDataCollection, $locale = null, $scope = null)
 {
     $value = $product->getValue($attribute->getCode(), $locale, $scope);
     if (null === $value) {
         $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope);
     }
     $referenceDataName = $attribute->getReferenceDataName();
     $addMethod = MethodNameGuesser::guess('add', $referenceDataName, true);
     $removeMethod = MethodNameGuesser::guess('remove', $referenceDataName, true);
     $getMethod = MethodNameGuesser::guess('get', $referenceDataName);
     if (!method_exists($value, $addMethod) || !method_exists($value, $removeMethod) || !method_exists($value, $getMethod)) {
         throw new \LogicException(sprintf('One of these methods is not implemented in %s: "%s", "%s", "%s"', ClassUtils::getClass($value), $addMethod, $removeMethod, $getMethod));
     }
     $currentCollection = $value->{$getMethod}();
     foreach ($currentCollection as $currentReferenceData) {
         $value->{$removeMethod}($currentReferenceData);
     }
     foreach ($referenceDataCollection as $referenceData) {
         $value->{$addMethod}($referenceData);
     }
 }
 /**
  * @param ProductValueInterface $value
  * @param AttributeInterface    $attribute
  * @param string                $type
  *
  * @return string
  */
 private function getValueMethodName(ProductValueInterface $value, AttributeInterface $attribute, $type)
 {
     $method = MethodNameGuesser::guess($type, $attribute->getReferenceDataName(), true);
     if (!method_exists($value, $method)) {
         throw new \LogicException(sprintf('ProductValue method "%s" is not implemented', $method));
     }
     return $method;
 }