Documentation

Manipulating Measurements - Zend_Measure

Manipulating Measurements

Parsing and normalization of input, combined with output to localized notations makes data accessible to users in different locales. Many additional methods exist in Zend_Measure_* components to manipulate and work with this data, after it has been normalized.

Convert

Probably the most important feature is the conversion into different units of measurement. The conversion of a unit can be done any number of times using the method convertTo(). Units of measurement can only be converted to other units of the same type (class). Therefore, it is not possible to convert (e.g.) a length into a weight, which would might encourage poor programming practices and allow errors to propagate without exceptions.

The convertTo() method accepts an optional parameter. With this parameter you can define an precision for the returned output. The standard precision is '2'.

Example #1 Convert

  1. $locale = new Zend_Locale('de');
  2. $mystring = "1.234.567,89";
  3. $unit = new Zend_Measure_Weight($mystring,'POND', $locale);
  4.  
  5. print "Kilo:".$unit->convertTo('KILOGRAM');
  6.  
  7. // constants are considered "better practice" than strings
  8. print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON);
  9.  
  10. // define a precision for the output
  11. print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON, 3);

Add and subtract

Measurements can be added together using add() and subtracted using sub(). The result will use the same type as the originating object. Dynamic objects support a fluid style of programming, where complex sequences of operations can be nested without risk of side-effects altering the input objects.

Example #2 Adding units

  1. // Define objects
  2. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  3. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  4.  
  5. // Add $unit2 to $unit
  6. $sum = $unit->add($unit2);
  7.  
  8. echo $sum; // outputs "300 cm"

Note: Automatic conversion
Adding one object to another will automatically convert it to the correct unit. It is not necessary to call convertTo() before adding different units.

Example #3 Subtract

Subtraction of measurements works just like addition.

  1. // Define objects
  2. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  3. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  4.  
  5. // Subtract $unit2 from $unit
  6. $sum = $unit->sub($unit2);
  7.  
  8. echo $sum;

Compare

Measurements can also be compared, but without automatic unit conversion. Thus, equals() returns TRUE, only if both the value and the unit of measure are identical.

Example #4 Different measurements

  1. // Define measurements
  2. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  3. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  4.  
  5. if ($unit->equals($unit2)) {
  6.     print "Both measurements are identical";
  7. } else {
  8.     print "These are different measurements";
  9. }

Example #5 Identical measurements

  1. // Define measurements
  2. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  3. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  4.  
  5. $unit2->setType(Zend_Measure_Length::CENTIMETER);
  6.  
  7. if ($unit->equals($unit2)) {
  8.     print "Both measurements are identical";
  9. } else {
  10.     print "These are different measurements";
  11. }

Compare

To determine if a measurement is less than or greater than another, use compare(), which returns 0, -1 or 1 depending on the difference between the two objects. Identical measurements will return 0. Lesser ones will return a negative, greater ones a positive value.

Example #6 Difference

  1. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  2. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  3. $unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);
  4.  
  5. print "Equal:".$unit2->compare($unit);
  6. print "Lesser:".$unit2->compare($unit3);
  7. print "Greater:".$unit3->compare($unit2);

Manually change values

To change the value of a measurement explicitly, use setValue(). to overwrite the current value. The parameters are the same as the constructor.

Example #7 Changing a value

  1. $locale = new Zend_Locale('de_AT');
  2. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  3.  
  4. $unit->setValue(1.2);
  5. echo $unit;
  6.  
  7. $unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
  8. echo $unit;
  9.  
  10. $unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
  11. echo $unit;

Manually change types

To change the type of a measurement without altering its value use setType().

Example #8 Changing the type

  1. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  2. echo $unit; // outputs "1 m"
  3.  
  4. $unit->setType(Zend_Measure_Length::KILOMETER);
  5. echo $unit; // outputs "1000 km"

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts