Permalink
Browse files

Merge branch 'master' of github.com:radig/locale

Conflicts:
	libs/localize.php
  • Loading branch information...
CauanCabral committed Apr 26, 2012
2 parents 3cea00d + caf13a8 commit ba6bfab17189057bcf17890620bb0b040c71abb1
Showing with 325 additions and 139 deletions.
  1. +11 −44 libs/localize.php
  2. +6 −8 libs/unlocalize.php
  3. +105 −0 libs/utils.php
  4. +161 −87 models/behaviors/locale.php
  5. +42 −0 tests/cases/libs/utils.test.php
View
@@ -56,9 +56,12 @@ public static function getInstance()
*/
static public function setLocale($locale)
{
- if(!setlocale(LC_ALL, $locale))
+ if(!setlocale(LC_ALL, array($locale . '.utf-8', $locale)))
throw new LocaleException("Locale {$locale} não disponível no seu sistema.");
+ if(!isset(Formats::$output[$locale]))
+ throw new LocaleException("Localização '{$locale}' não possuí formatação definida. Tente adicionar o formato antes de usa-lo.");
+
self::$currentLocale = $locale;
return self::getInstance();
@@ -145,22 +148,14 @@ static public function dateLiteral($value, $displayTime = false, $format = null)
*/
static public function currency($value)
{
- if(!is_string($value))
- {
- setlocale(LC_NUMERIC, 'en_US');
- $value = (string)$value;
- setlocale(LC_NUMERIC, self::$currentLocale);
- }
-
$currentFormat = localeconv();
- $value = str_replace(',', '', $value);
- if(empty($value) || !is_numeric($value))
- return $value;
+ $number = Utils::numberFormat($value, 2, true, $currentFormat['mon_decimal_point'], $currentFormat['mon_thousands_sep']);
- $currency = $currentFormat['currency_symbol'] . ' ' . self::number($value, 2, true);
+ if($number === false)
+ return $value;
- return $currency;
+ return $currentFormat['currency_symbol'] . ' ' . $number;
}
/**
@@ -179,40 +174,12 @@ static function number($value, $precision = null, $thousands = false)
if($precision === null)
$precision = 2;
- if(!is_string($value))
- {
- setlocale(LC_NUMERIC, 'en_US');
- $value = (string)$value;
- setlocale(LC_NUMERIC, self::$currentLocale);
- }
-
$currentFormat = localeconv();
- $value = (string)$value;
- $value = str_replace(',', '', $value);
-
- $parts = explode('.', $value);
+ $number = Utils::numberFormat($value, $precision, $thousands, $currentFormat['decimal_point'], $currentFormat['thousands_sep']);
- if(count($parts) == 2)
- {
- $int = (string)$parts[0];
- $dec = str_pad((string)$parts[1], $precision, '0', STR_PAD_RIGHT);
- }
- else
- {
- $int = (string)$parts[0];
- $dec = str_repeat('0', $precision);
- }
-
- $dec = substr($dec, 0, $precision);
-
- if($thousands)
- $int = number_format($int, 0, $currentFormat['decimal_point'], $currentFormat['thousands_sep']);
-
- $number = $int;
-
- if(!empty($dec))
- $number .= $currentFormat['decimal_point'] . $dec;
+ if($number === false)
+ return $value;
return $number;
}
View
@@ -56,9 +56,12 @@ public static function getInstance()
*/
static public function setLocale($locale)
{
- if(!setlocale(LC_ALL, $locale))
+ if(!setlocale(LC_ALL, array($locale . '.utf-8', $locale)))
throw new LocaleException("Locale {$locale} não disponível no seu sistema.");
+ if(!isset(Formats::$input[$locale]))
+ throw new LocaleException("Localização '{$locale}' não possuí formatação definida. Tente adicionar o formato antes de usa-lo.");
+
self::$currentLocale = $locale;
return self::getInstance();
@@ -134,13 +137,10 @@ static public function decimal($value)
if(empty($value))
return $value;
- $oldLocale = setlocale(LC_NUMERIC, "0");
- setlocale(LC_NUMERIC, self::$currentLocale);
+ $currentFormat = localeconv();
$v = (string)$value;
- $currentFormat = localeconv();
-
$integer = $v;
$decimal = 0;
@@ -150,15 +150,13 @@ static public function decimal($value)
$decimal = substr($v, $decimalPoint + 1);
$integer = substr($v, 0, $decimalPoint);
- $integer = preg_replace('/[\.|,]/', '', $integer);
+ $integer = str_replace(array($currentFormat['thousands_sep'], $currentFormat['mon_thousands_sep']), '', $integer);
}
$value = $integer;
if($decimal > 0)
$value .= '.' . $decimal;
- setlocale(LC_NUMERIC, $oldLocale);
-
return $value;
}
View
@@ -71,4 +71,109 @@ static public function initDateTime($value)
return new DateTime();
}
}
+
+ /**
+ * Replacement function for number_format, with extras:
+ * - Use truncate over round
+ * - Optional use of thousands
+ *
+ * @param mixed $value
+ * @param int $precision
+ * @param bool $thousands
+ * @param string $decimalSep
+ * @param string $thousandSep
+ *
+ * @return mixed String numeric representation in success and False boolean on
+ * invalid numeric values.
+ */
+ static public function numberFormat($value, $precision = null, $thousands = false, $decimalSep = '.', $thousandSep = ',')
+ {
+ if($precision === null)
+ $precision = 2;
+
+ if(!is_string($value))
+ {
+ $oldLocale = setlocale(LC_NUMERIC, "");
+ setlocale(LC_NUMERIC, 'en_US');
+ $value = (string)$value;
+ setlocale(LC_NUMERIC, $oldLocale);
+ }
+
+ $value = str_replace(',', '', $value);
+
+ if(empty($value) || !is_numeric($value))
+ return false;
+
+ $parts = explode('.', $value);
+
+ if(count($parts) == 2)
+ {
+ $int = (string)$parts[0];
+ $dec = str_pad((string)$parts[1], $precision, '0', STR_PAD_RIGHT);
+ }
+ else
+ {
+ $int = (string)$parts[0];
+ $dec = str_repeat('0', $precision);
+ }
+
+ $dec = substr($dec, 0, $precision);
+
+ if($thousands)
+ {
+ $copy = '';
+ for($l = strlen($int) - 1, $c = 0; $l >= 0; $l--, $c++)
+ {
+ $copy = $int[$l] . $copy;
+
+ if($c === 2 && $l > 0)
+ {
+ $c = -1;
+ $copy = $thousandSep . $copy;
+ }
+ }
+ $int = $copy;
+ }
+
+ $number = $int;
+
+ if(!empty($dec))
+ $number .= $decimalSep . $dec;
+
+ return $number;
+ }
+
+ /**
+ * Extract Model and field name from a conditions
+ * consult string.
+ *
+ * @param string $raw Query correspondent field
+ * @return array Two position array with Model and field name
+ * respectively
+ */
+ static public function parseModelField($raw)
+ {
+ $validField = '/^([a-zA-Z][a-zA-Z0-9]*)(\\.[a-zA-Z][a-zA-Z0-9]*)?/';
+ $matched = array();
+
+ $field = $raw;
+ $modelName = '';
+
+ if(preg_match($validField, $raw, $matched) === 1)
+ {
+ $field = $matched[0];
+
+ // If condition have Model.field sintax
+ if(strpos($field, '.') !== false)
+ {
+ $ini = strpos($field, '.');
+
+ $modelName = substr($field, 0, $ini);
+ $field = substr($field, $ini + 1);
+ }
+
+ }
+
+ return array($modelName, $field);
+ }
}
Oops, something went wrong.

0 comments on commit ba6bfab

Please sign in to comment.