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