/**
  * {@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()]);
         }
     }
 }
 /**
  * {@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;
 }
 /**
  * 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;
 }