/**
  * {@inheritdoc}
  */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
     $metricAttributes = $this->entityManager->getRepository($this->attributeClass)->findBy(['attributeType' => AttributeTypes::METRIC]);
     foreach ($metricAttributes as $attribute) {
         if ($units = $this->measureManager->getUnitSymbolsForFamily($attribute->getMetricFamily())) {
             $builder->add($attribute->getCode(), 'choice', ['choices' => array_combine(array_keys($units), array_keys($units)), 'empty_value' => 'Do not convert', 'required' => false, 'select2' => true, 'label' => $attribute->getLabel()]);
         }
     }
 }
 /**
  * Allow to create convert data in standard unit for metrics
  *
  * @param AbstractMetric $metric
  */
 protected function createMetricBaseValues(AbstractMetric $metric)
 {
     $baseUnit = $this->manager->getStandardUnitForFamily($metric->getFamily());
     if (is_numeric($metric->getData())) {
         $baseData = $this->converter->setFamily($metric->getFamily())->convertBaseToStandard($metric->getUnit(), $metric->getData());
     } else {
         $baseData = null;
     }
     $metric->setBaseData($baseData)->setBaseUnit($baseUnit);
 }
 /**
  * {@inheritdoc}
  */
 public function prepareValueFormOptions(ProductValueInterface $value)
 {
     $unitsList = $this->manager->getUnitSymbolsForFamily($value->getAttribute()->getMetricFamily());
     $unitNames = array_combine(array_keys($unitsList), array_keys($unitsList));
     $units = array_map(function ($unit) {
         return $this->translator->trans($unit, [], 'measures');
     }, $unitNames);
     $options = array_merge(parent::prepareValueFormOptions($value), array('units' => $units, 'default_unit' => $value->getAttribute()->getDefaultMetricUnit(), 'family' => $value->getAttribute()->getMetricFamily()));
     $options['default_unit'] = array($options['default_unit']);
     return $options;
 }
 function it_converts_metric_data_before_updating(LifecycleEventArgs $args, AbstractMetric $metric, MeasureManager $manager, MeasureConverter $converter)
 {
     $args->getObject()->willReturn($metric);
     $metric->getUnit()->willReturn('cm');
     $metric->getFamily()->willReturn('distance');
     $metric->getData()->willReturn(100);
     $manager->getStandardUnitForFamily('distance')->willReturn('m');
     $converter->setFamily('distance')->shouldBeCalled()->willReturn($converter);
     $converter->convertBaseToStandard('cm', 100)->willReturn(1);
     $metric->setBaseData(1)->shouldBeCalled()->willReturn($metric);
     $metric->setBaseUnit('m')->shouldBeCalled();
     $this->preUpdate($args);
 }
 function it_converts_metric_data_before_updating(LifecycleEventArgs $args, MetricInterface $metric, MeasureManager $manager, MeasureConverter $converter, DocumentManager $dm, UnitOfWork $uow)
 {
     $args->getObject()->willReturn($metric);
     $args->getObjectManager()->willReturn($dm);
     $dm->getUnitOfWork()->willReturn($uow);
     $uow->recomputeSingleDocumentChangeSet(Argument::type('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata'), $metric)->shouldBeCalled();
     $metric->getUnit()->willReturn('cm');
     $metric->getFamily()->willReturn('distance');
     $metric->getData()->willReturn(100);
     $manager->getStandardUnitForFamily('distance')->willReturn('m');
     $converter->setFamily('distance')->shouldBeCalled()->willReturn($converter);
     $converter->convertBaseToStandard('cm', 100)->willReturn(1);
     $metric->setBaseData(1)->shouldBeCalled()->willReturn($metric);
     $metric->setBaseUnit('m')->shouldBeCalled();
     $this->preUpdate($args);
 }
 /**
  * Check if data is valid
  *
  * @param AttributeInterface $attribute
  * @param mixed              $data
  */
 protected function checkData(AttributeInterface $attribute, $data)
 {
     if (!is_array($data)) {
         throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'setter', 'metric', gettype($data));
     }
     if (!array_key_exists('data', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'setter', 'metric', print_r($data, true));
     }
     if (!array_key_exists('unit', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'unit', 'setter', 'metric', print_r($data, true));
     }
     if (!is_string($data['unit'])) {
         throw InvalidArgumentException::arrayStringValueExpected($attribute->getCode(), 'unit', 'setter', 'metric', $data['unit']);
     }
     if (!array_key_exists($data['unit'], $this->measureManager->getUnitSymbolsForFamily($attribute->getMetricFamily()))) {
         throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'unit', 'The unit does not exist', 'setter', 'metric', $data['unit']);
     }
 }
 /**
  * Check if value is valid
  *
  * @param AttributeInterface $attribute
  * @param mixed              $data
  */
 protected function checkValue(AttributeInterface $attribute, $data)
 {
     if (!is_array($data)) {
         throw InvalidArgumentException::arrayExpected($attribute->getCode(), 'filter', 'metric', gettype($data));
     }
     if (!array_key_exists('data', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'data', 'filter', 'metric', print_r($data, true));
     }
     if (!array_key_exists('unit', $data)) {
         throw InvalidArgumentException::arrayKeyExpected($attribute->getCode(), 'unit', 'filter', 'metric', print_r($data, true));
     }
     if (!is_numeric($data['data']) && null !== $data['data']) {
         throw InvalidArgumentException::arrayNumericKeyExpected($attribute->getCode(), 'data', 'filter', 'metric', gettype($data['data']));
     }
     if (!is_string($data['unit'])) {
         throw InvalidArgumentException::arrayStringKeyExpected($attribute->getCode(), 'unit', 'filter', 'metric', gettype($data['unit']));
     }
     if (!array_key_exists($data['unit'], $this->measureManager->getUnitSymbolsForFamily($attribute->getMetricFamily()))) {
         throw InvalidArgumentException::arrayInvalidKey($attribute->getCode(), 'unit', sprintf('The unit does not exist in the attribute\'s family "%s"', $attribute->getMetricFamily()), 'filter', 'metric', $data['unit']);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getMetadata()
 {
     $metadata = parent::getMetadata();
     $metadata['units'] = $this->measureManager->getUnitSymbolsForFamily($this->family);
     return $metadata;
 }
 /**
  * {@inheritdoc}
  */
 public function prepareValueFormOptions(ProductValueInterface $value)
 {
     $options = array_merge(parent::prepareValueFormOptions($value), array('units' => $this->manager->getUnitSymbolsForFamily($value->getAttribute()->getMetricFamily()), 'default_unit' => $value->getAttribute()->getDefaultMetricUnit(), 'family' => $value->getAttribute()->getMetricFamily()));
     $options['default_unit'] = array($options['default_unit']);
     return $options;
 }