Zend_Measure-Edit.xml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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">Convert</link>
  15. </para>
  16. </listitem>
  17. <listitem>
  18. <para>
  19. <link linkend="zend.measure.edit.add">Add and subtract</link>
  20. </para>
  21. </listitem>
  22. <listitem>
  23. <para>
  24. <link linkend="zend.measure.edit.equal">Compare to boolean</link>
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <link
  30. linkend="zend.measure.edit.compare">Compare to greater/smaller</link>
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. <link
  36. linkend="zend.measure.edit.changevalue">Manually change values</link>
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. <link
  42. linkend="zend.measure.edit.changetype">Manually change types</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 <methodname>convertTo()</methodname> method accepts an optional parameter. With
  58. this parameter you can define an precision for the returned output. The standard
  59. precision is '2'.
  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>. The result will use the same type as the
  80. originating object. Dynamic objects support a fluid style of programming, where complex
  81. sequences of operations can be nested without risk of side-effects altering the input
  82. 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
  101. is not necessary to call <link
  102. linkend="zend.measure.edit.convert"><methodname>convertTo()</methodname></link>
  103. before adding different units.
  104. </para>
  105. </note>
  106. <para>
  107. <example id="zend.measure.edit.add.example-2">
  108. <title>Subtract</title>
  109. <para>
  110. Subtraction of measurements works just like addition.
  111. </para>
  112. <programlisting language="php"><![CDATA[
  113. // Define objects
  114. $unit = new Zend_Measure_Length(200, Zend_Measure_Length::CENTIMETER);
  115. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  116. // Subtract $unit2 from $unit
  117. $sum = $unit->sub($unit2);
  118. echo $sum;
  119. ]]></programlisting>
  120. </example>
  121. </para>
  122. </sect2>
  123. <sect2 id="zend.measure.edit.equal">
  124. <title>Compare</title>
  125. <para>
  126. Measurements can also be compared, but without automatic unit conversion. Thus,
  127. <methodname>equals()</methodname> returns <constant>TRUE</constant>, only if both the
  128. 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
  164. <methodname>compare()</methodname>, which returns 0, -1 or 1 depending on the difference
  165. between the two objects. Identical measurements will return 0. Lesser ones will return a
  166. negative, greater ones a positive value.
  167. </para>
  168. <para>
  169. <example id="zend.measure.edit.compare.example-1">
  170. <title>Difference</title>
  171. <programlisting language="php"><![CDATA[
  172. $unit = new Zend_Measure_Length(100, Zend_Measure_Length::CENTIMETER);
  173. $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER);
  174. $unit3 = new Zend_Measure_Length(1.2, Zend_Measure_Length::METER);
  175. print "Equal:".$unit2->compare($unit);
  176. print "Lesser:".$unit2->compare($unit3);
  177. print "Greater:".$unit3->compare($unit2);
  178. ]]></programlisting>
  179. </example>
  180. </para>
  181. </sect2>
  182. <sect2 id="zend.measure.edit.changevalue">
  183. <title>Manually change values</title>
  184. <para>
  185. To change the value of a measurement explicitly, use
  186. <methodname>setValue()</methodname>. to overwrite the current value. The parameters are
  187. the same as the constructor.
  188. </para>
  189. <para>
  190. <example id="zend.measure.edit.changevalue.example-1">
  191. <title>Changing a value</title>
  192. <programlisting language="php"><![CDATA[
  193. $locale = new Zend_Locale('de_AT');
  194. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  195. $unit->setValue(1.2);
  196. echo $unit;
  197. $unit->setValue(1.2, Zend_Measure_Length::KILOMETER);
  198. echo $unit;
  199. $unit->setValue("1.234,56", Zend_Measure_Length::MILLIMETER,$locale);
  200. echo $unit;
  201. ]]></programlisting>
  202. </example>
  203. </para>
  204. </sect2>
  205. <sect2 id="zend.measure.edit.changetype">
  206. <title>Manually change types</title>
  207. <para>
  208. To change the type of a measurement without altering its value use
  209. <methodname>setType()</methodname>.
  210. </para>
  211. <example id="zend.measure.edit.changetype.example-1">
  212. <title>Changing the type</title>
  213. <programlisting language="php"><![CDATA[
  214. $unit = new Zend_Measure_Length(1,Zend_Measure_Length::METER);
  215. echo $unit; // outputs "1 m"
  216. $unit->setType(Zend_Measure_Length::KILOMETER);
  217. echo $unit; // outputs "1000 km"
  218. ]]></programlisting>
  219. </example>
  220. </sect2>
  221. </sect1>
  222. <!--
  223. vim:se ts=4 sw=4 et:
  224. -->