Zend_Currency-Exchange.xml 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.currency.exchange">
  4. <title>Exchanging currencies</title>
  5. <para>
  6. Within the previous section we discussed currency calculations. But as you can imaging
  7. calculating currencies does often mean to calculate different currencies from different
  8. countries.
  9. </para>
  10. <para>
  11. In this case the currencies have to be exchanged so that both use the same currency.
  12. Within real live this information is available by banks or by daily papers. But as we
  13. are in web, we should use available exchange services.
  14. <classname>Zend_Currency</classname> allows their usage with a simple callback.
  15. </para>
  16. <para>
  17. First let's write a simple exchange service.
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. class SimpleExchange implements Zend_Currency_CurrencyInterface
  21. {
  22. public function getRate($from, $to)
  23. {
  24. if ($from !== "USD") {
  25. throw new Exception('We can only exchange USD');
  26. }
  27. switch ($to) {
  28. case 'EUR':
  29. return 2;
  30. case 'JPE':
  31. return 0.7;
  32. }
  33. throw new Exception('Unable to exchange $to');
  34. }
  35. }
  36. ]]></programlisting>
  37. <para>
  38. We have now created a manual exchange service. It will not fit the real live, but it
  39. shows you how currency exchange works.
  40. </para>
  41. <para>
  42. Your exchange class must implement the
  43. <classname>Zend_Currency_CurrencyInterface</classname> interface. This interface
  44. requires the single method <methodname>getRate()</methodname> to be implemented. This
  45. method has two parameters it will receive. Both are the short names for the given
  46. currencies. <classname>Zend_Currency</classname> on the other side needs the exchange
  47. rate to be returned.
  48. </para>
  49. <para>
  50. In a living exchange class you would probably ask the service provider for the correct
  51. exchange rates. For our example the manual rate will be ok.
  52. </para>
  53. <para>
  54. Now we will simply attach our exchange class with <classname>Zend_Currency</classname>.
  55. There are two ways to do this. Eigher by attaching a instance of the Exchange class, or
  56. by simply giving a string with the classname.
  57. </para>
  58. <programlisting language="php"><![CDATA[
  59. $currency = new Zend_Currency(
  60. array(
  61. 'value' => 1000,
  62. 'currency' => 'EUR',
  63. )
  64. );
  65. $service = new SimpleExchange();
  66. // attach the exchange service
  67. $currency->setService($service);
  68. $currency2 = new Zend_Currency(
  69. array(
  70. 'value' => 1000,
  71. 'currency' => 'USD',
  72. )
  73. );
  74. print $currency->add($currency2);
  75. ]]></programlisting>
  76. <para>
  77. The above example will return '$ 3.000' because the 1.000 <acronym>USD</acronym> will be
  78. converted by a rate of 2 to 2.000 <acronym>EUR</acronym>.
  79. </para>
  80. <note>
  81. <title>Calculation without exchange service</title>
  82. <para>
  83. When you try to calculate two currency objects which do not use the same currency
  84. and have no exchange service attached, you will get an exception. The reason is
  85. that <classname>Zend_Currency</classname> is then not able to switch between the
  86. different currencies.
  87. </para>
  88. </note>
  89. </sect1>