2
0

Currency.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_View_Helper_Abstract.php */
  23. require_once 'Zend/View/Helper/Abstract.php';
  24. /**
  25. * Currency view helper
  26. *
  27. * @category Zend
  28. * @package Zend_View
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_View_Helper_Currency extends Zend_View_Helper_Abstract
  33. {
  34. /**
  35. * Currency object
  36. *
  37. * @var Zend_Currency
  38. */
  39. protected $_currency;
  40. /**
  41. * Constructor for manually handling
  42. *
  43. * @param Zend_Currency $currency Instance of Zend_Currency
  44. * @return void
  45. */
  46. public function __construct($currency = null)
  47. {
  48. if ($currency === null) {
  49. require_once 'Zend/Registry.php';
  50. if (Zend_Registry::isRegistered('Zend_Currency')) {
  51. $currency = Zend_Registry::get('Zend_Currency');
  52. }
  53. }
  54. $this->setCurrency($currency);
  55. }
  56. /**
  57. * Output a formatted currency
  58. *
  59. * @param integer|float $value Currency value to output
  60. * @param string|Zend_Locale|Zend_Currency $currency OPTIONAL Currency to use for this call
  61. * @return string Formatted currency
  62. */
  63. public function currency($value = null, $currency = null)
  64. {
  65. if ($value === null) {
  66. return $this;
  67. }
  68. if (is_string($currency) || ($currency instanceof Zend_Locale)) {
  69. require_once 'Zend/Locale.php';
  70. if (Zend_Locale::isLocale($currency)) {
  71. $currency = array('locale' => $currency);
  72. }
  73. }
  74. if (is_string($currency)) {
  75. $currency = array('currency' => $currency);
  76. }
  77. if (is_array($currency)) {
  78. return $this->_currency->toCurrency($value, $currency);
  79. }
  80. return $this->_currency->toCurrency($value);
  81. }
  82. /**
  83. * Sets a currency to use
  84. *
  85. * @param Zend_Currency|String|Zend_Locale $currency Currency to use
  86. * @throws Zend_View_Exception When no or a false currency was set
  87. * @return Zend_View_Helper_Currency
  88. */
  89. public function setCurrency($currency = null)
  90. {
  91. if (!$currency instanceof Zend_Currency) {
  92. require_once 'Zend/Currency.php';
  93. $currency = new Zend_Currency($currency);
  94. }
  95. $this->_currency = $currency;
  96. return $this;
  97. }
  98. /**
  99. * Retrieve currency object
  100. *
  101. * @return Zend_Currency|null
  102. */
  103. public function getCurrency()
  104. {
  105. return $this->_currency;
  106. }
  107. }