Skip to content
Permalink
Browse files

Make stringish hardcoded values a class constant.

  • Loading branch information...
dereuromark committed Sep 8, 2015
1 parent db23f62 commit b4344f5a8f57ddc97f18253f0c962ae55a643cdb
Showing with 43 additions and 8 deletions.
  1. +9 −2 src/I18n/I18n.php
  2. +26 −5 src/I18n/Number.php
  3. +8 −1 src/Validation/Validation.php
@@ -28,6 +28,13 @@
class I18n
{
/**
* Default locale
*
* @var string
*/
const DEFAULT_LOCALE = 'en_US';
/**
* The translators collection
*
@@ -215,7 +222,7 @@ public static function locale($locale = null)
$current = Locale::getDefault();
if ($current === '') {
$current = 'en_US';
$current = static::DEFAULT_LOCALE;
Locale::setDefault($current);
}
@@ -232,7 +239,7 @@ public static function locale($locale = null)
public static function defaultLocale()
{
if (static::$_defaultLocale === null) {
static::$_defaultLocale = Locale::getDefault() ?: 'en_US';
static::$_defaultLocale = Locale::getDefault() ?: static::DEFAULT_LOCALE;
}
return static::$_defaultLocale;
}
@@ -26,6 +26,20 @@
class Number
{
/**
* Default locale
*
* @var string
*/
const DEFAULT_LOCALE = 'en_US';
/**
* Format type to format as currency
*
* @var string
*/
const FORMAT_CURRENCY = 'currency';
/**
* A list of number formatters indexed by locale and type
*
@@ -203,7 +217,7 @@ public static function currency($value, $currency = null, array $options = [])
return $options['zero'];
}
$formatter = static::formatter(['type' => 'currency'] + $options);
$formatter = static::formatter(['type' => static::FORMAT_CURRENCY] + $options);
$abs = abs($value);
if (!empty($options['fractionSymbol']) && $abs > 0 && $abs < 1) {
$value = $value * 100;
@@ -235,7 +249,7 @@ public static function defaultCurrency($currency = null)
}
if (empty(self::$_defaultCurrency)) {
$locale = ini_get('intl.default_locale') ?: 'en_US';
$locale = ini_get('intl.default_locale') ?: static::DEFAULT_LOCALE;
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
self::$_defaultCurrency = $formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE);
}
@@ -249,7 +263,7 @@ public static function defaultCurrency($currency = null)
* using other methods in this class as only one formatter object needs to be
* constructed.
*
* The options array accepts the following keys:
* ### Options
*
* - `locale` - The locale name to use for formatting the number, e.g. fr_FR
* - `type` - The formatter type to construct, set it to `currency` if you need to format
@@ -268,13 +282,13 @@ public static function formatter($options = [])
$locale = isset($options['locale']) ? $options['locale'] : ini_get('intl.default_locale');
if (!$locale) {
$locale = 'en_US';
$locale = static::DEFAULT_LOCALE;
}
$type = NumberFormatter::DECIMAL;
if (!empty($options['type'])) {
$type = $options['type'];
if ($options['type'] === 'currency') {
if ($options['type'] === static::FORMAT_CURRENCY) {
$type = NumberFormatter::CURRENCY;
}
}
@@ -319,6 +333,13 @@ public static function formatter($options = [])
/**
* Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
*
* ### Options
*
* - `type` - The formatter type to construct, set it to `currency` if you need to format
* numbers representing money or a NumberFormatter constant.
*
* For all other options see formatter().
*
* @param int|float $value An integer
* @param array $options An array with options.
* @return string
@@ -28,6 +28,13 @@
class Validation
{
/**
* Default locale
*
* @var string
*/
const DEFAULT_LOCALE = 'en_US';
/**
* Some complex patterns needed in multiple places
*
@@ -495,7 +502,7 @@ public static function decimal($check, $places = null, $regex = null)
}
// account for localized floats.
$locale = ini_get('intl.default_locale') ?: 'en_US';
$locale = ini_get('intl.default_locale') ?: static::DEFAULT_LOCALE;
$formatter = new NumberFormatter($locale, NumberFormatter::DECIMAL);
$decimalPoint = $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$groupingSep = $formatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);

0 comments on commit b4344f5

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.