Introducción a Zend Framework

 Aprendiendo Zend Framework

Apéndice

 Referencia de Zend Framework


  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

    47.4. 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.

    47.4.1. 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'.

    Ejemplo 47.9. Convert

    $locale = new Zend_Locale('de');
    $mystring "1.234.567,89";
    $unit = new Zend_Measure_Weight($mystring,'POND'$locale);

    print 
    "Kilo:".$unit->convertTo('KILOGRAM');

    // constants are considered "better practice" than strings
    print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON);

    // define a precision for the output
    print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON3);

    47.4.2. 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.

    Ejemplo 47.10. Adding units

    // Define objects
    $unit = new Zend_Measure_Length(200Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

    // Add $unit2 to $unit
    $sum $unit->add($unit2);

    echo 
    $sum// outputs "300 cm"


    [Nota] 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.

    Ejemplo 47.11. Subtract

    Subtraction of measurements works just like addition.

    // Define objects
    $unit = new Zend_Measure_Length(200Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

    // Subtract $unit2 from $unit
    $sum $unit->sub($unit2);

    echo 
    $sum;


    47.4.3. 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.

    Ejemplo 47.12. Different measurements

    // Define measurements
    $unit = new Zend_Measure_Length(100Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

    if (
    $unit->equals($unit2)) {
        print 
    "Both measurements are identical";
    } else {
        print 
    "These are different measurements";
    }


    Ejemplo 47.13. Identical measurements

    // Define measurements
    $unit = new Zend_Measure_Length(100Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);

    $unit2->setType(Zend_Measure_Length::CENTIMETER);

    if (
    $unit->equals($unit2)) {
        print 
    "Both measurements are identical";
    } else {
        print 
    "These are different measurements";
    }


    47.4.4. 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.

    Ejemplo 47.14. Difference

    $unit = new Zend_Measure_Length(100Zend_Measure_Length::CENTIMETER);
    $unit2 = new Zend_Measure_Length(1Zend_Measure_Length::METER);
    $unit3 = new Zend_Measure_Length(1.2Zend_Measure_Length::METER);

    print 
    "Equal:".$unit2->compare($unit);
    print 
    "Lesser:".$unit2->compare($unit3);
    print 
    "Greater:".$unit3->compare($unit2);


    47.4.5. 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.

    Ejemplo 47.15. Changing a value

    $locale = new Zend_Locale('de_AT');
    $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);

    $unit->setValue(1.2);
    echo 
    $unit;

    $unit->setValue(1.2Zend_Measure_Length::KILOMETER);
    echo 
    $unit;

    $unit->setValue("1.234,56"Zend_Measure_Length::MILLIMETER,$locale);
    echo 
    $unit;


    47.4.6. Manually change types

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

    Ejemplo 47.16. Changing the type

    $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
    echo 
    $unit// outputs "1 m"

    $unit->setType(Zend_Measure_Length::KILOMETER);
    echo 
    $unit// outputs "1000 km"

    digg delicious meneame google twitter technorati facebook

    Comentarios

    Loading...