/**
  * {@inheritdoc}
  */
 public function prepareValueFormData(ProductValueInterface $value)
 {
     if (!is_null($value->getData())) {
         return $value->getData();
     }
     return $this->metricFactory->createMetric($value->getAttribute()->getMetricFamily());
 }
 /**
  * {@inheritdoc}
  */
 public function denormalize($data, $class, $format = null, array $context = [])
 {
     if (empty($data)) {
         return null;
     }
     $metric = $this->factory->createMetric($context['attribute']->getMetricFamily());
     $metric->setData($data['data']);
     $metric->setUnit($data['unit']);
     return $metric;
 }
 /**
  * The metric is built by many ordered calls, one for the data column, one for the unit column
  *
  * @param ProductValueInterface $value
  * @param string                $dataOrUnit
  *
  * @return \Pim\Bundle\CatalogBundle\Model\MetricInterface
  */
 protected function addFromManyFields(ProductValueInterface $value, $dataOrUnit)
 {
     // TODO come from original implementation, really FRAGIL because depends on many ordered calls
     if (null === ($metric = $value->getMetric())) {
         $metric = $this->factory->createMetric($value->getAttribute()->getMetricFamily());
         $metric->setData($dataOrUnit);
     } else {
         $metric->setUnit($dataOrUnit);
     }
     return $metric;
 }
 /**
  * @param ProductInterface   $product
  * @param AttributeInterface $fromAttribute
  * @param AttributeInterface $toAttribute
  * @param string             $fromLocale
  * @param string             $toLocale
  * @param string             $fromScope
  * @param string             $toScope
  */
 protected function copySingleValue(ProductInterface $product, AttributeInterface $fromAttribute, AttributeInterface $toAttribute, $fromLocale, $toLocale, $fromScope, $toScope)
 {
     $fromValue = $product->getValue($fromAttribute->getCode(), $fromLocale, $fromScope);
     if (null !== $fromValue) {
         $fromData = $fromValue->getData();
         $toValue = $product->getValue($toAttribute->getCode(), $toLocale, $toScope);
         if (null === $toValue) {
             $toValue = $this->productBuilder->addProductValue($product, $toAttribute, $toLocale, $toScope);
         }
         if (null === ($metric = $toValue->getMetric())) {
             $metric = $this->metricFactory->createMetric($fromData->getFamily());
         }
         $metric->setUnit($fromData->getUnit());
         $metric->setData($fromData->getData());
         $toValue->setMetric($metric);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function setValue($object, ColumnInfoInterface $columnInfo, $data, array $options = array())
 {
     $suffixes = $columnInfo->getSuffixes();
     $suffix = array_pop($suffixes);
     if (!$object->getMetric()) {
         $metric = $this->metricFactory->createMetric($columnInfo->getAttribute()->getMetricFamily());
         $object->setMetric($metric);
     }
     if ('unit' === $suffix) {
         $object->getMetric()->setUnit($data);
     } else {
         $parts = preg_split('/\\s+/', $data);
         $object->getMetric()->setData($parts[0] === '' ? null : $parts[0]);
         if (isset($parts[1])) {
             $object->getMetric()->setUnit($parts[1]);
         }
     }
 }
 /**
  * @param ProductValueInterface $productValue
  * @param ProductValueInterface $value
  */
 protected function setProductMetric(ProductValueInterface $productValue, ProductValueInterface $value)
 {
     if (null === ($metric = $productValue->getMetric())) {
         $metric = $this->metricFactory->createMetric($value->getAttribute()->getMetricFamily());
         $productValue->setMetric($metric);
     }
     $metric->setUnit($value->getMetric()->getUnit());
     $metric->setData($value->getMetric()->getData());
 }
 /**
  * Set the data into the product value
  *
  * @param ProductInterface   $product
  * @param AttributeInterface $attribute
  * @param mixed              $data
  * @param string             $unit
  * @param string             $locale
  * @param string             $scope
  */
 protected function setData(ProductInterface $product, AttributeInterface $attribute, $data, $unit, $locale, $scope)
 {
     $value = $product->getValue($attribute->getCode(), $locale, $scope);
     if (null === $value) {
         $value = $this->productBuilder->addProductValue($product, $attribute, $locale, $scope);
     }
     if (null === ($metric = $value->getMetric())) {
         $metric = $this->metricFactory->createMetric($attribute->getMetricFamily());
     }
     $value->setMetric($metric);
     $metric->setUnit($unit);
     $metric->setData($data);
 }