Zend_Currency-Usage.xml 22 KB

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