Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
313 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
<?php | ||
|
||
namespace NwLaravel\Locale; | ||
|
||
use Locale as PhpLocale; | ||
|
||
class Locale | ||
{ | ||
public static function extensoOrdinal($number) | ||
{ | ||
return self::numberToWords($number, false, true); | ||
} | ||
|
||
public static function extensoCurrency($number) | ||
{ | ||
return self::numberToWords($number, true, false); | ||
} | ||
|
||
public static function extensoCardinal($number) | ||
{ | ||
return self::numberToWords($number, false, false); | ||
} | ||
|
||
public static function extenso($number) | ||
{ | ||
return self::extensoCurrency($number); | ||
} | ||
|
||
protected static function translate($currency = false, $ordinals = false) | ||
{ | ||
$translate = array(); | ||
|
||
if ($ordinals) { | ||
$translate['singular'] = array("", "", "milésimo", "milhão", "bilhão", "trilhão", "quatrilhão"); | ||
$translate['plural'] = array("", "", "milésimo", "milhões", "bilhões", "trilhões", "quatrilhões"); | ||
$translate['centanas'] = array("", "centésimo", "ducentésimo", "trecentésimo", "quadrigentésimo", "quingentésimo", "sexcentésimo", "septigentésimo", "octigentésimo", "nongentésimo"); | ||
$translate['dezenas'] = array("", "décimo", "vigésimo", "trigésimo", "quadragésimo", "quinquagésimo", "sexagésimo", "septuagésimo", "octogésimo", "nonagésimo"); | ||
$translate['dezenas10'] = array("décimo", "décimo primeiro", "décimo segundo", "décimo terceiro", "décimo quarto", "décimo quinto", "décimo sexto", "décimo sétimo", "décimo oitavo", "décimo nono"); | ||
$translate['unidades'] = array("", "primeiro", "segundo", "terceiro", "quarto", "quinto", "sexto", "sétimo", "oitavo", "nono"); | ||
$translate['cento'] = "centésimo"; | ||
$translate['separator'] = " "; | ||
$translate['preposicao'] = " "; | ||
$translate['zero'] = "-"; | ||
} else { | ||
$translate['singular'] = array("", "", "mil", "milhão", "bilhão", "trilhão", "quatrilhão"); | ||
$translate['plural'] = array("", "", "mil", "milhões", "bilhões", "trilhões", "quatrilhões"); | ||
$translate['centanas'] = array("", "cem", "duzentos", "trezentos", "quatrocentos", "quinhentos", "seiscentos", "setecentos", "oitocentos", "novecentos"); | ||
$translate['dezenas'] = array("", "dez", "vinte", "trinta", "quarenta", "cinquenta", "sessenta", "setenta", "oitenta", "noventa"); | ||
$translate['dezenas10'] = array("dez", "onze", "doze", "treze", "quatorze", "quinze", "dezesseis", "dezessete", "dezoito", "dezenove"); | ||
$translate['unidades'] = array("", "um", "dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove"); | ||
$translate['cento'] = "cento"; | ||
$translate['separator'] = " e "; | ||
$translate['preposicao'] = ""; | ||
$translate['zero'] = "zero"; | ||
$translate['separator_decimal'] = ", "; | ||
|
||
if($currency){ | ||
$translate['singular'][0] = "centavo"; | ||
$translate['singular'][1] = "real"; | ||
$translate['plural'][0] = "centavos"; | ||
$translate['plural'][1] = "reais"; | ||
$translate['zero'] = "zero reais"; | ||
$translate['preposicao'] = " de "; // ex: um milhao de reais | ||
$translate['separator_decimal'] = " e "; | ||
} | ||
} | ||
|
||
return $translate; | ||
} | ||
|
||
/** | ||
* Number to Extenso String | ||
* | ||
* @param float $number | ||
* | ||
* @return string | ||
*/ | ||
protected static function numberToWords($number, $currency = false, $ordinals = false) | ||
{ | ||
$number = trim($number); | ||
if (preg_match("/[^0-9\.]/", $number)) { | ||
return $number; | ||
} | ||
|
||
$number = $ordinals ? intval($number) : floatval($number); | ||
|
||
if ($ordinals && $number >= 1000) { | ||
return $number.'º'; | ||
} | ||
|
||
$translate = self::translate($currency, $ordinals); | ||
|
||
$number = number_format($number, 2, ".", "."); | ||
$inteiro = explode(".", $number); | ||
for ($i=0;$i<count($inteiro);$i++) { | ||
for ($ii=strlen($inteiro[$i]);$ii<3;$ii++) { | ||
$inteiro[$i] = "0".$inteiro[$i]; | ||
} | ||
} | ||
|
||
$fim = count($inteiro) - ($inteiro[count($inteiro)-1] > 0 ? 1 : 2); | ||
$rt = ''; | ||
$z = 0; | ||
for ($i=0;$i<count($inteiro);$i++) { | ||
$number = $inteiro[$i]; | ||
if (($number > 100) && ($number < 200)) { | ||
$rc = $translate['cento']; | ||
} else { | ||
$rc = $translate['centanas'][$number[0]]; | ||
} | ||
|
||
if ($number[1] < 2) { | ||
$rd = ""; | ||
} else { | ||
$rd = $translate['dezenas'][$number[1]]; | ||
} | ||
|
||
if ($number > 0) { | ||
if ($number[1] == 1) { | ||
$ru = $translate['dezenas10'][$number[2]]; | ||
} else { | ||
$ru = $translate['unidades'][$number[2]]; | ||
} | ||
} else { | ||
$ru = ""; | ||
} | ||
|
||
$r = $rc; | ||
if ($rc && ($rd || $ru)) { | ||
$r .= $translate['separator']; | ||
} | ||
|
||
$r .= $rd; | ||
if ($rd && $ru) { | ||
$r .= $translate['separator']; | ||
} | ||
|
||
$r .= $ru; | ||
|
||
$t = count($inteiro)-1-$i; | ||
|
||
if ($r) { | ||
$r .= " "; | ||
if ($number > 1) { | ||
$r .= $translate['plural'][$t]; | ||
} else { | ||
$r .= $translate['singular'][$t]; | ||
} | ||
} | ||
|
||
if ($number == "000") { | ||
$z++; | ||
} elseif ($z > 0) { | ||
$z--; | ||
} | ||
|
||
if (($t==1) && ($z>0) && ($inteiro[0] > 0)) { | ||
if ($z > 1) { | ||
$r .= $translate['preposicao']; | ||
} | ||
|
||
$r .= $translate['plural'][$t]; | ||
} | ||
|
||
if ($r) { | ||
$rt = $rt; | ||
if (($i > 0) && ($i <= $fim) && ($inteiro[0] > 0) && ($z < 1)) { | ||
|
||
$rt .= (!$currency && $t == 0 && $i==$fim) ? ', ' : $translate['separator']; | ||
|
||
} else { | ||
if ($t == 0 && $rt) { | ||
$rt .= $currency ? $translate['separator'] : ', '; | ||
} else { | ||
$rt .= " "; | ||
} | ||
} | ||
|
||
$rt .= $r; | ||
} | ||
} | ||
|
||
$rt = preg_replace("/\s+/", " ", $rt); | ||
$rt = preg_replace("/\s+,\s*/", ", ", $rt); | ||
|
||
return trim($rt) ?: $translate['zero']; | ||
} | ||
|
||
/** | ||
* Ufs | ||
* | ||
* @return array | ||
*/ | ||
public static function states() | ||
{ | ||
return config('locales.ufs'); | ||
} | ||
|
||
/** | ||
* Sigla Ufs | ||
* | ||
* @return array | ||
*/ | ||
public static function siglaStates() | ||
{ | ||
$keys = array_keys(self::ufs()); | ||
return array_combine($keys, $keys); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Tests\OAuth; | ||
|
||
use Tests\TestCase; | ||
use NwLaravel\Locale\Locale; | ||
|
||
class LocaleTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider providerOrdinals | ||
*/ | ||
public function testExtensoOrdinal($expected, $number) | ||
{ | ||
$this->assertEquals($expected, Locale::extensoOrdinal($number)); | ||
} | ||
|
||
public function providerOrdinals() | ||
{ | ||
return [ | ||
['décimo', ' 10.00 '], | ||
['10,00', '10,00'], | ||
['-', 0], | ||
['primeiro', 1.33], | ||
['vigésimo primeiro', 21], | ||
['1000º', 1000], | ||
['nongentésimo nonagésimo nono', 999], | ||
['103580º', 103580.10], | ||
['centésimo décimo sétimo', 117], | ||
['centésimo', 100], | ||
['quarto', 4], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerCardinals | ||
*/ | ||
public function testExtensoCardinal($expected, $number) | ||
{ | ||
$this->assertEquals($expected, Locale::extensoCardinal($number)); | ||
} | ||
|
||
public function providerCardinals() | ||
{ | ||
return [ | ||
['dez', ' 10.00 '], | ||
['10,00', '10,00'], | ||
['zero', 0], | ||
['treze', 13], | ||
['vinte e um', 21], | ||
['cento e dois', 102], | ||
['um mil e trinta e nove', 1039], | ||
['cento e tres mil e quinhentos e oitenta', 103580], | ||
['cento e dezessete', 117], | ||
['cem', 100], | ||
['quatro', 4], | ||
['oito mil', 8000], | ||
['oitenta mil', 80000], | ||
['oitocentos mil', 800000], | ||
['oito milhões', 8000000], | ||
['cem, onze', 100.11], | ||
['onze mil e trezentos e sessenta e quatro, oitenta e nove', 11364.89], | ||
['um milhão, quarenta e quatro', 1000000.44], | ||
['um milhão e duzentos mil, quarenta e quatro', 1200000.44], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerCurrencies | ||
*/ | ||
public function testExtensoCurrency($expected, $number) | ||
{ | ||
$this->assertEquals($expected, Locale::extensoCurrency($number)); | ||
} | ||
|
||
public function providerCurrencies() | ||
{ | ||
return [ | ||
['um real', 1], | ||
['dez reais', ' 10.00 '], | ||
['10,00', '10,00'], | ||
['zero reais', 0], | ||
['setenta e sete centavos', 0.77], | ||
['um centavo', 0.01], | ||
['vinte e um reais', 21.00], | ||
['cem reais', 100.00], | ||
['cem reais e doze centavos', 100.12], | ||
['um mil e trinta e nove reais e quarenta e tres centavos', 1039.43], | ||
['cento e tres mil e quinhentos e oitenta reais e um centavo', 103580.01], | ||
['cento e dezessete reais e sessenta e sete centavos', 117.67], | ||
['cem reais e dezessete centavos', 100.17], | ||
['quatro reais e noventa e nove centavos', 4.99], | ||
['um milhão de reais e quarenta e quatro centavos', 1000000.44], | ||
['cem milhões de reais', 100000000], | ||
['um milhão e duzentos mil reais e quarenta e quatro centavos', 1200000.44], | ||
['oito mil reais', 8000], | ||
['oitenta mil reais', 80000], | ||
['oitocentos mil reais', 800000], | ||
['oito milhões de reais', 8000000], | ||
]; | ||
} | ||
} |