1: <?php
2: 3: 4: 5: 6: 7: 8:
9:
10: 11: 12: 13: 14:
15: class Horde_Support_Numerizer_Locale_Pt extends Horde_Support_Numerizer_Locale_Base
16: {
17: public $DIRECT_NUMS = array(
18: 'treze' => '13',
19: 'catorze' => '14',
20: 'quatorze' => '14',
21: 'quinze' => '15',
22: 'dezasseis' => '16',
23: 'dezassete' => '17',
24: 'dezoito' => '18',
25: 'dezanove' => '19',
26: 'um(\W|$)' => '1$1',
27: 'uma(\W|$)' => '1$1',
28: 'dois' => '2',
29: 'duas' => '2',
30: 'tres' => '3',
31: 'quatro' => '4',
32: 'cinco' => '5',
33: 'seis' => '6',
34: 'sete' => '7',
35: 'oito' => '8',
36: 'nove' => '9',
37: 'dez' => '10',
38: 'onze' => '11',
39: 'doze' => '12',
40: );
41:
42: public $TEN_PREFIXES = array(
43: 'vinte' => '20',
44: 'trinta' => '30',
45: 'quarenta' => '40',
46: 'cinquenta' => '50',
47: 'sessenta' => '60',
48: 'setenta' => '70',
49: 'oitenta' => '80',
50: 'noventa' => '90',
51: );
52:
53: public $BIG_PREFIXES = array(
54: 'cem' => '100',
55: 'mil' => '1000',
56: 'milhao *' => '1000000',
57: 'milhar de *' => '1000000000',
58: 'biliao *' => '1000000000000',
59: );
60:
61: public function numerize($string)
62: {
63:
64: $string = $this->_splitHyphenateWords($string);
65: $string = $this->_replaceTenPrefixes($string);
66: $string = $this->_directReplacements($string);
67: $string = $this->_replaceBigPrefixes($string);
68:
69:
70: return $string;
71: }
72:
73:
74: 75: 76:
77: protected function _splitHyphenateWords($string)
78: {
79: return preg_replace('/ +|([^\d]) e? ([^d])/', '$1 $2', $string);
80: }
81:
82: 83: 84:
85: protected function _directReplacements($string)
86: {
87: foreach ($this->DIRECT_NUMS as $dn => $dn_replacement) {
88: $string = preg_replace("/$dn/i", $dn_replacement, $string);
89: }
90: return $string;
91: }
92:
93: 94: 95:
96: protected function _replaceTenPrefixes($string)
97: {
98: foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
99: $string = preg_replace_callback(
100: "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
101: create_function(
102: '$m',
103: 'return ' . $tp_replacement . ' + (isset($m[1]) ? (int)$m[1] : 0);'
104: ),
105: $string);
106: }
107: return $string;
108: }
109:
110: 111: 112:
113: protected function _replaceBigPrefixes($string)
114: {
115: foreach ($this->BIG_PREFIXES as $bp => $bp_replacement) {
116: $string = preg_replace_callback(
117: '/(\d*) *' . $bp . '(\d?)/i',
118: create_function(
119: '$m',
120: '$factor = (int)$m[1]; if (!$factor) $factor = 1; return (' . $bp_replacement . ' * $factor)' . ($bp_replacement == 100 ? ' . ($m[2] ? "e" : "")' : ' . "e"') . ' . $m[2];'
121: ),
122: $string);
123: $string = $this->_andition($string);
124: }
125: return $string;
126: }
127:
128: protected function _andition($string)
129: {
130: while (preg_match('/(\d+)((?: *e *)+)(\d*)(?=\w|$)/i', $string, $sc, PREG_OFFSET_CAPTURE)) {
131: $string = substr($string, 0, $sc[1][1]) . ((int)$sc[1][0] + (int)$sc[3][0]) . substr($string, $sc[3][1] + strlen($sc[3][0]));
132: }
133: return $string;
134: }
135:
136: protected function _fractionalAddition($string)
137: {
138: return preg_replace_callback(
139: '/(\d+)(?: | e |-)*/i',
140: create_function(
141: '$m',
142: 'return (string)((float)$m[1] + 0.5);'
143: ),
144: $string);
145: }
146:
147: }
148: