Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Currency.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Locale.php';
26 #require_once 'Zend/Locale/Data.php';
27 #require_once 'Zend/Locale/Format.php';
28 
38 {
39  // Constants for defining what currency symbol should be displayed
40  const NO_SYMBOL = 1;
41  const USE_SYMBOL = 2;
42  const USE_SHORTNAME = 3;
43  const USE_NAME = 4;
44 
45  // Constants for defining the position of the currencysign
46  const STANDARD = 8;
47  const RIGHT = 16;
48  const LEFT = 32;
49 
69  protected $_options = array(
70  'position' => self::STANDARD,
71  'script' => null,
72  'format' => null,
73  'display' => self::NO_SYMBOL,
74  'precision' => 2,
75  'name' => null,
76  'currency' => null,
77  'symbol' => null,
78  'locale' => null,
79  'value' => 0,
80  'service' => null,
81  'tag' => 'Zend_Locale'
82  );
83 
92  public function __construct($options = null, $locale = null)
93  {
94  $calloptions = $options;
95  if (is_array($options) && isset($options['display'])) {
96  $this->_options['display'] = $options['display'];
97  }
98 
99  if (is_array($options)) {
100  $this->setLocale($locale);
101  $this->setFormat($options);
102  } else if (Zend_Locale::isLocale($options, false, false)) {
103  $this->setLocale($options);
104  $options = $locale;
105  } else {
106  $this->setLocale($locale);
107  }
108 
109  // Get currency details
110  if (!isset($this->_options['currency']) || !is_array($options)) {
111  $this->_options['currency'] = self::getShortName($options, $this->_options['locale']);
112  }
113 
114  if (!isset($this->_options['name']) || !is_array($options)) {
115  $this->_options['name'] = self::getName($options, $this->_options['locale']);
116  }
117 
118  if (!isset($this->_options['symbol']) || !is_array($options)) {
119  $this->_options['symbol'] = self::getSymbol($options, $this->_options['locale']);
120  }
121 
122  if (($this->_options['currency'] === null) and ($this->_options['name'] === null)) {
123  #require_once 'Zend/Currency/Exception.php';
124  throw new Zend_Currency_Exception("Currency '$options' not found");
125  }
126 
127  // Get the format
128  if ((is_array($calloptions) && !isset($calloptions['display']))
129  || (!is_array($calloptions) && $this->_options['display'] == self::NO_SYMBOL)) {
130  if (!empty($this->_options['symbol'])) {
131  $this->_options['display'] = self::USE_SYMBOL;
132  } else if (!empty($this->_options['currency'])) {
133  $this->_options['display'] = self::USE_SHORTNAME;
134  }
135  }
136  }
137 
146  public function toCurrency($value = null, array $options = array())
147  {
148  if ($value === null) {
149  if (is_array($options) && isset($options['value'])) {
150  $value = $options['value'];
151  } else {
152  $value = $this->_options['value'];
153  }
154  }
155 
156  if (is_array($value)) {
157  $options += $value;
158  if (isset($options['value'])) {
159  $value = $options['value'];
160  }
161  }
162 
163  // Validate the passed number
164  if (!(isset($value)) or (is_numeric($value) === false)) {
165  #require_once 'Zend/Currency/Exception.php';
166  throw new Zend_Currency_Exception("Value '$value' has to be numeric");
167  }
168 
169  if (isset($options['currency'])) {
170  if (!isset($options['locale'])) {
171  $options['locale'] = $this->_options['locale'];
172  }
173 
174  $options['currency'] = self::getShortName($options['currency'], $options['locale']);
175  $options['name'] = self::getName($options['currency'], $options['locale']);
176  $options['symbol'] = self::getSymbol($options['currency'], $options['locale']);
177  }
178 
180 
181  // Format the number
182  $format = $options['format'];
183  $locale = $options['locale'];
184  if (empty($format)) {
185  $format = Zend_Locale_Data::getContent($locale, 'currencynumber');
186  } else if (Zend_Locale::isLocale($format, true, false)) {
187  $locale = $format;
188  $format = Zend_Locale_Data::getContent($format, 'currencynumber');
189  }
190 
191  $original = $value;
192  $value = Zend_Locale_Format::toNumber($value, array('locale' => $locale,
193  'number_format' => $format,
194  'precision' => $options['precision']));
195 
196  if ($options['position'] !== self::STANDARD) {
197  $value = str_replace('¤', '', $value);
198  $space = '';
199  if (iconv_strpos($value, ' ') !== false) {
200  $value = str_replace(' ', '', $value);
201  $space = ' ';
202  }
203 
204  if ($options['position'] == self::LEFT) {
205  $value = '¤' . $space . $value;
206  } else {
207  $value = $value . $space . '¤';
208  }
209  }
210 
211  // Localize the number digits
212  if (empty($options['script']) === false) {
214  }
215 
216  // Get the sign to be placed next to the number
217  if (is_numeric($options['display']) === false) {
218  $sign = $options['display'];
219  } else {
220  switch($options['display']) {
221  case self::USE_SYMBOL:
222  $sign = $this->_extractPattern($options['symbol'], $original);
223  break;
224 
225  case self::USE_SHORTNAME:
226  $sign = $options['currency'];
227  break;
228 
229  case self::USE_NAME:
230  $sign = $options['name'];
231  break;
232 
233  default:
234  $sign = '';
235  $value = str_replace(' ', '', $value);
236  break;
237  }
238  }
239 
240  $value = str_replace('¤', $sign, $value);
241  return $value;
242  }
243 
252  private function _extractPattern($pattern, $value)
253  {
254  if (strpos($pattern, '|') === false) {
255  return $pattern;
256  }
257 
258  $patterns = explode('|', $pattern);
259  $token = $pattern;
260  $value = trim(str_replace('¤', '', $value));
261  krsort($patterns);
262  foreach($patterns as $content) {
263  if (strpos($content, '<') !== false) {
264  $check = iconv_substr($content, 0, iconv_strpos($content, '<'));
265  $token = iconv_substr($content, iconv_strpos($content, '<') + 1);
266  if ($check < $value) {
267  return $token;
268  }
269  } else {
270  $check = iconv_substr($content, 0, iconv_strpos($content, '≤'));
271  $token = iconv_substr($content, iconv_strpos($content, '≤') + 1);
272  if ($check <= $value) {
273  return $token;
274  }
275  }
276 
277  }
278 
279  return $token;
280  }
281 
290  public function setFormat(array $options = array())
291  {
292  $this->_options = $this->_checkOptions($options) + $this->_options;
293  return $this;
294  }
295 
304  private function _checkParams($currency = null, $locale = null)
305  {
306  // Manage the params
307  if ((empty($locale)) and (!empty($currency)) and
308  (Zend_Locale::isLocale($currency, true, false))) {
309  $locale = $currency;
310  $currency = null;
311  }
312 
313  // Validate the locale and get the country short name
314  $country = null;
315  if ((Zend_Locale::isLocale($locale, true, false)) and (strlen($locale) > 4)) {
316  $country = substr($locale, (strpos($locale, '_') + 1));
317  } else {
318  #require_once 'Zend/Currency/Exception.php';
319  throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
320  }
321 
322  // Get the available currencies for this country
323  $data = Zend_Locale_Data::getContent($locale, 'currencytoregion', $country);
324  if ((empty($currency) === false) and (empty($data) === false)) {
325  $abbreviation = $currency;
326  } else {
327  $abbreviation = $data;
328  }
329 
330  return array('locale' => $locale, 'currency' => $currency, 'name' => $abbreviation, 'country' => $country);
331  }
332 
341  public function getSymbol($currency = null, $locale = null)
342  {
343  if (($currency === null) and ($locale === null)) {
344  return $this->_options['symbol'];
345  }
346 
347  $params = self::_checkParams($currency, $locale);
348 
349  // Get the symbol
350  $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['currency']);
351  if (empty($symbol) === true) {
352  $symbol = Zend_Locale_Data::getContent($params['locale'], 'currencysymbol', $params['name']);
353  }
354 
355  if (empty($symbol) === true) {
356  return null;
357  }
358 
359  return $symbol;
360  }
361 
369  public function getShortName($currency = null, $locale = null)
370  {
371  if (($currency === null) and ($locale === null)) {
372  return $this->_options['currency'];
373  }
374 
375  $params = self::_checkParams($currency, $locale);
376 
377  // Get the shortname
378  if (empty($params['currency']) === true) {
379  return $params['name'];
380  }
381 
382  $list = Zend_Locale_Data::getContent($params['locale'], 'currencytoname', $params['currency']);
383  if (empty($list) === true) {
384  $list = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
385  if (empty($list) === false) {
386  $list = $params['currency'];
387  }
388  }
389 
390  if (empty($list) === true) {
391  return null;
392  }
393 
394  return $list;
395  }
396 
404  public function getName($currency = null, $locale = null)
405  {
406  if (($currency === null) and ($locale === null)) {
407  return $this->_options['name'];
408  }
409 
410  $params = self::_checkParams($currency, $locale);
411 
412  // Get the name
413  $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['currency']);
414  if (empty($name) === true) {
415  $name = Zend_Locale_Data::getContent($params['locale'], 'nametocurrency', $params['name']);
416  }
417 
418  if (empty($name) === true) {
419  return null;
420  }
421 
422  return $name;
423  }
424 
432  public function getRegionList($currency = null)
433  {
434  if ($currency === null) {
435  $currency = $this->_options['currency'];
436  }
437 
438  if (empty($currency) === true) {
439  #require_once 'Zend/Currency/Exception.php';
440  throw new Zend_Currency_Exception('No currency defined');
441  }
442 
443  $data = Zend_Locale_Data::getContent($this->_options['locale'], 'regiontocurrency', $currency);
444 
445  $result = explode(' ', $data);
446  return $result;
447  }
448 
457  public function getCurrencyList($region = null)
458  {
459  if (empty($region) === true) {
460  if (strlen($this->_options['locale']) > 4) {
461  $region = substr($this->_options['locale'], (strpos($this->_options['locale'], '_') + 1));
462  }
463  }
464 
465  $data = Zend_Locale_Data::getContent($this->_options['locale'], 'currencytoregion', $region);
466 
467  $result = explode(' ', $data);
468  return $result;
469  }
470 
476  public function toString()
477  {
478  return $this->toCurrency();
479  }
480 
486  public function __toString()
487  {
488  return $this->toString();
489  }
490 
496  public static function getCache()
497  {
499  }
500 
507  public static function setCache(Zend_Cache_Core $cache)
508  {
510  }
511 
517  public static function hasCache()
518  {
520  }
521 
527  public static function removeCache()
528  {
530  }
531 
538  public static function clearCache($tag = null)
539  {
541  }
542 
552  public function setLocale($locale = null)
553  {
554  #require_once 'Zend/Locale.php';
555  try {
556  $locale = Zend_Locale::findLocale($locale);
557  if (strlen($locale) > 4) {
558  $this->_options['locale'] = $locale;
559  } else {
560  #require_once 'Zend/Currency/Exception.php';
561  throw new Zend_Currency_Exception("No region found within the locale '" . (string) $locale . "'");
562  }
563  } catch (Zend_Locale_Exception $e) {
564  #require_once 'Zend/Currency/Exception.php';
565  throw new Zend_Currency_Exception($e->getMessage());
566  }
567 
568  // Get currency details
569  $this->_options['currency'] = $this->getShortName(null, $this->_options['locale']);
570  $this->_options['name'] = $this->getName(null, $this->_options['locale']);
571  $this->_options['symbol'] = $this->getSymbol(null, $this->_options['locale']);
572 
573  return $this;
574  }
575 
581  public function getLocale()
582  {
583  return $this->_options['locale'];
584  }
585 
591  public function getValue()
592  {
593  return $this->_options['value'];
594  }
595 
603  public function setValue($value, $currency = null)
604  {
605  $this->_options['value'] = $this->_exchangeCurrency($value, $currency);
606  return $this;
607  }
608 
616  public function add($value, $currency = null)
617  {
618  $value = $this->_exchangeCurrency($value, $currency);
619  $this->_options['value'] += (float) $value;
620  return $this;
621  }
622 
630  public function sub($value, $currency = null)
631  {
632  $value = $this->_exchangeCurrency($value, $currency);
633  $this->_options['value'] -= (float) $value;
634  return $this;
635  }
636 
644  public function div($value, $currency = null)
645  {
646  $value = $this->_exchangeCurrency($value, $currency);
647  $this->_options['value'] /= (float) $value;
648  return $this;
649  }
650 
658  public function mul($value, $currency = null)
659  {
660  $value = $this->_exchangeCurrency($value, $currency);
661  $this->_options['value'] *= (float) $value;
662  return $this;
663  }
664 
672  public function mod($value, $currency = null)
673  {
674  $value = $this->_exchangeCurrency($value, $currency);
675  $this->_options['value'] %= (float) $value;
676  return $this;
677  }
678 
686  public function compare($value, $currency = null)
687  {
688  $value = $this->_exchangeCurrency($value, $currency);
689  $value = $this->_options['value'] - $value;
690  if ($value < 0) {
691  return -1;
692  } else if ($value > 0) {
693  return 1;
694  }
695 
696  return 0;
697  }
698 
706  public function equals($value, $currency = null)
707  {
708  $value = $this->_exchangeCurrency($value, $currency);
709  if ($this->_options['value'] == $value) {
710  return true;
711  }
712 
713  return false;
714  }
715 
723  public function isMore($value, $currency = null)
724  {
725  $value = $this->_exchangeCurrency($value, $currency);
726  if ($this->_options['value'] > $value) {
727  return true;
728  }
729 
730  return false;
731  }
732 
740  public function isLess($value, $currency = null)
741  {
742  $value = $this->_exchangeCurrency($value, $currency);
743  if ($this->_options['value'] < $value) {
744  return true;
745  }
746 
747  return false;
748 
749  }
750 
758  protected function _exchangeCurrency($value, $currency)
759  {
760  if ($value instanceof Zend_Currency) {
761  $currency = $value->getShortName();
762  $value = $value->getValue();
763  } else {
764  $currency = $this->getShortName($currency, $this->getLocale());
765  }
766 
767  $rate = 1;
768  if ($currency !== $this->getShortName()) {
769  $service = $this->getService();
770  if (!($service instanceof Zend_Currency_CurrencyInterface)) {
771  #require_once 'Zend/Currency/Exception.php';
772  throw new Zend_Currency_Exception('No exchange service applied');
773  }
774 
775  $rate = $service->getRate($currency, $this->getShortName());
776  }
777 
778  $value *= $rate;
779  return $value;
780  }
781 
787  public function getService()
788  {
789  return $this->_options['service'];
790  }
791 
798  public function setService($service)
799  {
800  if (is_string($service)) {
801  #require_once 'Zend/Loader.php';
802  if (!class_exists($service)) {
803  $file = str_replace('_', DIRECTORY_SEPARATOR, $service) . '.php';
804  if (Zend_Loader::isReadable($file)) {
806  }
807  }
808 
809  $service = new $service;
810  }
811 
812  if (!($service instanceof Zend_Currency_CurrencyInterface)) {
813  #require_once 'Zend/Currency/Exception.php';
814  throw new Zend_Currency_Exception('A currency service must implement Zend_Currency_CurrencyInterface');
815  }
816 
817  $this->_options['service'] = $service;
818  return $this;
819  }
820 
833  protected function _checkOptions(array $options = array())
834  {
835  if (count($options) === 0) {
836  return $this->_options;
837  }
838 
839  foreach ($options as $name => $value) {
840  $name = strtolower($name);
841  if ($name !== 'format') {
842  if (gettype($value) === 'string') {
843  $value = strtolower($value);
844  }
845  }
846 
847  switch($name) {
848  case 'position':
849  if (($value !== self::STANDARD) and ($value !== self::RIGHT) and ($value !== self::LEFT)) {
850  #require_once 'Zend/Currency/Exception.php';
851  throw new Zend_Currency_Exception("Unknown position '" . $value . "'");
852  }
853 
854  break;
855 
856  case 'format':
857  if ((empty($value) === false) and (Zend_Locale::isLocale($value, null, false) === false)) {
858  if (!is_string($value) || (strpos($value, '0') === false)) {
859  #require_once 'Zend/Currency/Exception.php';
860  throw new Zend_Currency_Exception("'" .
861  ((gettype($value) === 'object') ? get_class($value) : $value)
862  . "' is no format token");
863  }
864  }
865  break;
866 
867  case 'display':
868  if (is_numeric($value) and ($value !== self::NO_SYMBOL) and ($value !== self::USE_SYMBOL) and
869  ($value !== self::USE_SHORTNAME) and ($value !== self::USE_NAME)) {
870  #require_once 'Zend/Currency/Exception.php';
871  throw new Zend_Currency_Exception("Unknown display '$value'");
872  }
873  break;
874 
875  case 'precision':
876  if ($value === null) {
877  $value = -1;
878  }
879 
880  if (($value < -1) or ($value > 30)) {
881  #require_once 'Zend/Currency/Exception.php';
882  throw new Zend_Currency_Exception("'$value' precision has to be between -1 and 30.");
883  }
884  break;
885 
886  case 'script':
887  try {
889  } catch (Zend_Locale_Exception $e) {
890  #require_once 'Zend/Currency/Exception.php';
891  throw new Zend_Currency_Exception($e->getMessage());
892  }
893  break;
894 
895  default:
896  break;
897  }
898  }
899 
900  return $options;
901  }
902 }
getRegionList($currency=null)
Definition: Currency.php:432
getName($currency=null, $locale=null)
Definition: Currency.php:404
const USE_SYMBOL
Definition: Currency.php:41
isMore($value, $currency=null)
Definition: Currency.php:723
static loadClass($class, $dirs=null)
Definition: Loader.php:52
compare($value, $currency=null)
Definition: Currency.php:686
$pattern
Definition: website.php:22
getCurrencyList($region=null)
Definition: Currency.php:457
setLocale($locale=null)
Definition: Currency.php:552
static getContent($locale, $path, $value=false)
Definition: Data.php:968
getSymbol($currency=null, $locale=null)
Definition: Currency.php:341
const RIGHT
Definition: Currency.php:47
const USE_NAME
Definition: Currency.php:43
div($value, $currency=null)
Definition: Currency.php:644
__construct($options=null, $locale=null)
Definition: Currency.php:92
setValue($value, $currency=null)
Definition: Currency.php:603
static isReadable($filename)
Definition: Loader.php:162
setFormat(array $options=array())
Definition: Currency.php:290
equals($value, $currency=null)
Definition: Currency.php:706
static clearCache()
Definition: Data.php:1562
static getCache()
Definition: Data.php:1517
static getCache()
Definition: Currency.php:496
getShortName($currency=null, $locale=null)
Definition: Currency.php:369
_exchangeCurrency($value, $currency)
Definition: Currency.php:758
static convertNumerals($input, $from, $to=null)
Definition: Format.php:198
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
sub($value, $currency=null)
Definition: Currency.php:630
_checkOptions(array $options=array())
Definition: Currency.php:833
const STANDARD
Definition: Currency.php:46
static isLocale($locale, $strict=false, $compatible=true)
Definition: Locale.php:1683
add($value, $currency=null)
Definition: Currency.php:616
mul($value, $currency=null)
Definition: Currency.php:658
static findLocale($locale=null)
Definition: Locale.php:1740
static removeCache()
Definition: Data.php:1552
setService($service)
Definition: Currency.php:798
static setCache(Zend_Cache_Core $cache)
Definition: Currency.php:507
static setCache(Zend_Cache_Core $cache)
Definition: Data.php:1527
static clearCache($tag=null)
Definition: Currency.php:538
static hasCache()
Definition: Currency.php:517
mod($value, $currency=null)
Definition: Currency.php:672
static hasCache()
Definition: Data.php:1538
static toNumber($value, array $options=array())
Definition: Format.php:300
toCurrency($value=null, array $options=array())
Definition: Currency.php:146
const NO_SYMBOL
Definition: Currency.php:40
const USE_SHORTNAME
Definition: Currency.php:42
isLess($value, $currency=null)
Definition: Currency.php:740
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
static removeCache()
Definition: Currency.php:527
const LEFT
Definition: Currency.php:48
if(!isset($_GET['name'])) $name
Definition: log.php:14