Zend_Measure-Creation.xml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.measure.creation">
  4. <title>Creation of Measurements</title>
  5. <para>
  6. When creating a measurement object, <classname>Zend_Measure_*</classname> methods expect the input/original measurement
  7. data value as the first parameter. This can be a
  8. <link linkend="zend.measure.creation.number"><code>numeric argument</code>
  9. </link>
  10. , a
  11. <link linkend="zend.measure.creation.string"><type>String</type>
  12. </link>
  13. without units, or a
  14. <link linkend="zend.measure.creation.localized"><code>localized string</code> with unit(s) specified.
  15. </link>
  16. The second parameter defines the type of the measurement. Both parameters are mandatory. The language may
  17. optionally be specified as the third parameter.
  18. </para>
  19. <sect2 id="zend.measure.creation.number">
  20. <title>Creating measurements from integers and floats</title>
  21. <para>
  22. In addition to integer data values, floating point types may be used, but
  23. <ulink url="http://www.php.net/float">"simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision,"</ulink>
  24. sometimes giving surprising results. Also, do not compare two "float" type numbers for equality.
  25. </para>
  26. <example id="zend.measure.creation.number.example-1">
  27. <title>Creation using integer and floating values</title>
  28. <programlisting language="php"><![CDATA[
  29. $measurement = 1234.7;
  30. $unit = new Zend_Measure_Length((integer)$measurement,
  31. Zend_Measure_Length::STANDARD);
  32. echo $unit;
  33. // outputs '1234 m' (meters)
  34. $unit = new Zend_Measure_Length($measurement, Zend_Measure_Length::STANDARD);
  35. echo $unit;
  36. // outputs '1234.7 m' (meters)
  37. ]]></programlisting>
  38. </example>
  39. </sect2>
  40. <sect2 id="zend.measure.creation.string">
  41. <title>Creating measurements from strings</title>
  42. <para>
  43. Many measurements received as input to Zend Framework applications can only be passed
  44. to <classname>Zend_Measure_*</classname> classes as strings, such as numbers written
  45. using <ulink url="http://en.wikipedia.org/wiki/Roman_numerals">roman numerals</ulink>
  46. or extremely large binary values that exceed the precision of <acronym>PHP</acronym>'s native integer
  47. and float types. Since integers can be denoted using strings, if there is any risk of
  48. losing precision due to limitations of <acronym>PHP</acronym>'s native integer and float types, using
  49. strings instead. <classname>Zend_Measure_Number</classname> uses the BCMath extension
  50. to support arbitrary precision, as shown in the example below, to avoid limitations in
  51. many <acronym>PHP</acronym> functions, such as
  52. <ulink url="http://php.net/bin2dec"><methodname>bin2dec()</methodname></ulink>.
  53. </para>
  54. <example id="zend.measure.creation.string.example-1">
  55. <title>Creation using strings</title>
  56. <programlisting language="php"><![CDATA[
  57. $mystring = "10010100111010111010100001011011101010001";
  58. $unit = new Zend_Measure_Number($mystring, Zend_Measure_Number::BINARY);
  59. echo $unit;
  60. ]]></programlisting>
  61. </example>
  62. <para>
  63. Usually, <classname>Zend_Measure_*</classname> can automatically extract the desired measurement embedded in an
  64. arbitrary string. Only the first identifiable number denoted using standard European/Latin digits
  65. (0,1,2,3,4,5,6,7,8,9) will be used for measurement creation. If there are more numerals later in the string,
  66. the rest of these numerals will be ignored.
  67. </para>
  68. <example id="zend.measure.creation.string.example-2">
  69. <title>Arbitrary text input containing measurements</title>
  70. <programlisting language="php"><![CDATA[
  71. $mystring = "My house is 125m² in size";
  72. $unit = new Zend_Measure_Area($mystring, Zend_Measure_Area::STANDARD);
  73. echo $unit; // outputs "125 m²in size";
  74. $mystring = "My house is 125m² in size, it has 5 rooms of 25m² each.";
  75. $unit = new Zend_Measure_Area($mystring, Zend_Measure_Area::STANDARD);
  76. echo $unit; // outputs "125 m² in size";
  77. ]]></programlisting>
  78. </example>
  79. </sect2>
  80. <sect2 id="zend.measure.creation.localized">
  81. <title>Measurements from localized strings</title>
  82. <para>
  83. When a string is entered in a localized notation, the correct interpretation can not be determined without
  84. knowing the intended locale. The division of decimal digits with "." and grouping of thousands with "," is
  85. common in the English language, but not so in other languages. For example, the English number "1,234.50"
  86. would be interpreted as meaning "1.2345" in German. To deal with such problems, the locale-aware
  87. <classname>Zend_Measure_*</classname> family of classes offer the possibility to specify a language or region to
  88. disambiguate the input data and properly interpret the intended semantic value.
  89. </para>
  90. <example id="zend.measure.creation.localized.example-1">
  91. <title>Localized string</title>
  92. <programlisting language="php"><![CDATA[
  93. $locale = new Zend_Locale('de');
  94. $mystring = "The boat is 1,234.50 long.";
  95. $unit = new Zend_Measure_Length($mystring,
  96. Zend_Measure_Length::STANDARD,
  97. $locale);
  98. echo $unit; // outputs "1.234 m"
  99. $mystring = "The boat is 1,234.50 long.";
  100. $unit = new Zend_Measure_Length($mystring,
  101. Zend_Measure_Length::STANDARD,
  102. 'en_US');
  103. echo $unit; // outputs "1234.50 m"
  104. ]]></programlisting>
  105. </example>
  106. <para>
  107. Since Zend Framework 1.7.0 <classname>Zend_Measure</classname> does also support the usage of an application
  108. wide locale. You can simply set a <classname>Zend_Locale</classname> instance to the registry like shown
  109. below. With this notation you can forget about setting the locale manually with each instance when
  110. you want to use the same locale multiple times.
  111. </para>
  112. <programlisting language="php"><![CDATA[
  113. // in your bootstrap file
  114. $locale = new Zend_Locale('de_AT');
  115. Zend_Registry::set('Zend_Locale', $locale);
  116. // somewhere in your application
  117. $length = new Zend_Measure_Length(Zend_Measure_Length::METER();
  118. ]]></programlisting>
  119. </sect2>
  120. </sect1>
  121. <!--
  122. vim:se ts=4 sw=4 et:
  123. -->