Zend_Currency-Usage.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 language="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 language="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>currency</emphasis>:
  49. </para>
  50. <para>
  51. A locale can include several currencies. Therefore the first parameter
  52. <emphasis>'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>locale</emphasis>:
  61. </para>
  62. <para>
  63. The <emphasis>'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>en</emphasis> will cause an exception to be thrown whereas the locale
  71. <emphasis>en_US</emphasis> will return <emphasis>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 language="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>currency</emphasis>' parameter
  99. is suppressed the contemporary currency will be used. The region '<emphasis>de</emphasis>' for
  100. example knows the currencies '<emphasis>EUR</emphasis>' and
  101. '<emphasis>DEM</emphasis>'... '<emphasis>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>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 language="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>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>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>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 the chapter
  172. <link linkend="zend.locale.numbersystems" />.
  173. </para>
  174. </listitem>
  175. <listitem>
  176. <para>
  177. <emphasis>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>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>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>script</emphasis>: Defined which script should be used for
  203. displaying digits. The default script for most locales is
  204. <emphasis>'Latn'</emphasis>, which includes the digits 0 to 9. Other
  205. scripts such as 'Arab' (Arabian) can be used.
  206. </para>
  207. </listitem>
  208. <listitem>
  209. <para>
  210. <emphasis>format</emphasis>: Defines the format which should be used for displaying
  211. numbers. This number-format includes for example the thousand separator. You can eighter use
  212. a default format by giving a locale identifier, or define the number-format manually.
  213. If no format is set the locale from the <classname>Zend_Currency</classname> object will be used.
  214. </para>
  215. </listitem>
  216. <listitem>
  217. <para>
  218. <emphasis>display</emphasis>: Defines which part of the currency should be
  219. used for displaying the currency representation. There are 4 representations which can be
  220. used and which are all described in
  221. <link linkend="zend.currency.usage.setformat.constantsdescription">this table</link>.
  222. </para>
  223. </listitem>
  224. <listitem>
  225. <para>
  226. <emphasis>precision</emphasis>: Defines the precision which should be used for
  227. the currency representation. The default value is <emphasis>2</emphasis>.
  228. </para>
  229. </listitem>
  230. <listitem>
  231. <para>
  232. <emphasis>name</emphasis>: Defines the full currency name which should be
  233. displayed. This option overwrites any currency name which is set through
  234. the creation of <classname>Zend_Currency</classname>.
  235. </para>
  236. </listitem>
  237. <listitem>
  238. <para>
  239. <emphasis>currency</emphasis>: Defines the international abbreviation which
  240. should be displayed. This option overwrites any abbreviation which is set through
  241. the creation of <classname>Zend_Currency</classname>.
  242. </para>
  243. </listitem>
  244. <listitem>
  245. <para>
  246. <emphasis>symbol</emphasis>: Defines the currency symbol which should be
  247. displayed. This option overwrites any symbol which is set through
  248. the creation of <classname>Zend_Currency</classname>.
  249. </para>
  250. </listitem>
  251. </itemizedlist>
  252. <table id="zend.currency.usage.setformat.constantsdescription">
  253. <title>Constants for the selecting the currency description</title>
  254. <tgroup cols="2" align="left">
  255. <thead>
  256. <row>
  257. <entry>constant</entry>
  258. <entry>description</entry>
  259. </row>
  260. </thead>
  261. <tbody>
  262. <row>
  263. <entry>NO_SYMBOL</entry>
  264. <entry>Do not display any currency representation</entry>
  265. </row>
  266. <row>
  267. <entry>USE_SYMBOL</entry>
  268. <entry>Display the currency symbol</entry>
  269. </row>
  270. <row>
  271. <entry>USE_SHORTNAME</entry>
  272. <entry>Display the 3 lettered international currency abbreviation</entry>
  273. </row>
  274. <row>
  275. <entry>USE_NAME</entry>
  276. <entry>Display the full currency name</entry>
  277. </row>
  278. </tbody>
  279. </tgroup>
  280. </table>
  281. <table id="zend.currency.usage.setformat.constantsposition">
  282. <title>Constants for the selecting the position of the currency description</title>
  283. <tgroup cols="2" align="left">
  284. <thead>
  285. <row>
  286. <entry>constant</entry>
  287. <entry>description</entry>
  288. </row>
  289. </thead>
  290. <tbody>
  291. <row>
  292. <entry>STANDARD</entry>
  293. <entry>Set the standard position as defined within the locale</entry>
  294. </row>
  295. <row>
  296. <entry>RIGHT</entry>
  297. <entry>Display the currency representation at the right side of the value</entry>
  298. </row>
  299. <row>
  300. <entry>LEFT</entry>
  301. <entry>Display the currency representation at the left side of the value</entry>
  302. </row>
  303. </tbody>
  304. </tgroup>
  305. </table>
  306. <example id="zend.currency.usage.setformat.example">
  307. <title>Changing the displayed format of a currency</title>
  308. <programlisting language="php"><![CDATA[
  309. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as
  310. // these are the default values from 'en_US'
  311. $currency = new Zend_Currency('en_US');
  312. // prints 'US$ 1,000.00'
  313. echo $currency->toCurrency(1000);
  314. $currency->setFormat(array('display' => Zend_Currency::USE_NAME,
  315. 'position' => Zend_Currency::RIGHT));
  316. // prints '1.000,00 US Dollar'
  317. echo $currency->toCurrency(1000);
  318. $currency->setFormat(array('name' => 'American Dollar'));
  319. // prints '1.000,00 American Dollar'
  320. echo $currency->toCurrency(1000);
  321. $currency->setFormat(array('format' => '##0.00'));
  322. // prints '1000,00 American Dollar'
  323. echo $currency->toCurrency(1000);
  324. ]]></programlisting>
  325. </example>
  326. </sect2>
  327. <sect2 id="zend.currency.usage.informational">
  328. <title>Reference Methods for Zend_Currency</title>
  329. <para>
  330. Of course, <classname>Zend_Currency</classname> supports also methods to get information about any existing
  331. and many ancient currencies from <classname>Zend_Locale</classname>. The supported
  332. methods are:
  333. </para>
  334. <itemizedlist mark='opencircle'>
  335. <listitem>
  336. <para>
  337. <emphasis>getSymbol()</emphasis>:
  338. </para>
  339. <para>
  340. Returns the known symbol of the set currency or a given currency. For example
  341. <emphasis>$</emphasis> for the US Dollar within the locale
  342. '<emphasis>en_US</emphasis>.
  343. </para>
  344. </listitem>
  345. <listitem>
  346. <para>
  347. <emphasis>getShortName()</emphasis>:
  348. </para>
  349. <para>
  350. Returns the abbreviation of the set currency or a given currency. For example
  351. <emphasis>USD</emphasis> for the US Dollar within the locale
  352. '<emphasis>en_US</emphasis>.
  353. </para>
  354. </listitem>
  355. <listitem>
  356. <para>
  357. <emphasis>getName()</emphasis>:
  358. </para>
  359. <para>
  360. Returns the full name of the set currency of a given currency. For example
  361. <emphasis>US Dollar</emphasis> for the US Dollar within the locale
  362. '<emphasis>en_US</emphasis>.
  363. </para>
  364. </listitem>
  365. <listitem>
  366. <para>
  367. <emphasis>getRegionList()</emphasis>:
  368. </para>
  369. <para>
  370. Returns a list of regions where the set currency or a given one is known to be used.
  371. It is possible that a currency is used within several regions, so the return value
  372. is always an array.
  373. </para>
  374. </listitem>
  375. <listitem>
  376. <para>
  377. <emphasis>getCurrencyList()</emphasis>:
  378. </para>
  379. <para>
  380. Returns a list of currencies which are used in the given region.
  381. </para>
  382. </listitem>
  383. </itemizedlist>
  384. <para>
  385. The function <code>getSymbol()</code>, <code>getShortName()</code> and <code>getName()</code> accept
  386. two optional parameters. If no parameter is given the expected data will be returned from the
  387. set currency. The first parameter takes the shortname of a currency. Short names are always three
  388. lettered, for example EUR for euro or USD for US Dollar. The second parameter defines from which
  389. locale the data should be read. If no locale is given, the set locale is used.
  390. </para>
  391. <example id="zend.currency.usage.informational.example">
  392. <title>Getting Information about Currencies</title>
  393. <programlisting language="php"><![CDATA[
  394. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US'
  395. // as these are the default values from 'en_US'
  396. $currency = new Zend_Currency('en_US');
  397. // prints '$'
  398. echo $currency->getSymbol();
  399. // prints 'EUR'
  400. echo $currency->getShortName('EUR');
  401. // prints 'Österreichische Schilling'
  402. echo $currency->getName('ATS', 'de_AT');
  403. // returns an array with all regions where USD is used
  404. print_r($currency->getRegionList();
  405. // returns an array with all currencies which were ever used in this
  406. // region
  407. print_r($currency->getCurrencyList('de_AT');
  408. ]]></programlisting>
  409. </example>
  410. </sect2>
  411. <sect2 id="zend.currency.usage.setlocale">
  412. <title>Settings new default values</title>
  413. <para>
  414. The method <code>setLocale</code> allows to set a new locale for
  415. <classname>Zend_Currency</classname>. All default
  416. values for the currency will be overwritten when this method is invoked. This includes currency name,
  417. abbreviation and symbol.
  418. </para>
  419. <example id="zend.currency.usage.setlocale.example">
  420. <title>Setting a New Locale</title>
  421. <programlisting language="php"><![CDATA[
  422. // get the currency for US
  423. $currency = new Zend_Currency('en_US');
  424. print $currency->toCurrency(1000);
  425. // get the currency for AT
  426. $currency->setLocale('de_AT');
  427. print $currency->toCurrency(1000);
  428. ]]></programlisting>
  429. </example>
  430. </sect2>
  431. <sect2 id="zend.currency.usage.cache">
  432. <title>Zend_Currency Performance Optimization</title>
  433. <para>
  434. <classname>Zend_Currency</classname>'s performance can be optimized using <classname>Zend_Cache</classname>.
  435. The static method <classname>Zend_Currency::setCache($cache)</classname> accepts one option: a
  436. <classname>Zend_Cache</classname> adapter. If the cache adapter is set, the localization data that
  437. Zend_Currency uses are cached. There are some static methods for manipulating the cache:
  438. <code>getCache()</code>, <code>hasCache()</code>, <code>clearCache()</code> and
  439. <code>removeCache()</code>.
  440. </para>
  441. <example id="zend.currency.usage.cache.example">
  442. <title>Caching currencies</title>
  443. <programlisting language="php"><![CDATA[
  444. // creating a cache object
  445. $cache = Zend_Cache::factory('Core',
  446. 'File',
  447. array('lifetime' => 120,
  448. 'automatic_serialization' => true),
  449. array('cache_dir'
  450. => dirname(__FILE__) . '/_files/'));
  451. Zend_Currency::setCache($cache);
  452. ]]></programlisting>
  453. </example>
  454. </sect2>
  455. </sect1>