Zend_Measure-Edit.xml 9.0 KB

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