Zend_Measure-Edit.xml 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 greater/smaller</code></link>
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. <link
  36. linkend="zend.measure.edit.changevalue"><code>Manually change values</code></link>
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. <link
  42. linkend="zend.measure.edit.changetype"><code>Manually change types</code></link>
  43. </para>
  44. </listitem>
  45. </itemizedlist>
  46. <sect2 id="zend.measure.edit.convert">
  47. <title>Convert</title>
  48. <para>
  49. Probably the most important feature is the conversion into different units of
  50. measurement. The conversion of a unit can be done any number of times using the method
  51. <methodname>convertTo()</methodname>. Units of measurement can only be converted to
  52. other units of the same type (class). Therefore, it is not possible to convert (e.g.) a
  53. length into a weight, which would might encourage poor programming practices and allow
  54. errors to propagate without exceptions.
  55. </para>
  56. <para>
  57. The <code>convertTo</code> method accepts an optional parameter. With this parameter you
  58. can define an precision for the returned output. The standard precision is
  59. '<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 <methodname>add()</methodname> and subtracted
  79. using <methodname>sub()</methodname>. Each addition will create a new object for the
  80. result. The actual object will never be changed by the class. The new object will be of
  81. the same type as the originating object. Dynamic objects support a fluid style of
  82. programming, where complex sequences of operations can be nested without risk of
  83. side-effects altering the input objects.
  84. </para>
  85. <para>
  86. <example id="zend.measure.edit.add.example-1">
  87. <title>Adding units</title>
  88. <programlisting language="php"><![CDATA[
  89. // Define objects
  90. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  91. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  92. // Add $unit2 to $unit
  93. $sum = $unit->add($unit2);
  94. echo $sum; // outputs "300 cm"
  95. ]]></programlisting>
  96. </example>
  97. </para>
  98. <note>
  99. <title>Automatic conversion</title>
  100. <para>
  101. Adding one object to another will automatically convert it to the correct unit. It
  102. is not necessary to call <link
  103. linkend="zend.measure.edit.convert"><methodname>convertTo()</methodname></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,
  128. <methodname>equals()</methodname> returns <constant>TRUE</constant>, only if both the
  129. value and the unit of measure are identical.
  130. </para>
  131. <para>
  132. <example id="zend.measure.edit.equal.example-1">
  133. <title>Different measurements</title>
  134. <programlisting language="php"><![CDATA[
  135. // Define measurements
  136. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  137. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  138. if ($unit->equals($unit2)) {
  139. print "Both measurements are identical";
  140. } else {
  141. print "These are different measurements";
  142. }
  143. ]]></programlisting>
  144. </example>
  145. <example id="zend.measure.edit.equal.example-2">
  146. <title>Identical measurements</title>
  147. <programlisting language="php"><![CDATA[
  148. // Define measurements
  149. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  150. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  151. $unit2->setType(Zend_Measure_Length::CENTIMETER);
  152. if ($unit->equals($unit2)) {
  153. print "Both measurements are identical";
  154. } else {
  155. print "These are different measurements";
  156. }
  157. ]]></programlisting>
  158. </example>
  159. </para>
  160. </sect2>
  161. <sect2 id="zend.measure.edit.compare">
  162. <title>Compare</title>
  163. <para>
  164. To determine if a measurement is less than or greater than another, use
  165. <methodname>compare()</methodname>, which returns 0, -1 or 1 depending on the difference
  166. between the two objects. Identical measurements will return 0. Lesser ones will return a
  167. negative, greater ones a positive value.
  168. </para>
  169. <para>
  170. <example id="zend.measure.edit.compare.example-1">
  171. <title>Difference</title>
  172. <programlisting language="php"><![CDATA[
  173. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  174. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  175. $unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);
  176. print "Equal:".$unit2->compare($unit);
  177. print "Lesser:".$unit2->compare($unit3);
  178. print "Greater:".$unit3->compare($unit2);
  179. ]]></programlisting>
  180. </example>
  181. </para>
  182. </sect2>
  183. <sect2 id="zend.measure.edit.changevalue">
  184. <title>Manually change values</title>
  185. <para>
  186. To change the value of a measurement explicitly, use
  187. <methodname>setValue()</methodname>. to overwrite the current value. The parameters are
  188. the same as the constructor.
  189. </para>
  190. <para>
  191. <example id="zend.measure.edit.changevalue.example-1">
  192. <title>Changing a value</title>
  193. <programlisting language="php"><![CDATA[
  194. $locale = new Zend_Locale('de_AT');
  195. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  196. $unit->setValue(1.2);
  197. echo $unit;
  198. $unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
  199. echo $unit;
  200. $unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
  201. echo $unit;
  202. ]]></programlisting>
  203. </example>
  204. </para>
  205. </sect2>
  206. <sect2 id="zend.measure.edit.changetype">
  207. <title>Manually change types</title>
  208. <para>
  209. To change the type of a measurement without altering its value use
  210. <methodname>setType()</methodname>.
  211. </para>
  212. <example id="zend.measure.edit.changetype.example-1">
  213. <title>Changing the type</title>
  214. <programlisting language="php"><![CDATA[
  215. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  216. echo $unit; // outputs "1 m"
  217. $unit->setType(Zend_Measure_Length::KILOMETER);
  218. echo $unit; // outputs "1000 km"
  219. ]]></programlisting>
  220. </example>
  221. </sect2>
  222. </sect1>
  223. <!--
  224. vim:se ts=4 sw=4 et:
  225. -->