/**
  * Convert weight in different measure types
  *
  * @param  mixed $value
  * @param  string $sourceWeightMeasure
  * @param  string $toWeightMeasure
  * @return int|null|string
  */
 public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure)
 {
     if ($value) {
         $locale = Mage::app()->getLocale()->getLocale();
         $unitWeight = new Zend_Measure_Weight($value, $sourceWeightMeasure, $locale);
         $unitWeight->setType($toWeightMeasure);
         return $unitWeight->getValue();
     }
     return null;
 }
 /**
  * Convert weight in different measure types
  *
  * @param int|float $value
  * @param string $sourceWeightMeasure
  * @param string $toWeightMeasure
  * @return int|null|string
  */
 public function convertMeasureWeight($value, $sourceWeightMeasure, $toWeightMeasure)
 {
     if ($value) {
         $locale = $this->localeResolver->getLocale();
         $unitWeight = new \Zend_Measure_Weight($value, $sourceWeightMeasure, $locale);
         $unitWeight->setType($toWeightMeasure);
         return $unitWeight->getValue();
     }
     return null;
 }
 /**
  * test for exception unknown locale
  * expected exception
  */
 public function testMeasureSetWithNoLocale()
 {
     $value = new Zend_Measure_Weight('100', Zend_Measure_Weight::STANDARD, 'de');
     $value->setValue('200', Zend_Measure_Weight::STANDARD);
     $this->assertEquals(200, $value->getValue(), 'Zend_Measure_Weight value expected to be a positive integer');
 }
/**
 * Parse weight dimension
 * @param string $ps_value value to parse
 * @param null|array $pa_options options array
 * @return bool|null|Zend_Measure_Weight
 * @throws Exception
 */
function caParseWeightDimension($ps_value, $pa_options = null)
{
    global $g_ui_locale;
    $vs_locale = caGetOption('locale', $pa_options, $g_ui_locale);
    $pa_values = array(caConvertFractionalNumberToDecimal(trim($ps_value), $vs_locale));
    $vo_parsed_measurement = null;
    while ($vs_expression = array_shift($pa_values)) {
        // parse units of measurement
        if (preg_match("!^([\\d\\.\\,/ ]+)[ ]*([^\\d ]+)!", $vs_expression, $va_matches)) {
            $vs_value = trim($va_matches[1]);
            $va_values = explode(" ", $vs_value);
            if ($vs_expression = trim(str_replace($va_matches[0], '', $vs_expression))) {
                array_unshift($pa_values, $vs_expression);
            }
            $vs_value = 0;
            foreach ($va_values as $vs_v) {
                $vs_value += caConvertLocaleSpecificFloat(trim($vs_v), $vs_locale);
            }
            switch (strtolower($va_matches[2])) {
                case "lbs":
                case 'lbs.':
                case 'lb':
                case 'lb.':
                case 'pound':
                case 'pounds':
                    $vs_units = Zend_Measure_Weight::POUND;
                    break;
                case 'kg':
                case 'kg.':
                case 'kilo':
                case 'kilos':
                case 'kilogram':
                case 'kilograms':
                    $vs_units = Zend_Measure_Weight::KILOGRAM;
                    break;
                case 'g':
                case 'g.':
                case 'gr':
                case 'gr.':
                case 'gram':
                case 'grams':
                    $vs_units = Zend_Measure_Weight::GRAM;
                    break;
                case 'mg':
                case 'mg.':
                case 'milligram':
                case 'milligrams':
                    $vs_units = Zend_Measure_Weight::MILLIGRAM;
                    break;
                case 'oz':
                case 'oz.':
                case 'ounce':
                case 'ounces':
                    $vs_units = Zend_Measure_Weight::OUNCE;
                    break;
                case 'ton':
                case 'tons':
                case 'tonne':
                case 'tonnes':
                case 't':
                case 't.':
                    $vs_units = Zend_Measure_Weight::TON;
                    break;
                case 'stone':
                    $vs_units = Zend_Measure_Weight::STONE;
                    break;
                default:
                    throw new Exception(_t('Not a valid unit of weight [%2]', $ps_value));
                    break;
            }
            try {
                $o_tmp = new Zend_Measure_Weight($vs_value, $vs_units, $vs_locale);
            } catch (Exception $e) {
                throw new Exception(_t('Not a valid measurement'));
            }
            if ($o_tmp->getValue() < 0) {
                // weight can't be negative in our universe
                throw new Exception(_t('Must not be less than zero'));
            }
            if ($vo_parsed_measurement) {
                $vo_parsed_measurement = $vo_parsed_measurement->add($o_tmp);
            } else {
                $vo_parsed_measurement = $o_tmp;
            }
        }
    }
    if (!$vo_parsed_measurement) {
        throw new Exception(_t('Not a valid measurement [%1]', $ps_value));
    }
    return $vo_parsed_measurement;
}
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     $ps_value = trim($ps_value);
     global $g_ui_locale;
     $va_settings = $this->getSettingValuesFromElementArray($pa_element_info, array('requireValue'));
     if (!$va_settings['requireValue'] && !trim($ps_value)) {
         return array('value_longtext1' => '', 'value_longtext2' => '', 'value_decimal1' => '');
     }
     // parse units of measurement
     if (preg_match("!^([\\d\\.\\,/ ]+)[ ]*([^\\d ]+)!", $ps_value, $va_matches)) {
         $va_values = explode(" ", $va_matches[1]);
         $vs_value = 0;
         foreach ($va_values as $vs_v) {
             $vs_value += caConvertFractionalNumberToDecimal(trim($vs_v));
         }
         switch (strtolower($va_matches[2])) {
             case "lbs":
             case 'lbs.':
             case 'lb':
             case 'lb.':
             case 'pound':
             case 'pounds':
                 $vs_units = Zend_Measure_Weight::POUND;
                 break;
             case 'kg':
             case 'kg.':
             case 'kilo':
             case 'kilos':
             case 'kilogram':
             case 'kilograms':
                 $vs_units = Zend_Measure_Weight::KILOGRAM;
                 break;
             case 'g':
             case 'g.':
             case 'gr':
             case 'gr.':
             case 'gram':
             case 'grams':
                 $vs_units = Zend_Measure_Weight::GRAM;
                 break;
             case 'mg':
             case 'mg.':
             case 'milligram':
             case 'milligrams':
                 $vs_units = Zend_Measure_Weight::MILLIGRAM;
                 break;
             case 'oz':
             case 'oz.':
             case 'ounce':
             case 'ounces':
                 $vs_units = Zend_Measure_Weight::OUNCE;
                 break;
             case 'ton':
             case 'tons':
             case 'tonne':
             case 'tonnes':
             case 't':
             case 't.':
                 $vs_units = Zend_Measure_Weight::TON;
                 break;
             case 'stone':
                 $vs_units = Zend_Measure_Weight::STONE;
                 break;
             default:
                 $this->postError(1970, _t('%1 is not a valid unit of weight [%2]', $va_matches[2], $ps_value), 'WeightAttributeValue->parseValue()');
                 return false;
                 break;
         }
     } else {
         $this->postError(1970, _t('%1 is not a valid measurement', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()');
         return false;
     }
     try {
         $vo_parsed_measurement = new Zend_Measure_Weight($vs_value, $vs_units, $g_ui_locale);
     } catch (Exception $e) {
         $this->postError(1970, _t('%1 is not a valid measurement', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()');
         return false;
     }
     if ($vo_parsed_measurement->getValue() < 0) {
         // Weight can't be negative in our universe
         // (at least I believe in *something*)
         $this->postError(1970, _t('%1 must not be less than zero', $pa_element_info['displayLabel']), 'WeightAttributeValue->parseValue()');
         return false;
     }
     return array('value_longtext1' => $vo_parsed_measurement->toString(), 'value_longtext2' => $vs_units, 'value_decimal1' => $vo_parsed_measurement->convertTo('KILOGRAM', 6, 'en_US'));
 }
 /**
  * @param $attributeType
  * @param $value
  *
  * @return int|string
  */
 public function getAttributesMappingUnitConversion($attributeType, $value)
 {
     $this->_getConfigAttributeMapped($attributeType);
     if ($attributeType == 'weight') {
         //check if needs conversion
         if ($this->_mapping[$attributeType]['unit'] != self::ME_WEIGHT_UNIT) {
             $unit = new \Zend_Measure_Weight((double) $value);
             $unit->convertTo(\Zend_Measure_Weight::GRAM);
             return $unit->getValue();
         }
     } elseif ($this->_mapping[$attributeType]['unit'] != self::ME_LENGTH_UNIT) {
         $unit = new \Zend_Measure_Length((double) $value);
         $unit->convertTo(\Zend_Measure_Length::CENTIMETER);
         return $unit->getValue();
     }
     return $value;
 }