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