forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.5 KiB
133 lines
3.5 KiB
2 years ago
|
<?php
|
||
|
|
||
|
define("MAX_NUM_EXTENSO", 999999999.99);
|
||
|
|
||
|
//var_dump(VocalizaNumero(1250.33));
|
||
|
|
||
|
function VocalizaNumero($numero, $rec = false, $cent = false) {
|
||
|
$valorI = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900);
|
||
|
/*
|
||
|
* A funcao trata numeros ate o valor da constante MAX_NUM_EXTENSO.
|
||
|
*/
|
||
|
if ($numero > MAX_NUM_EXTENSO) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ($rec) {
|
||
|
/*
|
||
|
* Armazena os numeros que serao retornados.
|
||
|
*/
|
||
|
$numeros = array();
|
||
|
|
||
|
/*
|
||
|
* Numero ja tratado.
|
||
|
*/
|
||
|
if (VocalizaDef($valorI, $numero)) {
|
||
|
return array(VocalizaAdd($numero, $numero));
|
||
|
}
|
||
|
|
||
|
$i = 0;
|
||
|
do {
|
||
|
$tam = strlen($numero);
|
||
|
$num = substr($numero, 0, 1);
|
||
|
|
||
|
/*
|
||
|
* Apenas numeros > 0 devem ser tratado aqui.
|
||
|
*/
|
||
|
if (!((int) $num)) {
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Adiciona um numero a ser vocalizado.
|
||
|
*/
|
||
|
$numeros[] = VocalizaAdd($numero, ($num . str_repeat('0', $tam - 1)));
|
||
|
$num = (int) substr($numero, 1);
|
||
|
|
||
|
|
||
|
if (VocalizaDef($valorI, $num)) {
|
||
|
$numeros[] = VocalizaAdd($numero, $num);
|
||
|
break;
|
||
|
}
|
||
|
$i++;
|
||
|
$numero = $num;
|
||
|
} while ($tam > 1);
|
||
|
|
||
|
return $numeros;
|
||
|
}
|
||
|
|
||
|
$sing = array(0 => array('real', 'centavo'), 1 => '', 2 => 'Mil', 3 => 'Milhao');
|
||
|
$plur = array(0 => array('reais', 'centavos'), 1 => '', 2 => 'Mil', 3 => 'Milhoes');
|
||
|
|
||
|
//list($valor, $centavo) = explode('.', $numero);
|
||
|
$arVal = explode('.', $numero);
|
||
|
$numeros = array();
|
||
|
$i = 0;
|
||
|
|
||
|
foreach ($arVal as $valor) {
|
||
|
$cent = $i;
|
||
|
$valores = VocalizaQuebra($valor); // print_r($valores); exit;
|
||
|
|
||
|
$idxExt = count($valores);
|
||
|
foreach ($valores as $vQuebra) {
|
||
|
foreach (VocalizaNumero((int) $vQuebra, true, $cent) as $vExt) {
|
||
|
$numeros[] = $vExt;
|
||
|
}
|
||
|
|
||
|
$ext = $vQuebra[0] > 1 ? $plur[$idxExt] : $sing[$idxExt];
|
||
|
if ($ext) {
|
||
|
$numeros[] = $ext;
|
||
|
}
|
||
|
$idxExt--;
|
||
|
}
|
||
|
|
||
|
$idxVl = $cent ? 1 : 0;
|
||
|
$ext = ((int) $valor > 1) ? $plur[0][$idxVl] : $sing[0][$idxVl];
|
||
|
if ($ext) {
|
||
|
$numeros[] = $ext;
|
||
|
}
|
||
|
$i++;
|
||
|
}
|
||
|
|
||
|
return VocalizaConector($numeros);
|
||
|
}
|
||
|
|
||
|
function VocalizaQuebra($valor) {
|
||
|
$tam = strlen($valor);
|
||
|
$arVl = array();
|
||
|
$str = '';
|
||
|
$cort = 0;
|
||
|
for ($i = 1; $i <= $tam; $i++) {
|
||
|
$str .= substr($valor, -$i, 1);
|
||
|
if (( ++$cort === 3) || ($i === $tam)) {
|
||
|
$arVl[] = strrev($str);
|
||
|
$str = '';
|
||
|
$cort = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return array_reverse($arVl);
|
||
|
}
|
||
|
|
||
|
function VocalizaAdd($numero, $numAdd) {
|
||
|
return ((int) $numAdd != 100) ? $numAdd : (((((int) $numAdd === 100) && $numero > 100) ? 'cento' : 'cem'));
|
||
|
}
|
||
|
|
||
|
function VocalizaDef($valorI, $numero) {
|
||
|
return array_search($numero, $valorI) !== false;
|
||
|
}
|
||
|
|
||
|
function VocalizaConector($numeros) {
|
||
|
$arExt = array('real', 'centavo', 'reais', 'centavos', 'Mil', 'Milhao', 'Milhoes');
|
||
|
$numRet = array();
|
||
|
for ($i = 0; $i < count($numeros); $i++) {
|
||
|
$numRet[] = $numeros[$i];
|
||
|
|
||
|
if (isset($numeros[$i + 1]) && (array_search($numeros[$i + 1], $arExt) === false)) {
|
||
|
$numRet[] = 'e';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return $numRet;
|
||
|
}
|