/** * Compare if the value and type is equal * * @param Zend_Measure_Weight $object Weight object to compare * @return boolean */ public function equals($object) { if ($object->toString() == $this->toString()) { return true; } return false; }
/** * test toString * expected string */ public function testWeightToString() { $value = new Zend_Measure_Weight('-100', Zend_Measure_Weight::STANDARD, 'de'); $this->assertEquals($value->toString(), '-100 kg', 'Value -100 kg expected'); }
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')); }