Currency.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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|array $currency OPTIONAL Currency to use for
  61. * this call
  62. * @return string Formatted currency
  63. */
  64. public function currency($value = null, $currency = null)
  65. {
  66. if ($value === null) {
  67. return $this;
  68. }
  69. if (is_string($currency) || ($currency instanceof Zend_Locale)) {
  70. require_once 'Zend/Locale.php';
  71. if (Zend_Locale::isLocale($currency)) {
  72. $currency = array('locale' => $currency);
  73. }
  74. }
  75. if (is_string($currency)) {
  76. $currency = array('currency' => $currency);
  77. }
  78. if (is_array($currency)) {
  79. return $this->_currency->toCurrency($value, $currency);
  80. }
  81. return $this->_currency->toCurrency($value);
  82. }
  83. /**
  84. * Sets a currency to use
  85. *
  86. * @param Zend_Currency|String|Zend_Locale $currency Currency to use
  87. * @throws Zend_View_Exception When no or a false currency was set
  88. * @return Zend_View_Helper_Currency
  89. */
  90. public function setCurrency($currency = null)
  91. {
  92. if (!$currency instanceof Zend_Currency) {
  93. require_once 'Zend/Currency.php';
  94. $currency = new Zend_Currency($currency);
  95. }
  96. $this->_currency = $currency;
  97. return $this;
  98. }
  99. /**
  100. * Retrieve currency object
  101. *
  102. * @return Zend_Currency|null
  103. */
  104. public function getCurrency()
  105. {
  106. return $this->_currency;
  107. }
  108. }