Zend_Currency-Usage.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.currency.usage">
  4. <title>How to Work with Currencies</title>
  5. <para>
  6. To use <classname>Zend_Currency</classname> within your application, create an instance of it without any
  7. parameters. This will create an instance of <classname>Zend_Currency</classname> with your locale set and defines
  8. the currency which should be used for this locale.
  9. </para>
  10. <example id="zend.currency.usage.example1">
  11. <title>Creating an Instance of Zend_Currency from the Locale</title>
  12. <para>
  13. Assume you have 'en_US' set as the set locale by the user or your environment. By passing no
  14. parameters while creating the instance you tell <classname>Zend_Currency</classname> to use the currency
  15. from the locale 'en_US'. This leads to an instance with US Dollar set as the currency with
  16. formatting rules from 'en_US'.
  17. </para>
  18. <programlisting role="php"><![CDATA[
  19. $currency = new Zend_Currency();
  20. ]]></programlisting>
  21. </example>
  22. <para>
  23. <classname>Zend_Currency</classname> also supports the usage of an application-wide locale. You can set a <classname>Zend_Locale</classname> instance in the registry as shown
  24. below. With this notation you can avoid setting the locale manually for each instance when
  25. you want to use the same locale throughout the application.
  26. </para>
  27. <programlisting role="php"><![CDATA[
  28. // in your bootstrap file
  29. $locale = new Zend_Locale('de_AT');
  30. Zend_Registry::set('Zend_Locale', $locale);
  31. // somewhere in your application
  32. $currency = new Zend_Currency();
  33. ]]></programlisting>
  34. <note>
  35. <para>
  36. If your system has no default locale, or if the locale of your system can not be
  37. detected automatically, <classname>Zend_Currency</classname> will throw an exception. If see this exception, you should consider setting the locale manually.
  38. </para>
  39. </note>
  40. <para>
  41. Depending on your needs, several parameters can be speicified at instantiation. Each of these parameters
  42. is optional and can be omitted. Even the order of the parameters can be switched. The meaning of each
  43. parameter is described in this list:
  44. </para>
  45. <itemizedlist mark='opencircle'>
  46. <listitem>
  47. <para>
  48. <emphasis role="strong">currency</emphasis>:
  49. </para>
  50. <para>
  51. A locale can include several currencies. Therefore the first parameter
  52. <emphasis role="strong">'currency'</emphasis> can define which currency should be used by giving
  53. the short name or full name of that currency. If that currency in not recognized in any locale an
  54. exception will be thrown. Currency short names are always made up of 3 letters, written in uppercase.
  55. Well known currency shortnames include <code>USD</code> or <code>EUR</code>.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. <emphasis role="strong">locale</emphasis>:
  61. </para>
  62. <para>
  63. The <emphasis role="strong">'locale'</emphasis> parameter defines which locale should be
  64. used for formatting the currency. The specified locale will also be used to get the script and symbol for this currency if these parameters are not given.
  65. </para>
  66. <note>
  67. <para>
  68. Note that Zend_Currency only accepts locales which include a region. This means that all
  69. locales that only include a language will result in an exception. For example the locale
  70. <emphasis role="strong">en</emphasis> will cause an exception to be thrown whereas the locale
  71. <emphasis role="strong">en_US</emphasis> will return <emphasis role="strong">USD</emphasis>
  72. as currency.
  73. </para>
  74. </note>
  75. </listitem>
  76. </itemizedlist>
  77. <example id="zend.currency.usage.example2">
  78. <title>Other Ways to Create Instances of Zend_Currency</title>
  79. <programlisting role="php"><![CDATA[
  80. // expect standard locale 'de_AT'
  81. // creates an instance from 'en_US' using 'USD' which is default
  82. // currency for 'en_US'
  83. $currency = new Zend_Currency('en_US');
  84. // creates an instance from the set locale ('de_AT') using 'EUR' as
  85. // currency
  86. $currency = new Zend_Currency();
  87. // creates an instance using 'EUR' as currency, 'en_US' for number
  88. // formating
  89. $currency = new Zend_Currency('en_US', 'EUR');
  90. ]]></programlisting>
  91. </example>
  92. <para>
  93. You can omit any of the parameters to <classname>Zend_Currency</classname>'s constructor if you want to use the default values. This has no negative effect
  94. on handling the currencies. It can be useful if, for example, you don't know the default currency for a region.
  95. </para>
  96. <note>
  97. <para>
  98. For many countries there are several known currencies. Typically, one currency will currently be in use, with one or more ancient currencies. If the '<emphasis role="strong">currency</emphasis>' parameter
  99. is suppressed the contemporary currency will be used. The region '<emphasis role="strong">de</emphasis>' for
  100. example knows the currencies '<emphasis role="strong">EUR</emphasis>' and
  101. '<emphasis role="strong">DEM</emphasis>'... '<emphasis role="strong">EUR</emphasis>' is the contemporary currency
  102. and will be used if the currency parameter is omitted.
  103. </para>
  104. </note>
  105. <sect2 id="zend.currency.usage.tocurrency">
  106. <title>Creating and Output String from a Currency</title>
  107. <para>
  108. To get a numeric value converted to an output string formatted for the currency at hand, use the method
  109. <emphasis role="strong">toCurrency()</emphasis>. It takes the value which should be converted.
  110. The value itself can be any normalized number.
  111. </para>
  112. <para>
  113. If you have a localized number you will have to convert it first to an normalized number with
  114. <link linkend="zend.locale.number.normalize">Zend_Locale_Format::getNumber()</link>. It may then
  115. be used with <code>toCurrency()</code> to create a currency output string.
  116. </para>
  117. <para>
  118. <code>toCurrency(array $options)</code> accepts an array with options which can be used to temporarily set another
  119. format or currency representation. For details about which options can be used see
  120. <link linkend="zend.currency.usage.setformat">Changing the Format of a Currency</link>.
  121. </para>
  122. <example id="zend.currency.usage.tocurrency.example">
  123. <title>Creating an Output String for a Currency</title>
  124. <programlisting role="php"><![CDATA[
  125. // creates an instance with 'en_US' using 'USD', which is the default
  126. // values for 'en_US'
  127. $currency = new Zend_Currency('en_US');
  128. // prints '$ 1,000.00'
  129. echo $currency->toCurrency(1000);
  130. // prints '$ 1.000,00'
  131. echo $currency->toCurrency(1000, array('format' => 'de_AT'));
  132. // prints '$ ١٬٠٠٠٫٠٠'
  133. echo $currency->toCurrency(1000, array('script' => 'Arab'));
  134. ]]></programlisting>
  135. </example>
  136. </sect2>
  137. <sect2 id="zend.currency.usage.setformat">
  138. <title>Changing the Format of a Currency</title>
  139. <para>
  140. The format which is used by creation of a <classname>Zend_Currency</classname> instance is, of course, the
  141. standard format. But occasionally it is necessary to change this format.
  142. </para>
  143. <para>
  144. The format of an currency output includes the following parts:
  145. </para>
  146. <itemizedlist mark='opencircle'>
  147. <listitem>
  148. <para>
  149. <emphasis role="strong">Currency symbol, shortname or name</emphasis>:
  150. </para>
  151. <para>
  152. The symbol of the currency is normally displayed within the currency output string. It can be suppressed
  153. when needed or even overwritten.
  154. </para>
  155. </listitem>
  156. <listitem>
  157. <para>
  158. <emphasis role="strong">Currency position</emphasis>:
  159. </para>
  160. <para>
  161. The position of the currency symbol is normally automatically defined by the locale. It can be
  162. changed if necessary.
  163. </para>
  164. </listitem>
  165. <listitem>
  166. <para>
  167. <emphasis role="strong">Script</emphasis>:
  168. </para>
  169. <para>
  170. The script which shall be used to display digits. Detailed information about scripts and their
  171. usage can be found in the documentation of <classname>Zend_Locale</classname> in
  172. <link linkend="zend.locale.appendix.numberscripts.supported">supported number scripts</link>.
  173. </para>
  174. </listitem>
  175. <listitem>
  176. <para>
  177. <emphasis role="strong">Number formatting</emphasis>:
  178. </para>
  179. <para>
  180. The amount of currency (formally known as value of money) is formatted by the usage of
  181. formatting rules within the locale. For example is in English the ',' sign used as separator for
  182. thousands, and in German the '.' sign.
  183. </para>
  184. </listitem>
  185. </itemizedlist>
  186. <para>
  187. So if you need to change the format, you should the
  188. <emphasis role="strong">setFormat()</emphasis> method. It takes an array which should include every
  189. option you want to change. The <code>options</code> array supports the following
  190. settings:
  191. </para>
  192. <itemizedlist mark='opencircle'>
  193. <listitem>
  194. <para>
  195. <emphasis role="strong">position</emphasis>: Defines the position at which the currency
  196. description should be displayed. The supported positions can be found in
  197. <link linkend="zend.currency.usage.setformat.constantsposition">this table</link>.
  198. </para>
  199. </listitem>
  200. <listitem>
  201. <para>
  202. <emphasis role="strong">script</emphasis>: Defined which script should be used for
  203. displaying digits. The default script for most locales is
  204. <emphasis role="strong">'Latn'</emphasis>, which includes the digits 0 to 9. Other
  205. scripts such as 'Arab' (Arabian) can be used. All supported scripts can be found in
  206. <link linkend="zend.locale.appendix.numberscripts.supported">this table</link>.
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. <emphasis role="strong">format</emphasis>: Defines the format which should be used for displaying
  212. numbers. This number-format includes for example the thousand separator. You can eighter use
  213. a default format by giving a locale identifier, or define the number-format manually.
  214. If no format is set the locale from the <classname>Zend_Currency</classname> object will be used.
  215. </para>
  216. </listitem>
  217. <listitem>
  218. <para>
  219. <emphasis role="strong">display</emphasis>: Defines which part of the currency should be
  220. used for displaying the currency representation. There are 4 representations which can be
  221. used and which are all described in
  222. <link linkend="zend.currency.usage.setformat.constantsdescription">this table</link>.
  223. </para>
  224. </listitem>
  225. <listitem>
  226. <para>
  227. <emphasis role="strong">precision</emphasis>: Defines the precision which should be used for
  228. the currency representation. The default value is <emphasis role="strong">2</emphasis>.
  229. </para>
  230. </listitem>
  231. <listitem>
  232. <para>
  233. <emphasis role="strong">name</emphasis>: Defines the full currency name which should be
  234. displayed. This option overwrites any currency name which is set through
  235. the creation of <classname>Zend_Currency</classname>.
  236. </para>
  237. </listitem>
  238. <listitem>
  239. <para>
  240. <emphasis role="strong">currency</emphasis>: Defines the international abbreviation which
  241. should be displayed. This option overwrites any abbreviation which is set through
  242. the creation of <classname>Zend_Currency</classname>.
  243. </para>
  244. </listitem>
  245. <listitem>
  246. <para>
  247. <emphasis role="strong">symbol</emphasis>: Defines the currency symbol which should be
  248. displayed. This option overwrites any symbol which is set through
  249. the creation of <classname>Zend_Currency</classname>.
  250. </para>
  251. </listitem>
  252. </itemizedlist>
  253. <table id="zend.currency.usage.setformat.constantsdescription">
  254. <title>Constants for the selecting the currency description</title>
  255. <tgroup cols="2" align="left">
  256. <thead>
  257. <row>
  258. <entry>constant</entry>
  259. <entry>description</entry>
  260. </row>
  261. </thead>
  262. <tbody>
  263. <row>
  264. <entry>NO_SYMBOL</entry>
  265. <entry>Do not display any currency representation</entry>
  266. </row>
  267. <row>
  268. <entry>USE_SYMBOL</entry>
  269. <entry>Display the currency symbol</entry>
  270. </row>
  271. <row>
  272. <entry>USE_SHORTNAME</entry>
  273. <entry>Display the 3 lettered international currency abbreviation</entry>
  274. </row>
  275. <row>
  276. <entry>USE_NAME</entry>
  277. <entry>Display the full currency name</entry>
  278. </row>
  279. </tbody>
  280. </tgroup>
  281. </table>
  282. <table id="zend.currency.usage.setformat.constantsposition">
  283. <title>Constants for the selecting the position of the currency description</title>
  284. <tgroup cols="2" align="left">
  285. <thead>
  286. <row>
  287. <entry>constant</entry>
  288. <entry>description</entry>
  289. </row>
  290. </thead>
  291. <tbody>
  292. <row>
  293. <entry>STANDARD</entry>
  294. <entry>Set the standard position as defined within the locale</entry>
  295. </row>
  296. <row>
  297. <entry>RIGHT</entry>
  298. <entry>Display the currency representation at the right side of the value</entry>
  299. </row>
  300. <row>
  301. <entry>LEFT</entry>
  302. <entry>Display the currency representation at the left side of the value</entry>
  303. </row>
  304. </tbody>
  305. </tgroup>
  306. </table>
  307. <example id="zend.currency.usage.setformat.example">
  308. <title>Changing the displayed format of a currency</title>
  309. <programlisting role="php"><![CDATA[
  310. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as
  311. // these are the default values from 'en_US'
  312. $currency = new Zend_Currency('en_US');
  313. // prints 'US$ 1,000.00'
  314. echo $currency->toCurrency(1000);
  315. $currency->setFormat(array('display' => Zend_Currency::USE_NAME,
  316. 'position' => Zend_Currency::RIGHT));
  317. // prints '1.000,00 US Dollar'
  318. echo $currency->toCurrency(1000);
  319. $currency->setFormat(array('name' => 'American Dollar'));
  320. // prints '1.000,00 American Dollar'
  321. echo $currency->toCurrency(1000);
  322. $currency->setFormat(array('format' => '##0.00'));
  323. // prints '1000,00 American Dollar'
  324. echo $currency->toCurrency(1000);
  325. ]]></programlisting>
  326. </example>
  327. </sect2>
  328. <sect2 id="zend.currency.usage.informational">
  329. <title>Reference Methods for Zend_Currency</title>
  330. <para>
  331. Of course, <classname>Zend_Currency</classname> supports also methods to get information about any existing
  332. and many ancient currencies from <classname>Zend_Locale</classname>. The supported
  333. methods are:
  334. </para>
  335. <itemizedlist mark='opencircle'>
  336. <listitem>
  337. <para>
  338. <emphasis role="strong">getSymbol()</emphasis>:
  339. </para>
  340. <para>
  341. Returns the known symbol of the set currency or a given currency. For example
  342. <emphasis role="strong">$</emphasis> for the US Dollar within the locale
  343. '<emphasis role="strong">en_US</emphasis>.
  344. </para>
  345. </listitem>
  346. <listitem>
  347. <para>
  348. <emphasis role="strong">getShortName()</emphasis>:
  349. </para>
  350. <para>
  351. Returns the abbreviation of the set currency or a given currency. For example
  352. <emphasis role="strong">USD</emphasis> for the US Dollar within the locale
  353. '<emphasis role="strong">en_US</emphasis>.
  354. </para>
  355. </listitem>
  356. <listitem>
  357. <para>
  358. <emphasis role="strong">getName()</emphasis>:
  359. </para>
  360. <para>
  361. Returns the full name of the set currency of a given currency. For example
  362. <emphasis role="strong">US Dollar</emphasis> for the US Dollar within the locale
  363. '<emphasis role="strong">en_US</emphasis>.
  364. </para>
  365. </listitem>
  366. <listitem>
  367. <para>
  368. <emphasis role="strong">getRegionList()</emphasis>:
  369. </para>
  370. <para>
  371. Returns a list of regions where the set currency or a given one is known to be used.
  372. It is possible that a currency is used within several regions, so the return value
  373. is always an array.
  374. </para>
  375. </listitem>
  376. <listitem>
  377. <para>
  378. <emphasis role="strong">getCurrencyList()</emphasis>:
  379. </para>
  380. <para>
  381. Returns a list of currencies which are used in the given region.
  382. </para>
  383. </listitem>
  384. </itemizedlist>
  385. <para>
  386. The function <code>getSymbol()</code>, <code>getShortName()</code> and <code>getName()</code> accept
  387. two optional parameters. If no parameter is given the expected data will be returned from the
  388. set currency. The first parameter takes the shortname of a currency. Short names are always three
  389. lettered, for example EUR for euro or USD for US Dollar. The second parameter defines from which
  390. locale the data should be read. If no locale is given, the set locale is used.
  391. </para>
  392. <example id="zend.currency.usage.informational.example">
  393. <title>Getting Information about Currencies</title>
  394. <programlisting role="php"><![CDATA[
  395. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US'
  396. // as these are the default values from 'en_US'
  397. $currency = new Zend_Currency('en_US');
  398. // prints '$'
  399. echo $currency->getSymbol();
  400. // prints 'EUR'
  401. echo $currency->getShortName('EUR');
  402. // prints 'Österreichische Schilling'
  403. echo $currency->getName('ATS', 'de_AT');
  404. // returns an array with all regions where USD is used
  405. print_r($currency->getRegionList();
  406. // returns an array with all currencies which were ever used in this
  407. // region
  408. print_r($currency->getCurrencyList('de_AT');
  409. ]]></programlisting>
  410. </example>
  411. </sect2>
  412. <sect2 id="zend.currency.usage.setlocale">
  413. <title>Settings new default values</title>
  414. <para>
  415. The method <code>setLocale</code> allows to set a new locale for
  416. <classname>Zend_Currency</classname>. All default
  417. values for the currency will be overwritten when this method is invoked. This includes currency name,
  418. abbreviation and symbol.
  419. </para>
  420. <example id="zend.currency.usage.setlocale.example">
  421. <title>Setting a New Locale</title>
  422. <programlisting role="php"><![CDATA[
  423. // get the currency for US
  424. $currency = new Zend_Currency('en_US');
  425. print $currency->toCurrency(1000);
  426. // get the currency for AT
  427. $currency->setLocale('de_AT');
  428. print $currency->toCurrency(1000);
  429. ]]></programlisting>
  430. </example>
  431. </sect2>
  432. <sect2 id="zend.currency.usage.cache">
  433. <title>Zend_Currency Performance Optimization</title>
  434. <para>
  435. <classname>Zend_Currency</classname>'s performance can be optimized using <classname>Zend_Cache</classname>.
  436. The static method <classname>Zend_Currency::setCache($cache)</classname> accepts one option: a
  437. <classname>Zend_Cache</classname> adapter. If the cache adapter is set, the localization data that
  438. Zend_Currency uses are cached. There are some static methods for manipulating the cache:
  439. <code>getCache()</code>, <code>hasCache()</code>, <code>clearCache()</code> and
  440. <code>removeCache()</code>.
  441. </para>
  442. <example id="zend.currency.usage.cache.example">
  443. <title>Caching currencies</title>
  444. <programlisting role="php"><![CDATA[
  445. // creating a cache object
  446. $cache = Zend_Cache::factory('Core',
  447. 'File',
  448. array('lifetime' => 120,
  449. 'automatic_serialization' => true),
  450. array('cache_dir'
  451. => dirname(__FILE__) . '/_files/'));
  452. Zend_Currency::setCache($cache);
  453. ]]></programlisting>
  454. </example>
  455. </sect2>
  456. </sect1>