Skip to content

Commit

Permalink
Make stringish hardcoded values a class constant.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Sep 8, 2015
1 parent db23f62 commit b4344f5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/I18n/I18n.php
Expand Up @@ -28,6 +28,13 @@
class I18n
{

/**
* Default locale
*
* @var string
*/
const DEFAULT_LOCALE = 'en_US';

/**
* The translators collection
*
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand Down
31 changes: 26 additions & 5 deletions src/I18n/Number.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -28,6 +28,13 @@
class Validation
{

/**
* Default locale
*
* @var string
*/
const DEFAULT_LOCALE = 'en_US';

/**
* Some complex patterns needed in multiple places
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b4344f5

Please sign in to comment.