Formatter.php 7.97 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\i18n;

use Yii;
use IntlDateFormatter;
use NumberFormatter;
use DateTime;
use yii\base\InvalidConfigException;

/**
 * Formatter is the localized version of [[\yii\base\Formatter]].
 *
 * Formatter requires the PHP "intl" extension to be installed. Formatter supports localized
 * formatting of date, time and numbers, based on the current [[locale]].
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Formatter extends \yii\base\Formatter
{
	/**
	 * @var string the locale ID that is used to localize the date and number formatting.
	 * If not set, [[\yii\base\Application::language]] will be used.
	 */
	public $locale;
	/**
	 * @var string the default format string to be used to format a date using PHP date() function.
	 */
	public $dateFormat = 'short';
	/**
	 * @var string the default format string to be used to format a time using PHP date() function.
	 */
	public $timeFormat = 'short';
	/**
	 * @var string the default format string to be used to format a date and time using PHP date() function.
	 */
	public $datetimeFormat = 'short';
	/**
	 * @var array the options to be set for the NumberFormatter objects. Please refer to
	 */
	public $numberFormatOptions = array();


	/**
	 * Initializes the component.
	 * This method will check if the "intl" PHP extension is installed and set the
	 * default value of [[locale]].
	 * @throws InvalidConfigException if the "intl" PHP extension is not installed.
	 */
	public function init()
	{
		if (!extension_loaded('intl')) {
			throw new InvalidConfigException('The "intl" PHP extension is not install. It is required to format data values in localized formats.');
		}
		if ($this->locale === null) {
			$this->locale = Yii::$app->language;
		}
		parent::init();
	}

	private $_dateFormats = array(
		'short' => IntlDateFormatter::SHORT,
		'medium' => IntlDateFormatter::MEDIUM,
		'long' => IntlDateFormatter::LONG,
		'full' => IntlDateFormatter::FULL,
	);

	/**
	 * Formats the value as a date.
	 * @param integer|string|DateTime $value the value to be formatted. The following
	 * types of value are supported:
	 *
79
	 * - an integer representing a UNIX timestamp
Qiang Xue committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	 * - a string that can be parsed into a UNIX timestamp via `strtotime()`
	 * - a PHP DateTime object
	 *
	 * @param string $format the format used to convert the value into a date string.
	 * If null, [[dateFormat]] will be used. The format string should be the one
	 * that can be recognized by the PHP `date()` function.
	 * @return string the formatted result
	 * @see dateFormat
	 */
	public function asDate($value, $format = null)
	{
		$value = $this->normalizeDatetimeValue($value);
		if ($format === null) {
			$format = $this->dateFormat;
		}
		if (isset($this->_dateFormats[$format])) {
			$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE);
		} else {
			$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE);
			$formatter->setPattern($format);
		}
		return $formatter->format($value);
	}

	/**
	 * Formats the value as a time.
	 * @param integer|string|DateTime $value the value to be formatted. The following
	 * types of value are supported:
	 *
109
	 * - an integer representing a UNIX timestamp
Qiang Xue committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	 * - a string that can be parsed into a UNIX timestamp via `strtotime()`
	 * - a PHP DateTime object
	 *
	 * @param string $format the format used to convert the value into a date string.
	 * If null, [[dateFormat]] will be used. The format string should be the one
	 * that can be recognized by the PHP `date()` function.
	 * @return string the formatted result
	 * @see timeFormat
	 */
	public function asTime($value, $format = null)
	{
		$value = $this->normalizeDatetimeValue($value);
		if ($format === null) {
			$format = $this->timeFormat;
		}
		if (isset($this->_dateFormats[$format])) {
			$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format]);
		} else {
			$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE);
			$formatter->setPattern($format);
		}
		return $formatter->format($value);
	}

	/**
	 * Formats the value as a datetime.
	 * @param integer|string|DateTime $value the value to be formatted. The following
	 * types of value are supported:
	 *
139
	 * - an integer representing a UNIX timestamp
Qiang Xue committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	 * - a string that can be parsed into a UNIX timestamp via `strtotime()`
	 * - a PHP DateTime object
	 *
	 * @param string $format the format used to convert the value into a date string.
	 * If null, [[dateFormat]] will be used. The format string should be the one
	 * that can be recognized by the PHP `date()` function.
	 * @return string the formatted result
	 * @see datetimeFormat
	 */
	public function asDatetime($value, $format = null)
	{
		$value = $this->normalizeDatetimeValue($value);
		if ($format === null) {
			$format = $this->datetimeFormat;
		}
		if (isset($this->_dateFormats[$format])) {
			$formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format]);
		} else {
			$formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE);
			$formatter->setPattern($format);
		}
		return $formatter->format($value);
	}

	/**
	 * Formats the value as a decimal number.
	 * @param mixed $value the value to be formatted
	 * @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
	 * for details on how to specify a format.
	 * @return string the formatted result.
	 */
	public function asDecimal($value, $format = null)
	{
Qiang Xue committed
173
		return $this->createNumberFormatter(NumberFormatter::DECIMAL, $format)->format($value);
Qiang Xue committed
174 175 176 177 178 179 180 181 182 183 184 185
	}

	/**
	 * Formats the value as a currency number.
	 * @param mixed $value the value to be formatted
	 * @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use.
	 * @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
	 * for details on how to specify a format.
	 * @return string the formatted result.
	 */
	public function asCurrency($value, $currency = 'USD', $format = null)
	{
Qiang Xue committed
186
		return $this->createNumberFormatter(NumberFormatter::CURRENCY, $format)->formatCurrency($value, $currency);
Qiang Xue committed
187 188 189 190 191 192 193 194 195 196 197
	}

	/**
	 * Formats the value as a percent number.
	 * @param mixed $value the value to be formatted
	 * @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
	 * for details on how to specify a format.
	 * @return string the formatted result.
	 */
	public function asPercent($value, $format = null)
	{
Qiang Xue committed
198
		return $this->createNumberFormatter(NumberFormatter::PERCENT, $format)->format($value);
Qiang Xue committed
199 200 201 202 203 204 205 206 207 208 209
	}

	/**
	 * Formats the value as a scientific number.
	 * @param mixed $value the value to be formatted
	 * @param string $format the format to be used. Please refer to [ICU manual](http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details)
	 * for details on how to specify a format.
	 * @return string the formatted result.
	 */
	public function asScientific($value, $format = null)
	{
Qiang Xue committed
210
		return $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $format)->format($value);
Qiang Xue committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
	}

	/**
	 * Creates a number formatter based on the given type and format.
	 * @param integer $type the type of the number formatter
	 * @param string $format the format to be used
	 * @return NumberFormatter the created formatter instance
	 */
	protected function createNumberFormatter($type, $format)
	{
		$formatter = new NumberFormatter($this->locale, $type);
		if ($format !== null) {
			$formatter->setPattern($format);
		}
		if (!empty($this->numberFormatOptions)) {
			foreach ($this->numberFormatOptions as $name => $attribute) {
				$formatter->setAttribute($name, $attribute);
			}
		}
		return $formatter;
	}
}