| Name | Size | Mode | Actions |
|---|---|---|---|
| migrations/ | - | 0755 | rm |
| DbMessageSource.php | 7133 | 0644 | editdlrm |
| Formatter.php | 91967 | 0644 | editdlrm |
| GettextFile.php | 1183 | 0644 | editdlrm |
| GettextMessageSource.php | 6376 | 0644 | editdlrm |
| GettextMoFile.php | 9471 | 0644 | editdlrm |
| GettextPoFile.php | 3740 | 0644 | editdlrm |
| I18N.php | 7782 | 0644 | editdlrm |
| Locale.php | 1627 | 0644 | editdlrm |
| MessageFormatter.php | 17678 | 0644 | editdlrm |
| MessageSource.php | 4538 | 0644 | editdlrm |
| MissingTranslationEvent.php | 1101 | 0644 | editdlrm |
| PhpMessageSource.php | 6405 | 0644 | editdlrm |
/var/www/vhosts/nabawater/vendor/yiisoft/yii2/i18n/Formatter.php (91967B)
` tag. * One or multiple consecutive empty lines divide two paragraphs. * @param string $value the value to be formatted. * @return string the formatted result. */ public function asParagraphs($value) { if ($value === null) { return $this->nullDisplay; } return str_replace('
', '', '' . preg_replace('/\R{2,}/u', "
\n", Html::encode($value)) . '
'); } /** * Formats the value as HTML text. * The value will be purified using [[HtmlPurifier]] to avoid XSS attacks. * Use [[asRaw()]] if you do not want any purification of the value. * @param string $value the value to be formatted. * @param array|null $config the configuration for the HTMLPurifier class. * @return string the formatted result. */ public function asHtml($value, $config = null) { if ($value === null) { return $this->nullDisplay; } return HtmlPurifier::process($value, $config); } /** * Formats the value as a mailto link. * @param string $value the value to be formatted. * @param array $options the tag options in terms of name-value pairs. See [[Html::mailto()]]. * @return string the formatted result. */ public function asEmail($value, $options = []) { if ($value === null) { return $this->nullDisplay; } return Html::mailto(Html::encode($value), $value, $options); } /** * Formats the value as an image tag. * @param mixed $value the value to be formatted. * @param array $options the tag options in terms of name-value pairs. See [[Html::img()]]. * @return string the formatted result. */ public function asImage($value, $options = []) { if ($value === null) { return $this->nullDisplay; } return Html::img($value, $options); } /** * Formats the value as a hyperlink. * @param mixed $value the value to be formatted. * @param array $options the tag options in terms of name-value pairs. See [[Html::a()]]. * @return string the formatted result. */ public function asUrl($value, $options = []) { if ($value === null) { return $this->nullDisplay; } $url = $value; if (strpos($url, '://') === false) { $url = 'http://' . $url; } return Html::a(Html::encode($value), $url, $options); } /** * Formats the value as a boolean. * @param mixed $value the value to be formatted. * @return string the formatted result. * @see booleanFormat */ public function asBoolean($value) { if ($value === null) { return $this->nullDisplay; } return $value ? $this->booleanFormat[1] : $this->booleanFormat[0]; } // date and time formats /** * Formats the value as a date. * @param int|string|DateTime $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone * for the DateTime object to specify the source time zone. * * The formatter will convert date values according to [[timeZone]] before formatting it. * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. * Also no conversion will be performed on values that have no time information, e.g. `"2017-06-05"`. * * @param string $format the format used to convert the value into a date string. * If null, [[dateFormat]] will be used. * * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). * * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the * PHP [date()](http://php.net/manual/en/function.date.php)-function. * * @return string the formatted result. * @throws InvalidArgumentException if the input value can not be evaluated as a date value. * @throws InvalidConfigException if the date format is invalid. * @see dateFormat */ public function asDate($value, $format = null) { if ($format === null) { $format = $this->dateFormat; } return $this->formatDateTimeValue($value, $format, 'date'); } /** * Formats the value as a time. * @param int|string|DateTime $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone * for the DateTime object to specify the source time zone. * * The formatter will convert date values according to [[timeZone]] before formatting it. * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. * * @param string $format the format used to convert the value into a date string. * If null, [[timeFormat]] will be used. * * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). * * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the * PHP [date()](http://php.net/manual/en/function.date.php)-function. * * @return string the formatted result. * @throws InvalidArgumentException if the input value can not be evaluated as a date value. * @throws InvalidConfigException if the date format is invalid. * @see timeFormat */ public function asTime($value, $format = null) { if ($format === null) { $format = $this->timeFormat; } return $this->formatDateTimeValue($value, $format, 'time'); } /** * Formats the value as a datetime. * @param int|string|DateTime $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp. A UNIX timestamp is always in UTC by its definition. * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object. You may set the time zone * for the DateTime object to specify the source time zone. * * The formatter will convert date values according to [[timeZone]] before formatting it. * If no timezone conversion should be performed, you need to set [[defaultTimeZone]] and [[timeZone]] to the same value. * * @param string $format the format used to convert the value into a date string. * If null, [[datetimeFormat]] will be used. * * This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. * It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). * * Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the * PHP [date()](http://php.net/manual/en/function.date.php)-function. * * @return string the formatted result. * @throws InvalidArgumentException if the input value can not be evaluated as a date value. * @throws InvalidConfigException if the date format is invalid. * @see datetimeFormat */ public function asDatetime($value, $format = null) { if ($format === null) { $format = $this->datetimeFormat; } return $this->formatDateTimeValue($value, $format, 'datetime'); } /** * @var array map of short format names to IntlDateFormatter constant values. */ private $_dateFormats = [ 'short' => 3, // IntlDateFormatter::SHORT, 'medium' => 2, // IntlDateFormatter::MEDIUM, 'long' => 1, // IntlDateFormatter::LONG, 'full' => 0, // IntlDateFormatter::FULL, ]; /** * @param int|string|DateTime $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object * * @param string $format the format used to convert the value into a date string. * @param string $type 'date', 'time', or 'datetime'. * @throws InvalidConfigException if the date format is invalid. * @return string the formatted result. */ private function formatDateTimeValue($value, $format, $type) { $timeZone = $this->timeZone; // avoid time zone conversion for date-only and time-only values if ($type === 'date' || $type === 'time') { list($timestamp, $hasTimeInfo, $hasDateInfo) = $this->normalizeDatetimeValue($value, true); if ($type === 'date' && !$hasTimeInfo || $type === 'time' && !$hasDateInfo) { $timeZone = $this->defaultTimeZone; } } else { $timestamp = $this->normalizeDatetimeValue($value); } if ($timestamp === null) { return $this->nullDisplay; } // intl does not work with dates >=2038 or <=1901 on 32bit machines, fall back to PHP $year = $timestamp->format('Y'); if ($this->_intlLoaded && !(PHP_INT_SIZE === 4 && ($year <= 1901 || $year >= 2038))) { if (strncmp($format, 'php:', 4) === 0) { $format = FormatConverter::convertDatePhpToIcu(substr($format, 4)); } if (isset($this->_dateFormats[$format])) { if ($type === 'date') { $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], IntlDateFormatter::NONE, $timeZone, $this->calendar); } elseif ($type === 'time') { $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, $this->_dateFormats[$format], $timeZone, $this->calendar); } else { $formatter = new IntlDateFormatter($this->locale, $this->_dateFormats[$format], $this->_dateFormats[$format], $timeZone, $this->calendar); } } else { $formatter = new IntlDateFormatter($this->locale, IntlDateFormatter::NONE, IntlDateFormatter::NONE, $timeZone, $this->calendar, $format); } if ($formatter === null) { throw new InvalidConfigException(intl_get_error_message()); } // make IntlDateFormatter work with DateTimeImmutable if ($timestamp instanceof \DateTimeImmutable) { $timestamp = new DateTime($timestamp->format(DateTime::ISO8601), $timestamp->getTimezone()); } return $formatter->format($timestamp); } if (strncmp($format, 'php:', 4) === 0) { $format = substr($format, 4); } else { $format = FormatConverter::convertDateIcuToPhp($format, $type, $this->locale); } if ($timeZone != null) { if ($timestamp instanceof \DateTimeImmutable) { $timestamp = $timestamp->setTimezone(new DateTimeZone($timeZone)); } else { $timestamp->setTimezone(new DateTimeZone($timeZone)); } } return $timestamp->format($format); } /** * Normalizes the given datetime value as a DateTime object that can be taken by various date/time formatting methods. * * @param int|string|DateTime $value the datetime value to be normalized. The following * types of value are supported: * * - an integer representing a UNIX timestamp * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object * * @param bool $checkDateTimeInfo whether to also check if the date/time value has some time and date information attached. * Defaults to `false`. If `true`, the method will then return an array with the first element being the normalized * timestamp, the second a boolean indicating whether the timestamp has time information and third a boolean indicating * whether the timestamp has date information. * This parameter is available since version 2.0.1. * @return DateTime|array the normalized datetime value. * Since version 2.0.1 this may also return an array if `$checkDateTimeInfo` is true. * The first element of the array is the normalized timestamp and the second is a boolean indicating whether * the timestamp has time information or it is just a date value. * Since version 2.0.12 the array has third boolean element indicating whether the timestamp has date information * or it is just a time value. * @throws InvalidArgumentException if the input value can not be evaluated as a date value. */ protected function normalizeDatetimeValue($value, $checkDateTimeInfo = false) { // checking for DateTime and DateTimeInterface is not redundant, DateTimeInterface is only in PHP>5.5 if ($value === null || $value instanceof DateTime || $value instanceof DateTimeInterface) { // skip any processing return $checkDateTimeInfo ? [$value, true, true] : $value; } if (empty($value)) { $value = 0; } try { if (is_numeric($value)) { // process as unix timestamp, which is always in UTC $timestamp = new DateTime('@' . (int) $value, new DateTimeZone('UTC')); return $checkDateTimeInfo ? [$timestamp, true, true] : $timestamp; } elseif (($timestamp = DateTime::createFromFormat('Y-m-d|', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d format (support invalid dates like 2012-13-01) return $checkDateTimeInfo ? [$timestamp, false, true] : $timestamp; } elseif (($timestamp = DateTime::createFromFormat('Y-m-d H:i:s', $value, new DateTimeZone($this->defaultTimeZone))) !== false) { // try Y-m-d H:i:s format (support invalid dates like 2012-13-01 12:63:12) return $checkDateTimeInfo ? [$timestamp, true, true] : $timestamp; } // finally try to create a DateTime object with the value if ($checkDateTimeInfo) { $timestamp = new DateTime($value, new DateTimeZone($this->defaultTimeZone)); $info = date_parse($value); return [ $timestamp, !($info['hour'] === false && $info['minute'] === false && $info['second'] === false), !($info['year'] === false && $info['month'] === false && $info['day'] === false && empty($info['zone'])), ]; } return new DateTime($value, new DateTimeZone($this->defaultTimeZone)); } catch (\Exception $e) { throw new InvalidArgumentException("'$value' is not a valid date time value: " . $e->getMessage() . "\n" . print_r(DateTime::getLastErrors(), true), $e->getCode(), $e); } } /** * Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970). * @param int|string|DateTime $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object * * @return string the formatted result. */ public function asTimestamp($value) { if ($value === null) { return $this->nullDisplay; } $timestamp = $this->normalizeDatetimeValue($value); return number_format($timestamp->format('U'), 0, '.', ''); } /** * Formats the value as the time interval between a date and now in human readable form. * * This method can be used in three different ways: * * 1. Using a timestamp that is relative to `now`. * 2. Using a timestamp that is relative to the `$referenceTime`. * 3. Using a `DateInterval` object. * * @param int|string|DateTime|DateInterval $value the value to be formatted. The following * types of value are supported: * * - an integer representing a UNIX timestamp * - a string that can be [parsed to create a DateTime object](http://php.net/manual/en/datetime.formats.php). * The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given. * - a PHP [DateTime](http://php.net/manual/en/class.datetime.php) object * - a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future) * * @param int|string|DateTime $referenceTime if specified the value is used as a reference time instead of `now` * when `$value` is not a `DateInterval` object. * @return string the formatted result. * @throws InvalidArgumentException if the input value can not be evaluated as a date value. */ public function asRelativeTime($value, $referenceTime = null) { if ($value === null) { return $this->nullDisplay; } if ($value instanceof DateInterval) { $interval = $value; } else { $timestamp = $this->normalizeDatetimeValue($value); if ($timestamp === false) { // $value is not a valid date/time value, so we try // to create a DateInterval with it try { $interval = new DateInterval($value); } catch (\Exception $e) { // invalid date/time and invalid interval return $this->nullDisplay; } } else { $timeZone = new DateTimeZone($this->timeZone); if ($referenceTime === null) { $dateNow = new DateTime('now', $timeZone); } else { $dateNow = $this->normalizeDatetimeValue($referenceTime); $dateNow->setTimezone($timeZone); } $dateThen = $timestamp->setTimezone($timeZone); $interval = $dateThen->diff($dateNow); } } if ($interval->invert) { if ($interval->y >= 1) { return Yii::t('yii', 'in {delta, plural, =1{a year} other{# years}}', ['delta' => $interval->y], $this->locale); } if ($interval->m >= 1) { return Yii::t('yii', 'in {delta, plural, =1{a month} other{# months}}', ['delta' => $interval->m], $this->locale); } if ($interval->d >= 1) { return Yii::t('yii', 'in {delta, plural, =1{a day} other{# days}}', ['delta' => $interval->d], $this->locale); } if ($interval->h >= 1) { return Yii::t('yii', 'in {delta, plural, =1{an hour} other{# hours}}', ['delta' => $interval->h], $this->locale); } if ($interval->i >= 1) { return Yii::t('yii', 'in {delta, plural, =1{a minute} other{# minutes}}', ['delta' => $interval->i], $this->locale); } if ($interval->s == 0) { return Yii::t('yii', 'just now', [], $this->locale); } return Yii::t('yii', 'in {delta, plural, =1{a second} other{# seconds}}', ['delta' => $interval->s], $this->locale); } if ($interval->y >= 1) { return Yii::t('yii', '{delta, plural, =1{a year} other{# years}} ago', ['delta' => $interval->y], $this->locale); } if ($interval->m >= 1) { return Yii::t('yii', '{delta, plural, =1{a month} other{# months}} ago', ['delta' => $interval->m], $this->locale); } if ($interval->d >= 1) { return Yii::t('yii', '{delta, plural, =1{a day} other{# days}} ago', ['delta' => $interval->d], $this->locale); } if ($interval->h >= 1) { return Yii::t('yii', '{delta, plural, =1{an hour} other{# hours}} ago', ['delta' => $interval->h], $this->locale); } if ($interval->i >= 1) { return Yii::t('yii', '{delta, plural, =1{a minute} other{# minutes}} ago', ['delta' => $interval->i], $this->locale); } if ($interval->s == 0) { return Yii::t('yii', 'just now', [], $this->locale); } return Yii::t('yii', '{delta, plural, =1{a second} other{# seconds}} ago', ['delta' => $interval->s], $this->locale); } /** * Represents the value as duration in human readable format. * * @param DateInterval|string|int $value the value to be formatted. Acceptable formats: * - [DateInterval object](http://php.net/manual/ru/class.dateinterval.php) * - integer - number of seconds. For example: value `131` represents `2 minutes, 11 seconds` * - ISO8601 duration format. For example, all of these values represent `1 day, 2 hours, 30 minutes` duration: * `2015-01-01T13:00:00Z/2015-01-02T13:30:00Z` - between two datetime values * `2015-01-01T13:00:00Z/P1D2H30M` - time interval after datetime value * `P1D2H30M/2015-01-02T13:30:00Z` - time interval before datetime value * `P1D2H30M` - simply a date interval * `P-1D2H30M` - a negative date interval (`-1 day, 2 hours, 30 minutes`) * * @param string $implodeString will be used to concatenate duration parts. Defaults to `, `. * @param string $negativeSign will be prefixed to the formatted duration, when it is negative. Defaults to `-`. * @return string the formatted duration. * @since 2.0.7 */ public function asDuration($value, $implodeString = ', ', $negativeSign = '-') { if ($value === null) { return $this->nullDisplay; } if ($value instanceof DateInterval) { $isNegative = $value->invert; $interval = $value; } elseif (is_numeric($value)) { $isNegative = $value < 0; $zeroDateTime = (new DateTime())->setTimestamp(0); $valueDateTime = (new DateTime())->setTimestamp(abs($value)); $interval = $valueDateTime->diff($zeroDateTime); } elseif (strncmp($value, 'P-', 2) === 0) { $interval = new DateInterval('P' . substr($value, 2)); $isNegative = true; } else { $interval = new DateInterval($value); $isNegative = $interval->invert; } $parts = []; if ($interval->y > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 year} other{# years}}', ['delta' => $interval->y], $this->locale); } if ($interval->m > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 month} other{# months}}', ['delta' => $interval->m], $this->locale); } if ($interval->d > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 day} other{# days}}', ['delta' => $interval->d], $this->locale); } if ($interval->h > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 hour} other{# hours}}', ['delta' => $interval->h], $this->locale); } if ($interval->i > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 minute} other{# minutes}}', ['delta' => $interval->i], $this->locale); } if ($interval->s > 0) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 second} other{# seconds}}', ['delta' => $interval->s], $this->locale); } if ($interval->s === 0 && empty($parts)) { $parts[] = Yii::t('yii', '{delta, plural, =1{1 second} other{# seconds}}', ['delta' => $interval->s], $this->locale); $isNegative = false; } return empty($parts) ? $this->nullDisplay : (($isNegative ? $negativeSign : '') . implode($implodeString, $parts)); } // number formats /** * Formats the value as an integer number by removing any decimal digits without rounding. * * Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function * without [PHP intl extension](http://php.net/manual/en/book.intl.php) support. For very big numbers it's * recommended to pass them as strings and not use scientific notation otherwise the output might be wrong. * * @param mixed $value the value to be formatted. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. */ public function asInteger($value, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } $normalizedValue = $this->normalizeNumericValue($value); if ($this->isNormalizedValueMispresented($value, $normalizedValue)) { return $this->asIntegerStringFallback((string) $value); } if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::DECIMAL, null, $options, $textOptions); $f->setAttribute(NumberFormatter::FRACTION_DIGITS, 0); if (($result = $f->format($normalizedValue, NumberFormatter::TYPE_INT64)) === false) { throw new InvalidArgumentException('Formatting integer value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } return number_format((int) $normalizedValue, 0, $this->decimalSeparator, $this->thousandSeparator); } /** * Formats the value as a decimal number. * * Property [[decimalSeparator]] will be used to represent the decimal point. The * value is rounded automatically to the defined decimal digits. * * Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function * without [PHP intl extension](http://php.net/manual/en/book.intl.php) support. For very big numbers it's * recommended to pass them as strings and not use scientific notation otherwise the output might be wrong. * * @param mixed $value the value to be formatted. * @param int $decimals the number of digits after the decimal point. * If not given, the number of digits depends in the input value and is determined based on * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured * using [[$numberFormatterOptions]]. * If the PHP intl extension is not available, the default value is `2`. * If you want consistent behavior between environments where intl is available and not, you should explicitly * specify a value here. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @see decimalSeparator * @see thousandSeparator */ public function asDecimal($value, $decimals = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } $normalizedValue = $this->normalizeNumericValue($value); if ($this->isNormalizedValueMispresented($value, $normalizedValue)) { return $this->asDecimalStringFallback((string) $value, $decimals); } if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::DECIMAL, $decimals, $options, $textOptions); if (($result = $f->format($normalizedValue)) === false) { throw new InvalidArgumentException('Formatting decimal value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } if ($decimals === null) { $decimals = 2; } return number_format($normalizedValue, $decimals, $this->decimalSeparator, $this->thousandSeparator); } /** * Formats the value as a percent number with "%" sign. * * Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function * without [PHP intl extension](http://php.net/manual/en/book.intl.php) support. For very big numbers it's * recommended to pass them as strings and not use scientific notation otherwise the output might be wrong. * * @param mixed $value the value to be formatted. It must be a factor e.g. `0.75` will result in `75%`. * @param int $decimals the number of digits after the decimal point. * If not given, the number of digits depends in the input value and is determined based on * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured * using [[$numberFormatterOptions]]. * If the PHP intl extension is not available, the default value is `0`. * If you want consistent behavior between environments where intl is available and not, you should explicitly * specify a value here. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. */ public function asPercent($value, $decimals = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } $normalizedValue = $this->normalizeNumericValue($value); if ($this->isNormalizedValueMispresented($value, $normalizedValue)) { return $this->asPercentStringFallback((string) $value, $decimals); } if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::PERCENT, $decimals, $options, $textOptions); if (($result = $f->format($normalizedValue)) === false) { throw new InvalidArgumentException('Formatting percent value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } if ($decimals === null) { $decimals = 0; } $normalizedValue *= 100; return number_format($normalizedValue, $decimals, $this->decimalSeparator, $this->thousandSeparator) . '%'; } /** * Formats the value as a scientific number. * * @param mixed $value the value to be formatted. * @param int $decimals the number of digits after the decimal point. * If not given, the number of digits depends in the input value and is determined based on * `NumberFormatter::MIN_FRACTION_DIGITS` and `NumberFormatter::MAX_FRACTION_DIGITS`, which can be configured * using [[$numberFormatterOptions]]. * If the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available, the default value depends on your PHP configuration. * If you want consistent behavior between environments where intl is available and not, you should explicitly * specify a value here. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. */ public function asScientific($value, $decimals = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } $value = $this->normalizeNumericValue($value); if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::SCIENTIFIC, $decimals, $options, $textOptions); if (($result = $f->format($value)) === false) { throw new InvalidArgumentException('Formatting scientific number value failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } if ($decimals !== null) { return sprintf("%.{$decimals}E", $value); } return sprintf('%.E', $value); } /** * Formats the value as a currency number. * * This function does not require the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed * to work, but it is highly recommended to install it to get good formatting results. * * Since 2.0.16 numbers that are mispresented after normalization are formatted as strings using fallback function * without PHP intl extension support. For very big numbers it's recommended to pass them as strings and not use * scientific notation otherwise the output might be wrong. * * @param mixed $value the value to be formatted. * @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use. * If null, [[currencyCode]] will be used. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined. */ public function asCurrency($value, $currency = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } $normalizedValue = $this->normalizeNumericValue($value); if ($this->isNormalizedValueMispresented($value, $normalizedValue)) { return $this->asCurrencyStringFallback((string) $value, $currency); } if ($this->_intlLoaded) { $currency = $currency ?: $this->currencyCode; // currency code must be set before fraction digits // http://php.net/manual/en/numberformatter.formatcurrency.php#114376 if ($currency && !isset($textOptions[NumberFormatter::CURRENCY_CODE])) { $textOptions[NumberFormatter::CURRENCY_CODE] = $currency; } $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions); if ($currency === null) { $result = $formatter->format($normalizedValue); } else { $result = $formatter->formatCurrency($normalizedValue, $currency); } if ($result === false) { throw new InvalidArgumentException('Formatting currency value failed: ' . $formatter->getErrorCode() . ' ' . $formatter->getErrorMessage()); } return $result; } if ($currency === null) { if ($this->currencyCode === null) { throw new InvalidConfigException('The default currency code for the formatter is not defined and the php intl extension is not installed which could take the default currency from the locale.'); } $currency = $this->currencyCode; } return $currency . ' ' . $this->asDecimal($normalizedValue, 2, $options, $textOptions); } /** * Formats the value as a number spellout. * * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. * * This formatter does not work well with very big numbers. * * @param mixed $value the value to be formatted * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. */ public function asSpellout($value) { if ($value === null) { return $this->nullDisplay; } $value = $this->normalizeNumericValue($value); if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::SPELLOUT); if (($result = $f->format($value)) === false) { throw new InvalidArgumentException('Formatting number as spellout failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } throw new InvalidConfigException('Format as Spellout is only supported when PHP intl extension is installed.'); } /** * Formats the value as a ordinal value of a number. * * This function requires the [PHP intl extension](http://php.net/manual/en/book.intl.php) to be installed. * * This formatter does not work well with very big numbers. * * @param mixed $value the value to be formatted * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @throws InvalidConfigException when the [PHP intl extension](http://php.net/manual/en/book.intl.php) is not available. */ public function asOrdinal($value) { if ($value === null) { return $this->nullDisplay; } $value = $this->normalizeNumericValue($value); if ($this->_intlLoaded) { $f = $this->createNumberFormatter(NumberFormatter::ORDINAL); if (($result = $f->format($value)) === false) { throw new InvalidArgumentException('Formatting number as ordinal failed: ' . $f->getErrorCode() . ' ' . $f->getErrorMessage()); } return $result; } throw new InvalidConfigException('Format as Ordinal is only supported when PHP intl extension is installed.'); } /** * Formats the value in bytes as a size in human readable form for example `12 KB`. * * This is the short form of [[asSize]]. * * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) * are used in the formatting result. * * @param string|int|float $value value in bytes to be formatted. * @param int $decimals the number of digits after the decimal point. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @see sizeFormatBase * @see asSize */ public function asShortSize($value, $decimals = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } list($params, $position) = $this->formatNumber($value, $decimals, 4, $this->sizeFormatBase, $options, $textOptions); if ($this->sizeFormatBase == 1024) { switch ($position) { case 0: return Yii::t('yii', '{nFormatted} B', $params, $this->locale); case 1: return Yii::t('yii', '{nFormatted} KiB', $params, $this->locale); case 2: return Yii::t('yii', '{nFormatted} MiB', $params, $this->locale); case 3: return Yii::t('yii', '{nFormatted} GiB', $params, $this->locale); case 4: return Yii::t('yii', '{nFormatted} TiB', $params, $this->locale); default: return Yii::t('yii', '{nFormatted} PiB', $params, $this->locale); } } else { switch ($position) { case 0: return Yii::t('yii', '{nFormatted} B', $params, $this->locale); case 1: return Yii::t('yii', '{nFormatted} KB', $params, $this->locale); case 2: return Yii::t('yii', '{nFormatted} MB', $params, $this->locale); case 3: return Yii::t('yii', '{nFormatted} GB', $params, $this->locale); case 4: return Yii::t('yii', '{nFormatted} TB', $params, $this->locale); default: return Yii::t('yii', '{nFormatted} PB', $params, $this->locale); } } } /** * Formats the value in bytes as a size in human readable form, for example `12 kilobytes`. * * If [[sizeFormatBase]] is 1024, [binary prefixes](http://en.wikipedia.org/wiki/Binary_prefix) (e.g. kibibyte/KiB, mebibyte/MiB, ...) * are used in the formatting result. * * @param string|int|float $value value in bytes to be formatted. * @param int $decimals the number of digits after the decimal point. * @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @see sizeFormatBase * @see asShortSize */ public function asSize($value, $decimals = null, $options = [], $textOptions = []) { if ($value === null) { return $this->nullDisplay; } list($params, $position) = $this->formatNumber($value, $decimals, 4, $this->sizeFormatBase, $options, $textOptions); if ($this->sizeFormatBase == 1024) { switch ($position) { case 0: return Yii::t('yii', '{nFormatted} {n, plural, =1{byte} other{bytes}}', $params, $this->locale); case 1: return Yii::t('yii', '{nFormatted} {n, plural, =1{kibibyte} other{kibibytes}}', $params, $this->locale); case 2: return Yii::t('yii', '{nFormatted} {n, plural, =1{mebibyte} other{mebibytes}}', $params, $this->locale); case 3: return Yii::t('yii', '{nFormatted} {n, plural, =1{gibibyte} other{gibibytes}}', $params, $this->locale); case 4: return Yii::t('yii', '{nFormatted} {n, plural, =1{tebibyte} other{tebibytes}}', $params, $this->locale); default: return Yii::t('yii', '{nFormatted} {n, plural, =1{pebibyte} other{pebibytes}}', $params, $this->locale); } } else { switch ($position) { case 0: return Yii::t('yii', '{nFormatted} {n, plural, =1{byte} other{bytes}}', $params, $this->locale); case 1: return Yii::t('yii', '{nFormatted} {n, plural, =1{kilobyte} other{kilobytes}}', $params, $this->locale); case 2: return Yii::t('yii', '{nFormatted} {n, plural, =1{megabyte} other{megabytes}}', $params, $this->locale); case 3: return Yii::t('yii', '{nFormatted} {n, plural, =1{gigabyte} other{gigabytes}}', $params, $this->locale); case 4: return Yii::t('yii', '{nFormatted} {n, plural, =1{terabyte} other{terabytes}}', $params, $this->locale); default: return Yii::t('yii', '{nFormatted} {n, plural, =1{petabyte} other{petabytes}}', $params, $this->locale); } } } /** * Formats the value as a length in human readable form for example `12 meters`. * Check properties [[baseUnits]] if you need to change unit of value as the multiplier * of the smallest unit and [[systemOfUnits]] to switch between [[UNIT_SYSTEM_METRIC]] or [[UNIT_SYSTEM_IMPERIAL]]. * * @param float|int $value value to be formatted. * @param int $decimals the number of digits after the decimal point. * @param array $numberOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. * @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. * @return string the formatted result. * @throws InvalidArgumentException if the input value is not numeric or the formatting failed. * @throws InvalidConfigException when INTL is not installed or does not contain required information. * @see asLength * @since 2.0.13 * @author John Was