2
0

Zend_Measure-Edit.xml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.measure.edit">
  4. <title>Manipulating Measurements</title>
  5. <para>
  6. Parsing and normalization of input, combined with output to localized notations makes data accessible to users
  7. in different locales. Many additional methods exist in <classname>Zend_Measure_*</classname> components to manipulate and
  8. work with this data, after it has been normalized.
  9. </para>
  10. <itemizedlist>
  11. <listitem>
  12. <para>
  13. <link linkend="zend.measure.edit.convert"><code>Convert</code>
  14. </link>
  15. </para>
  16. </listitem>
  17. <listitem>
  18. <para>
  19. <link linkend="zend.measure.edit.add"><code>Add and subtract</code>
  20. </link>
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. <link linkend="zend.measure.edit.equal"><code>Compare to boolean</code>
  26. </link>
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <link linkend="zend.measure.edit.compare"><code>Compare to greater/smaller</code>
  32. </link>
  33. </para>
  34. </listitem>
  35. <listitem>
  36. <para>
  37. <link linkend="zend.measure.edit.changevalue"><code>Manually change values</code>
  38. </link>
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. <link linkend="zend.measure.edit.changetype"><code>Manually change types</code>
  44. </link>
  45. </para>
  46. </listitem>
  47. </itemizedlist>
  48. <sect2 id="zend.measure.edit.convert">
  49. <title>Convert</title>
  50. <para>
  51. Probably the most important feature is the conversion into different units of measurement. The conversion of
  52. a unit can be done any number of times using the method <code>convertTo()</code>. Units of measurement can
  53. only be converted to other units of the same type (class). Therefore, it is not possible to convert (e.g.) a
  54. length into a weight, which would might encourage poor programming practices and allow errors to propagate
  55. without exceptions.
  56. </para>
  57. <para>
  58. The <code>convertTo</code> method accepts an optional parameter. With this parameter you can define
  59. an precision for the returned output. The standard precision is '<code>2</code>'.
  60. </para>
  61. <example id="zend.measure.edit.convert.example-1">
  62. <title>Convert</title>
  63. <programlisting language="php"><![CDATA[
  64. $locale = new Zend_Locale('de');
  65. $mystring = "1.234.567,89";
  66. $unit = new Zend_Measure_Weight($mystring,'POND', $locale);
  67. print "Kilo:".$unit->convertTo('KILOGRAM');
  68. // constants are considered "better practice" than strings
  69. print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON);
  70. // define a precision for the output
  71. print "Ton:".$unit->convertTo(Zend_Measure_Weight::TON, 3);
  72. ]]></programlisting>
  73. </example>
  74. </sect2>
  75. <sect2 id="zend.measure.edit.add">
  76. <title>Add and subtract</title>
  77. <para>
  78. Measurements can be added together using <code>add()</code> and subtracted using <code>sub()</code>. Each
  79. addition will create a new object for the result. The actual object will never be changed by the class. The
  80. new object will be of the same type as the originating object. Dynamic objects support a fluid style of
  81. programming, where complex sequences of operations can be nested without risk of side-effects altering the
  82. input objects.
  83. </para>
  84. <para>
  85. <example id="zend.measure.edit.add.example-1">
  86. <title>Adding units</title>
  87. <programlisting language="php"><![CDATA[
  88. // Define objects
  89. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  90. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  91. // Add $unit2 to $unit
  92. $sum = $unit->add($unit2);
  93. echo $sum; // outputs "300 cm"
  94. ]]></programlisting>
  95. </example>
  96. </para>
  97. <note>
  98. <title>Automatic conversion</title>
  99. <para>
  100. Adding one object to another will automatically convert it to the correct unit. It is not necessary to
  101. call
  102. <link linkend="zend.measure.edit.convert"><code>convertTo()</code>
  103. </link>
  104. before adding different units.
  105. </para>
  106. </note>
  107. <para>
  108. <example id="zend.measure.edit.add.example-2">
  109. <title>Subtract</title>
  110. <para>
  111. Subtraction of measurements works just like addition.
  112. </para>
  113. <programlisting language="php"><![CDATA[
  114. // Define objects
  115. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  116. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  117. // Subtract $unit2 from $unit
  118. $sum = $unit->sub($unit2);
  119. echo $sum;
  120. ]]></programlisting>
  121. </example>
  122. </para>
  123. </sect2>
  124. <sect2 id="zend.measure.edit.equal">
  125. <title>Compare</title>
  126. <para>
  127. Measurements can also be compared, but without automatic unit conversion. Thus, <code>equals()</code>
  128. returns <constant>TRUE</constant>, only if both the value and the unit of measure are identical.
  129. </para>
  130. <para>
  131. <example id="zend.measure.edit.equal.example-1">
  132. <title>Different measurements</title>
  133. <programlisting language="php"><![CDATA[
  134. // Define measurements
  135. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  136. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  137. if ($unit->equals($unit2)) {
  138. print "Both measurements are identical";
  139. } else {
  140. print "These are different measurements";
  141. }
  142. ]]></programlisting>
  143. </example>
  144. <example id="zend.measure.edit.equal.example-2">
  145. <title>Identical measurements</title>
  146. <programlisting language="php"><![CDATA[
  147. // Define measurements
  148. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  149. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  150. $unit2->setType(Zend_Measure_Length::CENTIMETER);
  151. if ($unit->equals($unit2)) {
  152. print "Both measurements are identical";
  153. } else {
  154. print "These are different measurements";
  155. }
  156. ]]></programlisting>
  157. </example>
  158. </para>
  159. </sect2>
  160. <sect2 id="zend.measure.edit.compare">
  161. <title>Compare</title>
  162. <para>
  163. To determine if a measurement is less than or greater than another, use <code>compare()</code>, which
  164. returns 0, -1 or 1 depending on the difference between the two objects. Identical measurements will return
  165. 0. Lesser ones will return a negative, greater ones a positive value.
  166. </para>
  167. <para>
  168. <example id="zend.measure.edit.compare.example-1">
  169. <title>Difference</title>
  170. <programlisting language="php"><![CDATA[
  171. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  172. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  173. $unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);
  174. print "Equal:".$unit2->compare($unit);
  175. print "Lesser:".$unit2->compare($unit3);
  176. print "Greater:".$unit3->compare($unit2);
  177. ]]></programlisting>
  178. </example>
  179. </para>
  180. </sect2>
  181. <sect2 id="zend.measure.edit.changevalue">
  182. <title>Manually change values</title>
  183. <para>
  184. To change the value of a measurement explicitly, use <code>setValue()</code>. to overwrite the current
  185. value. The parameters are the same as the constructor.
  186. </para>
  187. <para>
  188. <example id="zend.measure.edit.changevalue.example-1">
  189. <title>Changing a value</title>
  190. <programlisting language="php"><![CDATA[
  191. $locale = new Zend_Locale('de_AT');
  192. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  193. $unit->setValue(1.2);
  194. echo $unit;
  195. $unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
  196. echo $unit;
  197. $unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
  198. echo $unit;
  199. ]]></programlisting>
  200. </example>
  201. </para>
  202. </sect2>
  203. <sect2 id="zend.measure.edit.changetype">
  204. <title>Manually change types</title>
  205. <para>
  206. To change the type of a measurement without altering its value use <code>setType()</code>.
  207. </para>
  208. <example id="zend.measure.edit.changetype.example-1">
  209. <title>Changing the type</title>
  210. <programlisting language="php"><![CDATA[
  211. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  212. echo $unit; // outputs "1 m"
  213. $unit->setType(Zend_Measure_Length::KILOMETER);
  214. echo $unit; // outputs "1000 km"
  215. ]]></programlisting>
  216. </example>
  217. </sect2>
  218. </sect1>
  219. <!--
  220. vim:se ts=4 sw=4 et:
  221. -->