Zend_Measure-Edit.xml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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>. Each addition will create a new object for the
  82. result. The actual object will never be changed by the class. The new object will be of
  83. the same type as the originating object. Dynamic objects support a fluid style of
  84. programming, where complex sequences of operations can be nested without risk of
  85. side-effects altering the input objects.
  86. </para>
  87. <para>
  88. <example id="zend.measure.edit.add.example-1">
  89. <title>Adding units</title>
  90. <programlisting language="php"><![CDATA[
  91. // Define objects
  92. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  93. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  94. // Add $unit2 to $unit
  95. $sum = $unit->add($unit2);
  96. echo $sum; // outputs "300 cm"
  97. ]]></programlisting>
  98. </example>
  99. </para>
  100. <note>
  101. <title>Automatic conversion</title>
  102. <para>
  103. Adding one object to another will automatically convert it to the correct unit. It
  104. is not necessary to call <link
  105. linkend="zend.measure.edit.convert"><methodname>convertTo()</methodname></link>
  106. before adding different units.
  107. </para>
  108. </note>
  109. <para>
  110. <example id="zend.measure.edit.add.example-2">
  111. <title>Subtract</title>
  112. <para>
  113. Subtraction of measurements works just like addition.
  114. </para>
  115. <programlisting language="php"><![CDATA[
  116. // Define objects
  117. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  118. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  119. // Subtract $unit2 from $unit
  120. $sum = $unit->sub($unit2);
  121. echo $sum;
  122. ]]></programlisting>
  123. </example>
  124. </para>
  125. </sect2>
  126. <sect2 id="zend.measure.edit.equal">
  127. <title>Compare</title>
  128. <para>
  129. Measurements can also be compared, but without automatic unit conversion. Thus,
  130. <methodname>equals()</methodname> returns <constant>TRUE</constant>, only if both the
  131. value and the unit of measure are identical.
  132. </para>
  133. <para>
  134. <example id="zend.measure.edit.equal.example-1">
  135. <title>Different measurements</title>
  136. <programlisting language="php"><![CDATA[
  137. // Define measurements
  138. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  139. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  140. if ($unit->equals($unit2)) {
  141. print "Both measurements are identical";
  142. } else {
  143. print "These are different measurements";
  144. }
  145. ]]></programlisting>
  146. </example>
  147. <example id="zend.measure.edit.equal.example-2">
  148. <title>Identical measurements</title>
  149. <programlisting language="php"><![CDATA[
  150. // Define measurements
  151. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  152. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  153. $unit2->setType(Zend_Measure_Length::CENTIMETER);
  154. if ($unit->equals($unit2)) {
  155. print "Both measurements are identical";
  156. } else {
  157. print "These are different measurements";
  158. }
  159. ]]></programlisting>
  160. </example>
  161. </para>
  162. </sect2>
  163. <sect2 id="zend.measure.edit.compare">
  164. <title>Compare</title>
  165. <para>
  166. To determine if a measurement is less than or greater than another, use
  167. <methodname>compare()</methodname>, which returns 0, -1 or 1 depending on the difference
  168. between the two objects. Identical measurements will return 0. Lesser ones will return a
  169. negative, greater ones a positive value.
  170. </para>
  171. <para>
  172. <example id="zend.measure.edit.compare.example-1">
  173. <title>Difference</title>
  174. <programlisting language="php"><![CDATA[
  175. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  176. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  177. $unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);
  178. print "Equal:".$unit2->compare($unit);
  179. print "Lesser:".$unit2->compare($unit3);
  180. print "Greater:".$unit3->compare($unit2);
  181. ]]></programlisting>
  182. </example>
  183. </para>
  184. </sect2>
  185. <sect2 id="zend.measure.edit.changevalue">
  186. <title>Manually change values</title>
  187. <para>
  188. To change the value of a measurement explicitly, use
  189. <methodname>setValue()</methodname>. to overwrite the current value. The parameters are
  190. the same as the constructor.
  191. </para>
  192. <para>
  193. <example id="zend.measure.edit.changevalue.example-1">
  194. <title>Changing a value</title>
  195. <programlisting language="php"><![CDATA[
  196. $locale = new Zend_Locale('de_AT');
  197. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  198. $unit->setValue(1.2);
  199. echo $unit;
  200. $unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
  201. echo $unit;
  202. $unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
  203. echo $unit;
  204. ]]></programlisting>
  205. </example>
  206. </para>
  207. </sect2>
  208. <sect2 id="zend.measure.edit.changetype">
  209. <title>Manually change types</title>
  210. <para>
  211. To change the type of a measurement without altering its value use
  212. <methodname>setType()</methodname>.
  213. </para>
  214. <example id="zend.measure.edit.changetype.example-1">
  215. <title>Changing the type</title>
  216. <programlisting language="php"><![CDATA[
  217. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  218. echo $unit; // outputs "1 m"
  219. $unit->setType(Zend_Measure_Length::KILOMETER);
  220. echo $unit; // outputs "1000 km"
  221. ]]></programlisting>
  222. </example>
  223. </sect2>
  224. </sect1>
  225. <!--
  226. vim:se ts=4 sw=4 et:
  227. -->