Source of file NumberFormat.php
Size: 4,852 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/I18n/View/Helper/NumberFormat.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ namespace Zend\I18n\View\Helper; use Locale; use NumberFormatter; use Zend\I18n\Exception; use Zend\View\Helper\AbstractHelper; /** * View helper for formatting dates. */ class NumberFormat extends AbstractHelper { /** * number of decimals to use. * * @var int */ protected $decimals; /** * NumberFormat style to use * * @var int */ protected $formatStyle; /** * NumberFormat type to use * * @var int */ protected $formatType; /** * Formatter instances * * @var array */ protected $formatters = array(); /** * Locale to use instead of the default * * @var string */ protected $locale; /** * @throws Exception\ExtensionNotLoadedException if ext/intl is not present */ public function __construct() { if (!extension_loaded('intl')) { throw new Exception\ExtensionNotLoadedException(sprintf( '%s component requires the intl PHP extension', __NAMESPACE__ )); } } /** * Format a number * * @param int|float $number * @param int $formatStyle * @param int $formatType * @param string $locale * @param int $decimals * @return string */ public function __invoke( $number, $formatStyle = null, $formatType = null, $locale = null, $decimals = null ) { if (null === $locale) { $locale = $this->getLocale(); } if (null === $formatStyle) { $formatStyle = $this->getFormatStyle(); } if (null === $formatType) { $formatType = $this->getFormatType(); } if (!is_int($decimals) || $decimals < 0) { $decimals = $this->getDecimals(); } $formatterId = md5($formatStyle . "\0" . $locale . "\0" . $decimals); if (!isset($this->formatters[$formatterId])) { $this->formatters[$formatterId] = new NumberFormatter( $locale, $formatStyle ); if ($decimals !== null) { $this->formatters[$formatterId]->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $decimals); $this->formatters[$formatterId]->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $decimals); } } return $this->formatters[$formatterId]->format($number, $formatType); } /** * Set format style to use instead of the default * * @param int $formatStyle * @return NumberFormat */ public function setFormatStyle($formatStyle) { $this->formatStyle = (int) $formatStyle; return $this; } /** * Get the format style to use * * @return int */ public function getFormatStyle() { if (null === $this->formatStyle) { $this->formatStyle = NumberFormatter::DECIMAL; } return $this->formatStyle; } /** * Set format type to use instead of the default * * @param int $formatType * @return NumberFormat */ public function setFormatType($formatType) { $this->formatType = (int) $formatType; return $this; } /** * Get the format type to use * * @return int */ public function getFormatType() { if (null === $this->formatType) { $this->formatType = NumberFormatter::TYPE_DEFAULT; } return $this->formatType; } /** * Set number of decimals to use instead of the default. * * @param int $decimals * @return NumberFormat */ public function setDecimals($decimals) { $this->decimals = $decimals; return $this; } /** * Get number of decimals. * * @return int */ public function getDecimals() { return $this->decimals; } /** * Set locale to use instead of the default. * * @param string $locale * @return NumberFormat */ public function setLocale($locale) { $this->locale = (string) $locale; return $this; } /** * Get the locale to use * * @return string|null */ public function getLocale() { if ($this->locale === null) { $this->locale = Locale::getDefault(); } return $this->locale; } } |