CurrencyLib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * Other webservices:
  4. * - http://www.webservicex.net/WS/WSDetails.aspx?WSID=10 (XML)
  5. * - http://www.webserviceshare.com/business/financial/currency/service/Noon-Foreign-Exchange-Rates.htm (XML)
  6. */
  7. App::uses('HttpSocket', 'Network/Http');
  8. App::uses('Xml', 'Utility');
  9. /**
  10. * Component to retreive calculate currencies
  11. *
  12. * example:
  13. * $result = $this->Currency->convert(2.5, 'EUR', 'USD');
  14. *
  15. * from: http://www.studiocanaria.com/articles/cakephp_currency_conversion_component
  16. * alternativly: http://www.currencyserver.de/webservice/CurrencyServerWebService.asmx/getXmlStream?provider=AVERAGE
  17. *
  18. * @author Mark Scherer
  19. * @license http://opensource.org/licenses/mit-license.php MIT
  20. */
  21. class CurrencyLib {
  22. const URL = 'http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml';
  23. const URL_HISTORY = 'http://www.ecb.int/stats/eurofxref/eurofxref-hist.xml';
  24. //TODO: get information about a currency (name, ...)
  25. const URL_TABLE = 'http://www.ecb.int/rss/fxref-{currency}.html';
  26. public $baseCurrency = 'EUR';
  27. public $includeBitcoin = true;
  28. public $cacheFileUsed = false;
  29. public $cacheTime = DAY;
  30. /**
  31. * Converts the $amount from $fromCurrency to $toCurrency, formatted to
  32. * $decimals decimal places.
  33. *
  34. * @return float [Converted Currency Amount] or boolean FALSE on failure
  35. * @param $amount float
  36. * @param $fromCurrency string
  37. * @param $toCurrency string
  38. * @param $decimals integer[optional]default=2
  39. */
  40. public function convert($amount, $fromCurrency, $toCurrency, $decimals = 2) {
  41. //Get the rate table
  42. $rates = $this->_retrieveCurrencies();
  43. //Return result of conversion
  44. if (!array_key_exists($fromCurrency, $rates) || !array_key_exists($toCurrency, $rates)) {
  45. return false;
  46. }
  47. return number_format($amount / $rates[$fromCurrency] * $rates[$toCurrency], $decimals);
  48. }
  49. /**
  50. * Returns an array of rates in comparison the the $base currency given to $decimals
  51. * number of decimal places.
  52. *
  53. * @param $base string[optional]default='EUR'
  54. * @param $decimals integer[optional]default=2
  55. * @return array table or boolean FALSE on failure
  56. */
  57. public function table($base = 'EUR', $decimals = 2) {
  58. //Create array to holds rates
  59. $rateTable = [];
  60. //Get rate table array
  61. $rates = $this->_retrieveCurrencies();
  62. if (!array_key_exists($base, $rates)) {
  63. return false;
  64. }
  65. //Iterate throught each rate converting it against $base
  66. foreach ($rates as $key => $value) {
  67. $rate = 0;
  68. if ($value > 0) {
  69. $rate = 1 / $rates[$base] * $rates[$key];
  70. }
  71. $rateTable[$key] = number_format($rate, $decimals);
  72. }
  73. //Return result array
  74. return $rateTable;
  75. }
  76. /**
  77. * CurrencyLib::history()
  78. *
  79. * @param string $date Date in Format XXXX-XX-XX
  80. * @return array
  81. */
  82. public function history($date = null) {
  83. $history = $this->_retrieveHistory();
  84. if ($date) {
  85. if (!empty($history[$date])) {
  86. return $history[$date];
  87. }
  88. return [];
  89. }
  90. return $history;
  91. }
  92. /**
  93. * CurrencyComponent::isAvailable()
  94. *
  95. * @param mixed $currency
  96. * @return bool Success.
  97. */
  98. public function isAvailable($currency) {
  99. $rates = $this->_retrieveCurrencies();
  100. return array_key_exists($currency, $rates);
  101. }
  102. /**
  103. * @param string $name: "" (none), "history", "full" (both)
  104. * @return bool Success.
  105. */
  106. public function reset($name = 'full') {
  107. if ($name === 'full') {
  108. $name = '';
  109. Cache::delete('currencyListHistory');
  110. }
  111. return Cache::delete('currencyList' . ucfirst($name));
  112. }
  113. public function cacheFileUsed() {
  114. return $this->cacheFileUsed;
  115. }
  116. /**
  117. * @param string $code (3digit - e.g. EUR)
  118. * @param mixed $default (defaults to bool false)
  119. */
  120. public function getName($currency, $default = false) {
  121. if (empty($currency)) {
  122. return $default;
  123. }
  124. $currency = strtoupper($currency);
  125. $currencies = $this->currencies;
  126. if ($this->includeBitcoin) {
  127. $currencies['BTC'] = 'Bitcoin';
  128. }
  129. if (array_key_exists($currency, $currencies)) {
  130. return $currencies[$currency];
  131. }
  132. return $default;
  133. }
  134. /**
  135. * CurrencyComponent::_retrieveHistory()
  136. *
  137. * @return array
  138. */
  139. protected function _retrieveHistory() {
  140. if ($historyList = $this->_retrieve('history')) {
  141. return $historyList;
  142. }
  143. $currencies = $this->_loadXml(static::URL_HISTORY);
  144. // Filter down to just the rates
  145. $dates = $currencies['Envelope']['Cube']['Cube'];
  146. $historyList = [];
  147. foreach ($dates as $date) {
  148. $time = $date['@time'];
  149. foreach ($date['Cube'] as $currency) {
  150. $historyList[$time][$currency['@currency']] = $currency['@rate'];
  151. // European Central bank gives us everything against Euro so add this manually
  152. $historyList[$time][$this->baseCurrency] = 1;
  153. }
  154. }
  155. $this->_store($historyList, 'history');
  156. return $historyList;
  157. }
  158. /**
  159. * CurrencyComponent::_retrieveCurrencies()
  160. *
  161. * @return array
  162. */
  163. protected function _retrieveCurrencies() {
  164. if ($currencyList = $this->_retrieve()) {
  165. return $currencyList;
  166. }
  167. // Retrieve rates as an XML object
  168. $currencies = $this->_loadXml(static::URL);
  169. //Filter down to just the rates
  170. $currencies = $currencies['Envelope']['Cube']['Cube']['Cube'];
  171. //Create an array to hold the rates
  172. $currencyList = [];
  173. //European Central bank gives us everything against Euro so add this manually
  174. $currencyList[$this->baseCurrency] = 1;
  175. //Now iterate through and add the rates provided
  176. foreach ($currencies as $currency) {
  177. $currencyList[$currency['@currency']] = $currency['@rate'];
  178. }
  179. if ($this->includeBitcoin && ($res = $this->_getBitcoin())) {
  180. $currencyList['BTC'] = $res;
  181. }
  182. //Cache
  183. $this->_store($currencyList);
  184. return $currencyList;
  185. }
  186. protected function _getBitcoin() {
  187. App::uses('CurrencyBitcoinLib', 'Tools.Lib');
  188. $Btc = new CurrencyBitcoinLib();
  189. return $Btc->rate(['currency' => $this->baseCurrency]);
  190. }
  191. /**
  192. * @param string $name: "" (none), "history", "full" (both)
  193. */
  194. protected function _store($currencyList, $name = '') {
  195. $this->cacheFileUsed = false;
  196. Cache::write('currencyList' . ucfirst($name), serialize($currencyList));
  197. }
  198. /**
  199. * @param string $name: "" (none), "history", "full" (both)
  200. */
  201. protected function _retrieve($name = '') {
  202. $res = Cache::read('currencyList' . ucfirst($name));
  203. if ($res !== false) {
  204. $this->cacheFileUsed = true;
  205. return unserialize($res);
  206. }
  207. return false;
  208. }
  209. /**
  210. * CurrencyLib::_loadXml()
  211. *
  212. * @param string $url
  213. * @return array
  214. */
  215. protected function _loadXml($url) {
  216. $CurrencyXml = Xml::build($url);
  217. return Xml::toArray($CurrencyXml);
  218. }
  219. public $currencies = [
  220. 'AFA' => 'Afghanistan Afghani',
  221. 'ALL' => 'Albanian Lek',
  222. 'DZD' => 'Algerian Dinar',
  223. 'ARS' => 'Argentine Peso',
  224. 'AWG' => 'Aruba Florin',
  225. 'AUD' => 'Australian Dollar',
  226. 'BSD' => 'Bahamian Dollar',
  227. 'BHD' => 'Bahraini Dinar',
  228. 'BDT' => 'Bangladesh Taka',
  229. 'BBD' => 'Barbados Dollar',
  230. 'BZD' => 'Belize Dollar',
  231. 'BMD' => 'Bermuda Dollar',
  232. 'BTN' => 'Bhutan Ngultrum',
  233. 'BOB' => 'Bolivian Boliviano',
  234. 'BWP' => 'Botswana Pula',
  235. 'BRL' => 'Brazilian Real',
  236. 'GBP' => 'British Pound',
  237. 'BND' => 'Brunei Dollar',
  238. 'BIF' => 'Burundi Franc',
  239. 'XOF' => 'CFA Franc (BCEAO)',
  240. 'XAF' => 'CFA Franc (BEAC)',
  241. 'KHR' => 'Cambodia Riel',
  242. 'CAD' => 'Canadian Dollar',
  243. 'CVE' => 'Cape Verde Escudo',
  244. 'KYD' => 'Cayman Islands Dollar',
  245. 'CLP' => 'Chilean Peso',
  246. 'CNY' => 'Chinese Yuan',
  247. 'COP' => 'Colombian Peso',
  248. 'KMF' => 'Comoros Franc',
  249. 'CRC' => 'Costa Rica Colon',
  250. 'HRK' => 'Croatian Kuna',
  251. 'CUP' => 'Cuban Peso',
  252. 'CYP' => 'Cyprus Pound',
  253. 'CZK' => 'Czech Koruna',
  254. 'DKK' => 'Danish Krone',
  255. 'DJF' => 'Dijibouti Franc',
  256. 'DOP' => 'Dominican Peso',
  257. 'XCD' => 'East Caribbean Dollar',
  258. 'EGP' => 'Egyptian Pound',
  259. 'SVC' => 'El Salvador Colon',
  260. 'EEK' => 'Estonian Kroon',
  261. 'ETB' => 'Ethiopian Birr',
  262. 'EUR' => 'Euro',
  263. 'FKP' => 'Falkland Islands Pound',
  264. 'GMD' => 'Gambian Dalasi',
  265. 'GHC' => 'Ghanian Cedi',
  266. 'GIP' => 'Gibraltar Pound',
  267. 'XAU' => 'Gold Ounces',
  268. 'GTQ' => 'Guatemala Quetzal',
  269. 'GNF' => 'Guinea Franc',
  270. 'GYD' => 'Guyana Dollar',
  271. 'HTG' => 'Haiti Gourde',
  272. 'HNL' => 'Honduras Lempira',
  273. 'HKD' => 'Hong Kong Dollar',
  274. 'HUF' => 'Hungarian Forint',
  275. 'ISK' => 'Iceland Krona',
  276. 'INR' => 'Indian Rupee',
  277. 'IDR' => 'Indonesian Rupiah',
  278. 'IQD' => 'Iraqi Dinar',
  279. 'ILS' => 'Israeli Shekel',
  280. 'JMD' => 'Jamaican Dollar',
  281. 'JPY' => 'Japanese Yen',
  282. 'JOD' => 'Jordanian Dinar',
  283. 'KZT' => 'Kazakhstan Tenge',
  284. 'KES' => 'Kenyan Shilling',
  285. 'KRW' => 'Korean Won',
  286. 'KWD' => 'Kuwaiti Dinar',
  287. 'LAK' => 'Lao Kip',
  288. 'LVL' => 'Latvian Lat',
  289. 'LBP' => 'Lebanese Pound',
  290. 'LSL' => 'Lesotho Loti',
  291. 'LRD' => 'Liberian Dollar',
  292. 'LYD' => 'Libyan Dinar',
  293. 'LTL' => 'Lithuanian Lita',
  294. 'MOP' => 'Macau Pataca',
  295. 'MKD' => 'Macedonian Denar',
  296. 'MGF' => 'Malagasy Franc',
  297. 'MWK' => 'Malawi Kwacha',
  298. 'MYR' => 'Malaysian Ringgit',
  299. 'MVR' => 'Maldives Rufiyaa',
  300. 'MTL' => 'Maltese Lira',
  301. 'MRO' => 'Mauritania Ougulya',
  302. 'MUR' => 'Mauritius Rupee',
  303. 'MXN' => 'Mexican Peso',
  304. 'MDL' => 'Moldovan Leu',
  305. 'MNT' => 'Mongolian Tugrik',
  306. 'MAD' => 'Moroccan Dirham',
  307. 'MZM' => 'Mozambique Metical',
  308. 'MMK' => 'Myanmar Kyat',
  309. 'NAD' => 'Namibian Dollar',
  310. 'NPR' => 'Nepalese Rupee',
  311. 'ANG' => 'Neth Antilles Guilder',
  312. 'NZD' => 'New Zealand Dollar',
  313. 'NIO' => 'Nicaragua Cordoba',
  314. 'NGN' => 'Nigerian Naira',
  315. 'KPW' => 'North Korean Won',
  316. 'NOK' => 'Norwegian Krone',
  317. 'OMR' => 'Omani Rial',
  318. 'XPF' => 'Pacific Franc',
  319. 'PKR' => 'Pakistani Rupee',
  320. 'XPD' => 'Palladium Ounces',
  321. 'PAB' => 'Panama Balboa',
  322. 'PGK' => 'Papua New Guinea Kina',
  323. 'PYG' => 'Paraguayan Guarani',
  324. 'PEN' => 'Peruvian Nuevo Sol',
  325. 'PHP' => 'Philippine Peso',
  326. 'XPT' => 'Platinum Ounces',
  327. 'PLN' => 'Polish Zloty',
  328. 'QAR' => 'Qatar Rial',
  329. 'ROL' => 'Romanian Leu',
  330. 'RUB' => 'Russian Rouble',
  331. 'WST' => 'Samoa Tala',
  332. 'STD' => 'Sao Tome Dobra',
  333. 'SAR' => 'Saudi Arabian Riyal',
  334. 'SCR' => 'Seychelles Rupee',
  335. 'SLL' => 'Sierra Leone Leone',
  336. 'XAG' => 'Silver Ounces',
  337. 'SGD' => 'Singapore Dollar',
  338. 'SKK' => 'Slovak Koruna',
  339. 'SIT' => 'Slovenian Tolar',
  340. 'SBD' => 'Solomon Islands Dollar',
  341. 'SOS' => 'Somali Shilling',
  342. 'ZAR' => 'South African Rand',
  343. 'LKR' => 'Sri Lanka Rupee',
  344. 'SHP' => 'St Helena Pound',
  345. 'SDD' => 'Sudanese Dinar',
  346. 'SRG' => 'Surinam Guilder',
  347. 'SZL' => 'Swaziland Lilageni',
  348. 'SEK' => 'Swedish Krona',
  349. 'TRY' => 'Turkey Lira',
  350. 'CHF' => 'Swiss Franc',
  351. 'SYP' => 'Syrian Pound',
  352. 'TWD' => 'Taiwan Dollar',
  353. 'TZS' => 'Tanzanian Shilling',
  354. 'THB' => 'Thai Baht',
  355. 'TOP' => 'Tonga Pa\'anga',
  356. 'TTD' => 'Trinidad & Tobago Dollar',
  357. 'TND' => 'Tunisian Dinar',
  358. 'TRL' => 'Turkish Lira',
  359. 'USD' => 'U.S. Dollar',
  360. 'AED' => 'UAE Dirham',
  361. 'UGX' => 'Ugandan Shilling',
  362. 'UAH' => 'Ukraine Hryvnia',
  363. 'UYU' => 'Uruguayan New Peso',
  364. 'VUV' => 'Vanuatu Vatu',
  365. 'VEB' => 'Venezuelan Bolivar',
  366. 'VND' => 'Vietnam Dong',
  367. 'YER' => 'Yemen Riyal',
  368. 'YUM' => 'Yugoslav Dinar',
  369. 'ZMK' => 'Zambian Kwacha',
  370. 'ZWD' => 'Zimbabwe Dollar',
  371. ];
  372. }