PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Currency.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 902 lines | 499 code | 106 blank | 297 comment | 93 complexity | 18786af6ff60d31f519289d4338de015 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Currency
  17. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Currency.php 24855 2012-06-01 00:12:25Z adamlundrigan $
  20. */
  21. /**
  22. * include needed classes
  23. */
  24. require_once 'Zend/Locale.php';
  25. require_once 'Zend/Locale/Data.php';
  26. require_once 'Zend/Locale/Format.php';
  27. /**
  28. * Class for handling currency notations
  29. *
  30. * @category Zend
  31. * @package Zend_Currency
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Currency
  36. {
  37. // Constants for defining what currency symbol should be displayed
  38. const NO_SYMBOL = 1;
  39. const USE_SYMBOL = 2;
  40. const USE_SHORTNAME = 3;
  41. const USE_NAME = 4;
  42. // Constants for defining the position of the currencysign
  43. const STANDARD = 8;
  44. const RIGHT = 16;
  45. const LEFT = 32;
  46. /**
  47. * Options array
  48. *
  49. * The following options are available
  50. * 'position' => Position for the currency sign
  51. * 'script' => Script for the output
  52. * 'format' => Locale for numeric output
  53. * 'display' => Currency detail to show
  54. * 'precision' => Precision for the currency
  55. * 'name' => Name for this currency
  56. * 'currency' => 3 lettered international abbreviation
  57. * 'symbol' => Currency symbol
  58. * 'locale' => Locale for this currency
  59. * 'value' => Money value
  60. * 'service' => Exchange service to use
  61. *
  62. * @var array
  63. * @see Zend_Locale
  64. */
  65. protected $_options = array(
  66. 'position' => self::STANDARD,
  67. 'script' => null,
  68. 'format' => null,
  69. 'display' => self::NO_SYMBOL,
  70. 'precision' => 2,
  71. 'name' => null,
  72. 'currency' => null,
  73. 'symbol' => null,
  74. 'locale' => null,
  75. 'value' => 0,
  76. 'service' => null,
  77. 'tag' => 'Zend_Locale'
  78. );
  79. /**
  80. * Creates a currency instance. Every supressed parameter is used from the actual or the given locale.
  81. *
  82. * @param string|array $options OPTIONAL Options array or currency short name
  83. * when string is given
  84. * @param string|Zend_Locale $locale OPTIONAL locale name
  85. * @throws Zend_Currency_Exception When currency is invalid
  86. */
  87. public function __construct($options = null, $locale = null)
  88. {
  89. $calloptions = $options;
  90. if (is_array($options) && isset($options['display'])) {
  91. $this->_options['display'] = $options['display'];
  92. }
  93. if (is_array($options)) {
  94. $this->setLocale($locale);
  95. $this->setFormat($options);
  96. } else if (Zend_Locale::isLocale($options, false, false)) {
  97. $this->setLocale($options);
  98. $options = $locale;
  99. } else {
  100. $this->setLocale($locale);
  101. }
  102. // Get currency details
  103. if (!isset($this->_options['currency']) || !is_array($options)) {
  104. $this->_options['currency'] = self::getShortName($options, $this->_options['locale']);
  105. }
  106. if (!isset($this->_options['name']) || !is_array($options)) {
  107. $this->_options['name'] = self::getName($options, $this->_options['locale']);
  108. }
  109. if (!isset($this->_options['symbol']) || !is_array($options)) {
  110. $this->_options['symbol'] = self::getSymbol($options, $this->_options['locale']);
  111. }
  112. if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
  113. require_once 'Zend/Currency/Exception.php';
  114. throw new Zend_Currency_Exception("Currency '$options' not found");
  115. }
  116. // Get the format
  117. if ((is_array($calloptions) && !isset($calloptions['display']))
  118. || (!is_array($calloptions) && $this->_options['display'] == self::NO_SYMBOL)) {
  119. if (!empty($this->_options['symbol'])) {
  120. $this->_options['display'] = self::USE_SYMBOL;
  121. } else if (!empty($this->_options['currency'])) {
  122. $this->_options['display'] = self::USE_SHORTNAME;
  123. }
  124. }
  125. }
  126. /**
  127. * Returns a localized currency string
  128. *
  129. * @param integer|float $value OPTIONAL Currency value
  130. * @param array $options OPTIONAL options to set temporary
  131. * @throws Zend_Currency_Exception When the value is not a number
  132. * @return string
  133. */
  134. public function toCurrency($value = null, array $options = array())
  135. {
  136. if ($value === null) {
  137. if (is_array($options) && isset($options['value'])) {
  138. $value = $options['value'];
  139. } else {
  140. $value = $this->_options['value'];
  141. }
  142. }
  143. if (is_array($value)) {
  144. $options += $value;
  145. if (isset($options['value'])) {
  146. $value = $options['value'];
  147. }
  148. }
  149. // Validate the passed number
  150. if (!(isset($value)) or (is_numeric($value) === false)) {
  151. require_once 'Zend/Currency/Exception.php';
  152. throw new Zend_Currency_Exception("Value '$value' has to be numeric");
  153. }
  154. if (isset($options['currency'])) {
  155. if (!isset($options['locale'])) {
  156. $options['locale'] = $this->_options['locale'];
  157. }
  158. $options['currency'] = self::getShortName($options['currency'], $options['locale']);
  159. $options['name'] = self::getName($options['currency'], $options['locale']);
  160. $options['symbol'] = self::getSymbol($options['currency'], $options['locale']);
  161. }
  162. $options = $this->_checkOptions($options) + $this->_options;
  163. // Format the number
  164. $format = $options['format'];
  165. $locale = $options['locale'];
  166. if (empty($format)) {
  167. $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
  168. } else if (Zend_Locale::isLocale($format, true, false)) {
  169. $locale = $format;
  170. $format = Zend_Locale_Data::getContent($format, 'currencynumber');
  171. }
  172. $original = $value;
  173. $value = Zend_Locale_Format::toNumber($value, array('locale' => $locale,
  174. 'number_format' => $format,
  175. 'precision' => $options['precision']));
  176. if ($options['position'] !== self::STANDARD) {
  177. $value = str_replace('¤', '', $value);
  178. $space = '';
  179. if (iconv_strpos($value, ' ') !== false) {
  180. $value = str_replace(' ', '', $value);
  181. $space = ' ';
  182. }
  183. if ($options['position'] == self::LEFT) {
  184. $value = '¤' . $space . $value;
  185. } else {
  186. $value = $value . $space . '¤';
  187. }
  188. }
  189. // Localize the number digits
  190. if (empty($options['script']) === false) {
  191. $value = Zend_Locale_Format::convertNumerals($value, 'Latn', $options['script']);
  192. }
  193. // Get the sign to be placed next to the number
  194. if (is_numeric($options['display']) === false) {
  195. $sign = $options['display'];
  196. } else {
  197. switch($options['display']) {
  198. case self::USE_SYMBOL:
  199. $sign = $this->_extractPattern($options['symbol'], $original);
  200. break;
  201. case self::USE_SHORTNAME:
  202. $sign = $options['currency'];
  203. break;
  204. case self::USE_NAME:
  205. $sign = $options['name'];
  206. break;
  207. default:
  208. $sign = '';
  209. $value = str_replace(' ', '', $value);
  210. break;
  211. }
  212. }
  213. $value = str_replace('¤', $sign, $value);
  214. return $value;
  215. }
  216. /**
  217. * Internal method to extract the currency pattern
  218. * when a choice is given based on the given value
  219. *
  220. * @param string $pattern
  221. * @param float|integer $value
  222. * @return string
  223. */
  224. private function _extractPattern($pattern, $value)
  225. {
  226. if (strpos($pattern, '|') === false) {
  227. return $pattern;
  228. }
  229. $patterns = explode('|', $pattern);
  230. $token = $pattern;
  231. $value = trim(str_replace('¤', '', $value));
  232. krsort($patterns);
  233. foreach($patterns as $content) {
  234. if (strpos($content, '<') !== false) {
  235. $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
  236. $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
  237. if ($check < $value) {
  238. return $token;
  239. }
  240. } else {
  241. $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
  242. $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
  243. if ($check <= $value) {
  244. return $token;
  245. }
  246. }
  247. }
  248. return $token;
  249. }
  250. /**
  251. * Sets the formating options of the localized currency string
  252. * If no parameter is passed, the standard setting of the
  253. * actual set locale will be used
  254. *
  255. * @param array $options (Optional) Options to set
  256. * @return Zend_Currency
  257. */
  258. public function setFormat(array $options = array())
  259. {
  260. $this->_options = $this->_checkOptions($options) + $this->_options;
  261. return $this;
  262. }
  263. /**
  264. * Internal function for checking static given locale parameter
  265. *
  266. * @param string $currency (Optional) Currency name
  267. * @param string|Zend_Locale $locale (Optional) Locale to display informations
  268. * @throws Zend_Currency_Exception When locale contains no region
  269. * @return string The extracted locale representation as string
  270. */
  271. private function _checkParams($currency = null, $locale = null)
  272. {
  273. // Manage the params
  274. if ((empty($locale)) and (!empty($currency)) and
  275. (Zend_Locale::isLocale($currency, true, false))) {
  276. $locale = $currency;
  277. $currency = null;
  278. }
  279. // Validate the locale and get the country short name
  280. $country = null;
  281. if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
  282. $country = substr($locale, (strpos($locale, '_') + 1));
  283. } else {
  284. require_once 'Zend/Currency/Exception.php';
  285. throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
  286. }
  287. // Get the available currencies for this country
  288. $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
  289. if ((empty($currency) === false) and (empty($data) === false)) {
  290. $abbreviation = $currency;
  291. } else {
  292. $abbreviation = $data;
  293. }
  294. return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
  295. }
  296. /**
  297. * Returns the actual or details of other currency symbols,
  298. * when no symbol is available it returns the currency shortname (f.e. FIM for Finnian Mark)
  299. *
  300. * @param string $currency (Optional) Currency name
  301. * @param string|Zend_Locale $locale (Optional) Locale to display informations
  302. * @return string
  303. */
  304. public function getSymbol($currency = null, $locale = null)
  305. {
  306. if (($currency === null) and ($locale === null)) {
  307. return $this->_options['symbol'];
  308. }
  309. $params = self::_checkParams($currency, $locale);
  310. // Get the symbol
  311. $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
  312. if (empty($symbol) === true) {
  313. $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
  314. }
  315. if (empty($symbol) === true) {
  316. return null;
  317. }
  318. return $symbol;
  319. }
  320. /**
  321. * Returns the actual or details of other currency shortnames
  322. *
  323. * @param string $currency OPTIONAL Currency's name
  324. * @param string|Zend_Locale $locale OPTIONAL The locale
  325. * @return string
  326. */
  327. public function getShortName($currency = null, $locale = null)
  328. {
  329. if (($currency === null) and ($locale === null)) {
  330. return $this->_options['currency'];
  331. }
  332. $params = self::_checkParams($currency, $locale);
  333. // Get the shortname
  334. if (empty($params['currency']) === true) {
  335. return $params['name'];
  336. }
  337. $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
  338. if (empty($list) === true) {
  339. $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
  340. if (empty($list) === false) {
  341. $list = $params['currency'];
  342. }
  343. }
  344. if (empty($list) === true) {
  345. return null;
  346. }
  347. return $list;
  348. }
  349. /**
  350. * Returns the actual or details of other currency names
  351. *
  352. * @param string $currency (Optional) Currency's short name
  353. * @param string|Zend_Locale $locale (Optional) The locale
  354. * @return string
  355. */
  356. public function getName($currency = null, $locale = null)
  357. {
  358. if (($currency === null) and ($locale === null)) {
  359. return $this->_options['name'];
  360. }
  361. $params = self::_checkParams($currency, $locale);
  362. // Get the name
  363. $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
  364. if (empty($name) === true) {
  365. $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
  366. }
  367. if (empty($name) === true) {
  368. return null;
  369. }
  370. return $name;
  371. }
  372. /**
  373. * Returns a list of regions where this currency is or was known
  374. *
  375. * @param string $currency OPTIONAL Currency's short name
  376. * @throws Zend_Currency_Exception When no currency was defined
  377. * @return array List of regions
  378. */
  379. public function getRegionList($currency = null)
  380. {
  381. if ($currency === null) {
  382. $currency = $this->_options['currency'];
  383. }
  384. if (empty($currency) === true) {
  385. require_once 'Zend/Currency/Exception.php';
  386. throw new Zend_Currency_Exception('No currency defined');
  387. }
  388. $data = Zend_Locale_Data::getContent($this->_options['locale'], 'regiontocurrency', $currency);
  389. $result = explode(' ', $data);
  390. return $result;
  391. }
  392. /**
  393. * Returns a list of currencies which are used in this region
  394. * a region name should be 2 charachters only (f.e. EG, DE, US)
  395. * If no region is given, the actual region is used
  396. *
  397. * @param string $region OPTIONAL Region to return the currencies for
  398. * @return array List of currencies
  399. */
  400. public function getCurrencyList($region = null)
  401. {
  402. if (empty($region) === true) {
  403. if (strlen($this->_options['locale']) > 4) {
  404. $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
  405. }
  406. }
  407. $data = Zend_Locale_Data::getContent($this->_options['locale'], 'currencytoregion', $region);
  408. $result = explode(' ', $data);
  409. return $result;
  410. }
  411. /**
  412. * Returns the actual currency name
  413. *
  414. * @return string
  415. */
  416. public function toString()
  417. {
  418. return $this->toCurrency();
  419. }
  420. /**
  421. * Returns the currency name
  422. *
  423. * @return string
  424. */
  425. public function __toString()
  426. {
  427. return $this->toString();
  428. }
  429. /**
  430. * Returns the set cache
  431. *
  432. * @return Zend_Cache_Core The set cache
  433. */
  434. public static function getCache()
  435. {
  436. return Zend_Locale_Data::getCache();
  437. }
  438. /**
  439. * Sets a cache for Zend_Currency
  440. *
  441. * @param Zend_Cache_Core $cache Cache to set
  442. * @return void
  443. */
  444. public static function setCache(Zend_Cache_Core $cache)
  445. {
  446. Zend_Locale_Data::setCache($cache);
  447. }
  448. /**
  449. * Returns true when a cache is set
  450. *
  451. * @return boolean
  452. */
  453. public static function hasCache()
  454. {
  455. return Zend_Locale_Data::hasCache();
  456. }
  457. /**
  458. * Removes any set cache
  459. *
  460. * @return void
  461. */
  462. public static function removeCache()
  463. {
  464. Zend_Locale_Data::removeCache();
  465. }
  466. /**
  467. * Clears all set cache data
  468. *
  469. * @param string $tag Tag to clear when the default tag name is not used
  470. * @return void
  471. */
  472. public static function clearCache($tag = null)
  473. {
  474. Zend_Locale_Data::clearCache($tag);
  475. }
  476. /**
  477. * Sets a new locale for data retreivement
  478. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  479. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  480. *
  481. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  482. * @throws Zend_Currency_Exception When the given locale does not exist
  483. * @return Zend_Currency Provides fluent interface
  484. */
  485. public function setLocale($locale = null)
  486. {
  487. require_once 'Zend/Locale.php';
  488. try {
  489. $locale = Zend_Locale::findLocale($locale);
  490. if (strlen($locale) > 4) {
  491. $this->_options['locale'] = $locale;
  492. } else {
  493. require_once 'Zend/Currency/Exception.php';
  494. throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
  495. }
  496. } catch (Zend_Locale_Exception $e) {
  497. require_once 'Zend/Currency/Exception.php';
  498. throw new Zend_Currency_Exception($e->getMessage());
  499. }
  500. // Get currency details
  501. $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
  502. $this->_options['name'] = $this->getName(null, $this->_options['locale']);
  503. $this->_options['symbol'] = $this->getSymbol(null, $this->_options['locale']);
  504. return $this;
  505. }
  506. /**
  507. * Returns the actual set locale
  508. *
  509. * @return string
  510. */
  511. public function getLocale()
  512. {
  513. return $this->_options['locale'];
  514. }
  515. /**
  516. * Returns the value
  517. *
  518. * @return float
  519. */
  520. public function getValue()
  521. {
  522. return $this->_options['value'];
  523. }
  524. /**
  525. * Adds a currency
  526. *
  527. * @param float|integer|Zend_Currency $value Add this value to currency
  528. * @param string|Zend_Currency $currency The currency to add
  529. * @return Zend_Currency
  530. */
  531. public function setValue($value, $currency = null)
  532. {
  533. $this->_options['value'] = $this->_exchangeCurrency($value, $currency);
  534. return $this;
  535. }
  536. /**
  537. * Adds a currency
  538. *
  539. * @param float|integer|Zend_Currency $value Add this value to currency
  540. * @param string|Zend_Currency $currency The currency to add
  541. * @return Zend_Currency
  542. */
  543. public function add($value, $currency = null)
  544. {
  545. $value = $this->_exchangeCurrency($value, $currency);
  546. $this->_options['value'] += (float) $value;
  547. return $this;
  548. }
  549. /**
  550. * Substracts a currency
  551. *
  552. * @param float|integer|Zend_Currency $value Substracts this value from currency
  553. * @param string|Zend_Currency $currency The currency to substract
  554. * @return Zend_Currency
  555. */
  556. public function sub($value, $currency = null)
  557. {
  558. $value = $this->_exchangeCurrency($value, $currency);
  559. $this->_options['value'] -= (float) $value;
  560. return $this;
  561. }
  562. /**
  563. * Divides a currency
  564. *
  565. * @param float|integer|Zend_Currency $value Divides this value from currency
  566. * @param string|Zend_Currency $currency The currency to divide
  567. * @return Zend_Currency
  568. */
  569. public function div($value, $currency = null)
  570. {
  571. $value = $this->_exchangeCurrency($value, $currency);
  572. $this->_options['value'] /= (float) $value;
  573. return $this;
  574. }
  575. /**
  576. * Multiplies a currency
  577. *
  578. * @param float|integer|Zend_Currency $value Multiplies this value from currency
  579. * @param string|Zend_Currency $currency The currency to multiply
  580. * @return Zend_Currency
  581. */
  582. public function mul($value, $currency = null)
  583. {
  584. $value = $this->_exchangeCurrency($value, $currency);
  585. $this->_options['value'] *= (float) $value;
  586. return $this;
  587. }
  588. /**
  589. * Calculates the modulo from a currency
  590. *
  591. * @param float|integer|Zend_Currency $value Calculate modulo from this value
  592. * @param string|Zend_Currency $currency The currency to calculate the modulo
  593. * @return Zend_Currency
  594. */
  595. public function mod($value, $currency = null)
  596. {
  597. $value = $this->_exchangeCurrency($value, $currency);
  598. $this->_options['value'] %= (float) $value;
  599. return $this;
  600. }
  601. /**
  602. * Compares two currencies
  603. *
  604. * @param float|integer|Zend_Currency $value Compares the currency with this value
  605. * @param string|Zend_Currency $currency The currency to compare this value from
  606. * @return Zend_Currency
  607. */
  608. public function compare($value, $currency = null)
  609. {
  610. $value = $this->_exchangeCurrency($value, $currency);
  611. $value = $this->_options['value'] - $value;
  612. if ($value < 0) {
  613. return -1;
  614. } else if ($value > 0) {
  615. return 1;
  616. }
  617. return 0;
  618. }
  619. /**
  620. * Returns true when the two currencies are equal
  621. *
  622. * @param float|integer|Zend_Currency $value Compares the currency with this value
  623. * @param string|Zend_Currency $currency The currency to compare this value from
  624. * @return boolean
  625. */
  626. public function equals($value, $currency = null)
  627. {
  628. $value = $this->_exchangeCurrency($value, $currency);
  629. if ($this->_options['value'] == $value) {
  630. return true;
  631. }
  632. return false;
  633. }
  634. /**
  635. * Returns true when the currency is more than the given value
  636. *
  637. * @param float|integer|Zend_Currency $value Compares the currency with this value
  638. * @param string|Zend_Currency $currency The currency to compare this value from
  639. * @return boolean
  640. */
  641. public function isMore($value, $currency = null)
  642. {
  643. $value = $this->_exchangeCurrency($value, $currency);
  644. if ($this->_options['value'] > $value) {
  645. return true;
  646. }
  647. return false;
  648. }
  649. /**
  650. * Returns true when the currency is less than the given value
  651. *
  652. * @param float|integer|Zend_Currency $value Compares the currency with this value
  653. * @param string|Zend_Currency $currency The currency to compare this value from
  654. * @return boolean
  655. */
  656. public function isLess($value, $currency = null)
  657. {
  658. $value = $this->_exchangeCurrency($value, $currency);
  659. if ($this->_options['value'] < $value) {
  660. return true;
  661. }
  662. return false;
  663. }
  664. /**
  665. * Internal method which calculates the exchanges currency
  666. *
  667. * @param float|integer|Zend_Currency $value Compares the currency with this value
  668. * @param string|Zend_Currency $currency The currency to compare this value from
  669. * @return unknown
  670. */
  671. protected function _exchangeCurrency($value, $currency)
  672. {
  673. if ($value instanceof Zend_Currency) {
  674. $currency = $value->getShortName();
  675. $value = $value->getValue();
  676. } else {
  677. $currency = $this->getShortName($currency, $this->getLocale());
  678. }
  679. $rate = 1;
  680. if ($currency !== $this->getShortName()) {
  681. $service = $this->getService();
  682. if (!($service instanceof Zend_Currency_CurrencyInterface)) {
  683. require_once 'Zend/Currency/Exception.php';
  684. throw new Zend_Currency_Exception('No exchange service applied');
  685. }
  686. $rate = $service->getRate($currency, $this->getShortName());
  687. }
  688. $value *= $rate;
  689. return $value;
  690. }
  691. /**
  692. * Returns the set service class
  693. *
  694. * @return Zend_Service
  695. */
  696. public function getService()
  697. {
  698. return $this->_options['service'];
  699. }
  700. /**
  701. * Sets a new exchange service
  702. *
  703. * @param string|Zend_Currency_CurrencyInterface $service Service class
  704. * @return Zend_Currency
  705. */
  706. public function setService($service)
  707. {
  708. if (is_string($service)) {
  709. require_once 'Zend/Loader.php';
  710. if (!class_exists($service)) {
  711. $file = str_replace('_', DIRECTORY_SEPARATOR, $service) . '.php';
  712. if (Zend_Loader::isReadable($file)) {
  713. Zend_Loader::loadClass($service);
  714. }
  715. }
  716. $service = new $service;
  717. }
  718. if (!($service instanceof Zend_Currency_CurrencyInterface)) {
  719. require_once 'Zend/Currency/Exception.php';
  720. throw new Zend_Currency_Exception('A currency service must implement Zend_Currency_CurrencyInterface');
  721. }
  722. $this->_options['service'] = $service;
  723. return $this;
  724. }
  725. /**
  726. * Internal method for checking the options array
  727. *
  728. * @param array $options Options to check
  729. * @throws Zend_Currency_Exception On unknown position
  730. * @throws Zend_Currency_Exception On unknown locale
  731. * @throws Zend_Currency_Exception On unknown display
  732. * @throws Zend_Currency_Exception On precision not between -1 and 30
  733. * @throws Zend_Currency_Exception On problem with script conversion
  734. * @throws Zend_Currency_Exception On unknown options
  735. * @return array
  736. */
  737. protected function _checkOptions(array $options = array())
  738. {
  739. if (count($options) === 0) {
  740. return $this->_options;
  741. }
  742. foreach ($options as $name => $value) {
  743. $name = strtolower($name);
  744. if ($name !== 'format') {
  745. if (gettype($value) === 'string') {
  746. $value = strtolower($value);
  747. }
  748. }
  749. switch($name) {
  750. case 'position':
  751. if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
  752. require_once 'Zend/Currency/Exception.php';
  753. throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
  754. }
  755. break;
  756. case 'format':
  757. if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
  758. if (!is_string($value) || (strpos($value, '0') === false)) {
  759. require_once 'Zend/Currency/Exception.php';
  760. throw new Zend_Currency_Exception("'" .
  761. ((gettype($value) === 'object') ? get_class($value) : $value)
  762. . "' is no format token");
  763. }
  764. }
  765. break;
  766. case 'display':
  767. if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
  768. ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
  769. require_once 'Zend/Currency/Exception.php';
  770. throw new Zend_Currency_Exception("Unknown display '$value'");
  771. }
  772. break;
  773. case 'precision':
  774. if ($value === null) {
  775. $value = -1;
  776. }
  777. if (($value < -1) or ($value > 30)) {
  778. require_once 'Zend/Currency/Exception.php';
  779. throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
  780. }
  781. break;
  782. case 'script':
  783. try {
  784. Zend_Locale_Format::convertNumerals(0, $options['script']);
  785. } catch (Zend_Locale_Exception $e) {
  786. require_once 'Zend/Currency/Exception.php';
  787. throw new Zend_Currency_Exception($e->getMessage());
  788. }
  789. break;
  790. default:
  791. break;
  792. }
  793. }
  794. return $options;
  795. }
  796. }