2
0

Zend_Measure-Creation.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 ZF applications can only be passed to <classname>Zend_Measure_*</classname>
  44. classes as strings, such as numbers written using
  45. <ulink url="http://en.wikipedia.org/wiki/Roman_numerals">roman numerals</ulink>
  46. or extremely large binary values that exceed the precision of PHP's native integer and float types. Since
  47. integers can be denoted using strings, if there is any risk of losing precision due to limitations of PHP's
  48. native integer and float types, using strings instead. <classname>Zend_Measure_Number</classname> uses the BCMath
  49. extension to support arbitrary precision, as shown in the example below, to avoid limitations in many PHP
  50. functions, such as
  51. <ulink url="http://php.net/bin2dec"><code>bin2dec()</code>
  52. </ulink>
  53. .
  54. </para>
  55. <example id="zend.measure.creation.string.example-1">
  56. <title>Creation using strings</title>
  57. <programlisting language="php"><![CDATA[
  58. $mystring = "10010100111010111010100001011011101010001";
  59. $unit = new Zend_Measure_Number($mystring, Zend_Measure_Number::BINARY);
  60. echo $unit;
  61. ]]></programlisting>
  62. </example>
  63. <para>
  64. Usually, <classname>Zend_Measure_*</classname> can automatically extract the desired measurement embedded in an
  65. arbitrary string. Only the first identifiable number denoted using standard European/Latin digits
  66. (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,
  67. the rest of these numerals will be ignored.
  68. </para>
  69. <example id="zend.measure.creation.string.example-2">
  70. <title>Arbitrary text input containing measurements</title>
  71. <programlisting language="php"><![CDATA[
  72. $mystring = "My house is 125m² in size";
  73. $unit = new Zend_Measure_Area($mystring, Zend_Measure_Area::STANDARD);
  74. echo $unit; // outputs "125 m²in size";
  75. $mystring = "My house is 125m² in size, it has 5 rooms of 25m² each.";
  76. $unit = new Zend_Measure_Area($mystring, Zend_Measure_Area::STANDARD);
  77. echo $unit; // outputs "125 m² in size";
  78. ]]></programlisting>
  79. </example>
  80. </sect2>
  81. <sect2 id="zend.measure.creation.localized">
  82. <title>Measurements from localized strings</title>
  83. <para>
  84. When a string is entered in a localized notation, the correct interpretation can not be determined without
  85. knowing the intended locale. The division of decimal digits with "." and grouping of thousands with "," is
  86. common in the English language, but not so in other languages. For example, the English number "1,234.50"
  87. would be interpreted as meaning "1.2345" in German. To deal with such problems, the locale-aware
  88. <classname>Zend_Measure_*</classname> family of classes offer the possibility to specify a language or region to
  89. disambiguate the input data and properly interpret the intended semantic value.
  90. </para>
  91. <example id="zend.measure.creation.localized.example-1">
  92. <title>Localized string</title>
  93. <programlisting language="php"><![CDATA[
  94. $locale = new Zend_Locale('de');
  95. $mystring = "The boat is 1,234.50 long.";
  96. $unit = new Zend_Measure_Length($mystring,
  97. Zend_Measure_Length::STANDARD,
  98. $locale);
  99. echo $unit; // outputs "1.234 m"
  100. $mystring = "The boat is 1,234.50 long.";
  101. $unit = new Zend_Measure_Length($mystring,
  102. Zend_Measure_Length::STANDARD,
  103. 'en_US');
  104. echo $unit; // outputs "1234.50 m"
  105. ]]></programlisting>
  106. </example>
  107. <para>
  108. Since Zend Framework 1.7.0 <classname>Zend_Measure</classname> does also support the usage of an application
  109. wide locale. You can simply set a <classname>Zend_Locale</classname> instance to the registry like shown
  110. below. With this notation you can forget about setting the locale manually with each instance when
  111. you want to use the same locale multiple times.
  112. </para>
  113. <programlisting language="php"><![CDATA[
  114. // in your bootstrap file
  115. $locale = new Zend_Locale('de_AT');
  116. Zend_Registry::set('Zend_Locale', $locale);
  117. // somewhere in your application
  118. $length = new Zend_Measure_Length(Zend_Measure_Length::METER();
  119. ]]></programlisting>
  120. </sect2>
  121. </sect1>
  122. <!--
  123. vim:se ts=4 sw=4 et:
  124. -->