Exemplo n.º 1
0
 /**
  * Convert dimensions in different measure types
  *
  * @param  mixed $value
  * @param  string $sourceDimensionMeasure
  * @param  string $toDimensionMeasure
  * @return int|null|string
  */
 public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDimensionMeasure)
 {
     if ($value) {
         $locale = Mage::app()->getLocale()->getLocale();
         $unitDimension = new Zend_Measure_Length($value, $sourceDimensionMeasure, $locale);
         $unitDimension->setType($toDimensionMeasure);
         return $unitDimension->getValue();
     }
     return null;
 }
Exemplo n.º 2
0
 /**
  * test for exception unknown locale
  * expected exception
  */
 public function testLengthSetWithNoLocale()
 {
     $value = new Zend_Measure_Length('100', Zend_Measure_Length::STANDARD, 'de');
     $value->setValue('200', Zend_Measure_Length::STANDARD);
     $this->assertEquals(200, $value->getValue(), 'Zend_Measure_Length value expected to be a positive integer');
 }
Exemplo n.º 3
0
/**
 * Parse length dimension
 * @param string $ps_value
 * @param null|array $pa_options
 * @return bool|null|Zend_Measure_Length
 * @throws Exception
 */
function caParseLengthDimension($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);
            $vs_unit_expression = strtolower(trim($va_matches[2]));
            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);
            }
            if (!($vs_units = caGetLengthUnitType($vs_unit_expression))) {
                throw new Exception(_t('%1 is not a valid unit of length [%2]', $va_matches[2], $ps_value));
            }
            try {
                $o_tmp = new Zend_Measure_Length($vs_value, $vs_units, $vs_locale);
            } catch (Exception $e) {
                throw new Exception(_t('Not a valid measurement'));
            }
            if ($o_tmp->getValue() < 0) {
                // length can't be negative in our universe
                throw new Exception(_t('Must not be less than zero'));
                return false;
            }
            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;
}
Exemplo n.º 4
0
 /**
  * Convert dimensions in different measure types
  *
  * @param  int|float $value
  * @param  string $sourceDimensionMeasure
  * @param  string $toDimensionMeasure
  * @return int|null|string
  */
 public function convertMeasureDimension($value, $sourceDimensionMeasure, $toDimensionMeasure)
 {
     if ($value) {
         $locale = $this->localeResolver->getLocale();
         $unitDimension = new \Zend_Measure_Length($value, $sourceDimensionMeasure, $locale);
         $unitDimension->setType($toDimensionMeasure);
         return $unitDimension->getValue();
     }
     return null;
 }
Exemplo n.º 5
0
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     global $g_ui_locale;
     $ps_value = caConvertFractionalNumberToDecimal(trim($ps_value), $g_ui_locale);
     $va_settings = $this->getSettingValuesFromElementArray($pa_element_info, array('requireValue'));
     if (!$va_settings['requireValue'] && !$ps_value) {
         return array('value_longtext1' => '', 'value_longtext2' => '', 'value_decimal1' => '');
     }
     $vo_parsed_measurement = null;
     $pa_values = array($ps_value);
     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);
             $vs_unit_expression = strtolower(trim($va_matches[2]));
             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), $g_ui_locale);
             }
             switch ($vs_unit_expression) {
                 case "'":
                 case "’":
                 case 'ft':
                 case 'ft.':
                 case 'feet':
                 case 'foot':
                     $vs_units = Zend_Measure_Length::FEET;
                     break;
                 case '"':
                 case "”":
                 case 'in':
                 case 'in.':
                 case 'inch':
                 case 'inches':
                     $vs_units = Zend_Measure_Length::INCH;
                     break;
                 case 'm':
                 case 'm.':
                 case 'meter':
                 case 'meters':
                 case 'metre':
                 case 'metres':
                 case 'mt':
                     $vs_units = Zend_Measure_Length::METER;
                     break;
                 case 'cm':
                 case 'cm.':
                 case 'centimeter':
                 case 'centimeters':
                 case 'centimetre':
                 case 'centimetres':
                     $vs_units = Zend_Measure_Length::CENTIMETER;
                     break;
                 case 'mm':
                 case 'mm.':
                 case 'millimeter':
                 case 'millimeters':
                 case 'millimetre':
                 case 'millimetres':
                     $vs_units = Zend_Measure_Length::MILLIMETER;
                     break;
                 case 'point':
                 case 'pt':
                 case 'pt.':
                     $vs_units = Zend_Measure_Length::POINT;
                     break;
                 case 'mile':
                 case 'miles':
                     $vs_units = Zend_Measure_Length::MILE;
                     break;
                 case 'km':
                 case 'k':
                 case 'kilometer':
                 case 'kilometers':
                 case 'kilometre':
                 case 'kilometres':
                     $vs_units = Zend_Measure_Length::KILOMETER;
                     break;
                 default:
                     $this->postError(1970, _t('%1 is not a valid unit of length [%2]', $va_matches[2], $ps_value), 'LengthAttributeValue->parseValue()');
                     return false;
                     break;
             }
             try {
                 $o_tmp = new Zend_Measure_Length($vs_value, $vs_units, $g_ui_locale);
             } catch (Exception $e) {
                 $this->postError(1970, _t('%1 is not a valid measurement', $pa_element_info['displayLabel']), 'LengthAttributeValue->parseValue()');
                 return false;
             }
             if ($o_tmp->getValue() < 0) {
                 // length can't be negative in our universe
                 $this->postError(1970, _t('%1 must not be less than zero', $pa_element_info['displayLabel']), 'LengthAttributeValue->parseValue()');
                 return false;
             }
             if ($vo_parsed_measurement) {
                 $vo_parsed_measurement = $vo_parsed_measurement->add($o_tmp);
             } else {
                 $vo_parsed_measurement = $o_tmp;
             }
         }
     }
     if (!$vo_parsed_measurement) {
         $this->postError(1970, _t('%1 is not a valid measurement [%2]', $pa_element_info['displayLabel'], $ps_value), 'LengthAttributeValue->parseValue()');
         return false;
     }
     return array('value_longtext1' => $vo_parsed_measurement->toString(4), 'value_longtext2' => $vs_units, 'value_decimal1' => $vo_parsed_measurement->convertTo('METER', 6, 'en_US'));
 }
Exemplo n.º 6
0
 /**
  * @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;
 }