#native_company# #native_desc#
#native_cta#

Create a Unit Conversion Application with the Zend_Measure Component Page 2

By W. Jason Gilmore
on February 11, 2010

Performing the Conversion Calculation

With the calculator form in place, all that’s left to do is write the code, which takes advantage of Zend_Measure in order to perform the calculation. To stay on message, I’m eschewing standard development practice by omitting the user input validation. However, I of course strongly encourage you to add the necessary safeguards if you choose to use this code in a production environment:
public function indexAction()
{
  
  if ($this->getRequest()->isPost()) {       
   
    $locale = new Zend_Locale('en');
    $className = "Zend_Measure_Volume";
   
    // Retrieve the measurement type and value
    $sourceType = $this->_request->getPost('source_type');
    $sourceValue = $this->_request->getPost('source_val');

    // Retrieve the desired conversion type
    $destinationType = $this->_request->getPost('destination_type');
   
    // Reflect the Zend_Measure_Volume class
    $refl = new ReflectionClass('Zend_Measure_Volume');
     
    // Retrieve the appropriate class constants
    $sourceConstant = $refl->getConstant($sourceType);
    $destinationConstant = $refl->getConstant($destinationType);
     
    // Define the source measurement
    $unit = new Zend_Measure_Volume($sourceValue, $sourceConstant, $locale);

    // Convert the value to the desired measurement
    $this->view->convertedValue = $unit->convertTo($destinationConstant);       

  }
  
} 
All of the code in this action should be pretty straightforward, except perhaps the section that deals with reflection. As I mentioned previously, each Zend_Measure class contains a list of supported conversion types, each defined as a class constant. For instance, here’s a snippet of several class constants found in the Zend_Measure_Volume class:
  const ACRE_FOOT           = 'ACRE_FOOT';
  const ACRE_FOOT_SURVEY    = 'ACRE_FOOT_SURVEY';
  const ACRE_INCH           = 'ACRE_INCH';
  const BARREL_WINE         = 'BARREL_WINE';
  const BARREL              = 'BARREL';
Because we’re using the form to dynamically assign the conversion types (for instance, OUNCE_US and GALLON_US), we require a means to dynamically retrieve the appropriate class constant. Using the reflection class’s getConstant() method serves this need nicely.

Converting Roman Numerals to Decimal Equivalents

As I mentioned, the Zend_Measure component is capable of converting much more than the typical volume, weight, and length values that we regularly encounter. Roman numerals offer one particularly interesting example of this component’s flexibility. An avid watcher of Bugs Bunny cartoons as a child, I often took wild guesses at the meaning of the Roman numerals located below the Warner Bros. shield in the opening credits. I rarely managed to decipher many beyond the most commonly used numbers such as I, V, and X. Armed with a browser and the Zend_Measure component, today’s toddlers can make such conversions with ease. Suppose you want to convert the Roman number MCMLXIV to the Arabic equivalent:
$locale = new Zend_Locale('en');
$unit = new Zend_Measure_Number('MCMLXIV', Zend_Measure_Number::ROMAN, $locale);
echo "CONVERT: ".str_replace(",", "", $unit->convertTo(Zend_Measure_Number::DECIMAL)); 

Conclusion

The Zend_Measure component is illustrative of the considerable conveniences the Zend Framework offers. It can abstract a tedious, error-prone conversion process into an easily accessible set of classes. I’d love to see links to your own conversion solutions involving this powerful component!

About the Author

W. Jason Gilmore is founder of EasyPHPWebsites.com. He is the author of several popular books, including “Easy PHP web sites with the Zend Framework,” “Easy PayPal with PHP,” and “Beginning PHP and MySQL, Third Edition.” to e-mail him.