1: <?php
2: 3:
4:
5: class Horde_Date_Parser_Locale_Pt extends Horde_Date_Parser_Locale_Base
6: {
7: public $definitions = array();
8: public $args = array();
9: public $now;
10:
11: public function __construct($args)
12: {
13: $this->args = $args;
14: }
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58:
59: public function parse($text, $specifiedOptions = array())
60: {
61:
62: $defaultOptions = array(
63: 'context' => 'future',
64: 'now' => new Horde_Date(time()),
65: 'return' => 'date',
66: 'ambiguousTimeRange' => 6,
67: );
68: $options = array_merge($defaultOptions, $this->args, $specifiedOptions);
69:
70:
71: foreach (array_keys($specifiedOptions) as $key) {
72: if (!isset($defaultOptions[$key])) {
73: throw new InvalidArgumentException("$key is not a valid option key");
74: }
75: }
76:
77: if (!in_array($options['context'], array('past', 'future', 'none'))) {
78: throw new InvalidArgumentException("Invalid value " . $options['context'] . " for 'context' specified. Valid values are 'past', 'future', and 'none'");
79: }
80:
81:
82: $this->now = $options['now'];
83:
84:
85: $text = $this->normalize_special_characters($text);
86:
87:
88: $text = $this->preNormalize($text);
89:
90:
91: $tokens = $this->preTokenize($text);
92:
93:
94: foreach (array('Repeater') as $tokenizer) {
95: $tokenizer = $this->componentFactory($tokenizer);
96: $tokens = $tokenizer->scan($tokens, $options);
97: }
98:
99: foreach (array('Grabber', 'Pointer', 'Scalar', 'Ordinal', 'Separator', 'Timezone') as $tokenizer) {
100: $tokenizer = $this->componentFactory($tokenizer);
101: $tokens = $tokenizer->scan($tokens);
102: }
103:
104:
105: $taggedTokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();')));
106:
107:
108:
109: $taggedTokens = $this->postTokenize($taggedTokens);
110:
111:
112: $span = $this->tokensToSpan($taggedTokens, $options);
113:
114:
115: $result = new Horde_Date_Parser_Result($span, $tokens);
116: switch ($options['return']) {
117: case 'result':
118: return $result;
119: case 'span':
120: return $result->span;
121: case 'date':
122: return $result->guess();
123: }
124: }
125:
126: 127: 128: 129:
130: public function normalize_special_characters( $str )
131: {
132:
133: $str = ereg_replace( chr(ord("`")), "'", $str );
134: $str = ereg_replace( chr(ord("´")), "'", $str );
135: $str = ereg_replace( chr(ord("„")), ",", $str );
136: $str = ereg_replace( chr(ord("`")), "'", $str );
137: $str = ereg_replace( chr(ord("´")), "'", $str );
138: $str = ereg_replace( chr(ord("“")), "\"", $str );
139: $str = ereg_replace( chr(ord("”")), "\"", $str );
140: $str = ereg_replace( chr(ord("´")), "'", $str );
141:
142: $unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
143: 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
144: 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
145: 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
146: 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
147: $str = strtr( $str, $unwanted_array );
148:
149:
150: $str = ereg_replace( chr(149), "•", $str );
151: $str = ereg_replace( chr(150), "–", $str );
152: $str = ereg_replace( chr(151), "—", $str );
153: $str = ereg_replace( chr(153), "™", $str );
154: $str = ereg_replace( chr(169), "©", $str );
155: $str = ereg_replace( chr(174), "®", $str );
156:
157: return $str;
158: }
159:
160:
161: 162: 163: 164: 165: 166:
167: public function preNormalize($text)
168: {
169:
170: $text = preg_replace('/\b([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})\b/', '', $text);
171:
172: $text = strtolower($text);
173: $text = $this->numericizeNumbers($text);
174:
175: $text = preg_replace('/(?:(?:https?|ftp):\/\/)/', '', $text);
176:
177:
178: $text = preg_replace('/\bsegunda[ \-]feira\b/', 'segunda', $text);
179: $text = preg_replace('/\bterca[ \-]feira\b/', 'terca', $text);
180: $text = preg_replace('/\bquarta[ \-]feira\b/', 'quarta', $text);
181: $text = preg_replace('/\bquinta[ \-]feira\b/', 'quinta', $text);
182: $text = preg_replace('/\bsexta[ \-]feira\b/', 'sexta', $text);
183:
184: $text = preg_replace('/[\'"\.]/', '', $text);
185: $text = preg_replace('/([\/\-\,\@])/', ' $1 ', $text);
186: $text = preg_replace('/\bhoje\b/', 'this day', $text);
187: $text = preg_replace('/\bamanh[aã]\b/', 'next day', $text);
188: $text = preg_replace('/\bontem\b/', 'last day', $text);
189: $text = preg_replace('/\bfim de semana\b/', 'fds', $text);
190: $text = preg_replace('/\bmeio\s+dia\b/', '12:00', $text);
191: $text = preg_replace('/\bmeia\s+noite\b/', '24:00', $text);
192: $text = preg_replace('/\b(antes|anterior)\b/', 'past', $text);
193: $text = preg_replace('/\b(agora|j[aá])\b/', 'this second', $text);
194: $text = preg_replace('/\b[uú]ltim[oa]\b/', 'last', $text);
195: $text = preg_replace('/\b(?:de|na|durante\s+a|logo(?:\s[aà]|de))\s+(manh[aã]|madrugada)\b/', 'morning', $text);
196: $text = preg_replace('/\b(?:de|[àa]|durante\s+a|logo(?:\s[aà]|de))\s+tarde\b/', 'afternoon', $text);
197: $text = preg_replace('/\b((?:de|[àa]|durante\s+a|logo(?:\s[aà]))\s+noite|(?:ao)\s+anoitecer)\b/', 'this night', $text);
198:
199: $text = preg_replace_callback(
200: '/\b([0-1]?[0-9]|2[0-3])(:|,|.)?([0-5][0-9])?\s?(horas?)\b/',
201: create_function(
202: '$matches',
203: '
204: $minute = ($matches[3]!="")? str_pad($matches[3], 2 , "0", STR_PAD_LEFT): "00";
205: $hour = $matches[1];
206: return $hour.":".$minute." oclock";
207: '
208: ),
209: $text);
210:
211: $text = preg_replace('/\b(horas?|h|hrs?)\b/', ' oclock', $text);
212:
213: $text = preg_replace('/\b(depois|ap[oó]s)\b/', 'future', $text);
214:
215: $text = preg_replace('/\bdia\b/', '', $text);
216:
217:
218:
219: return $text;
220: }
221:
222: 223: 224:
225: public function numericizeOrdinals($text)
226: {
227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248:
249: return $text;
250: }
251: public function initDefinitions()
252: {
253: if ($this->definitions) { return; }
254:
255: $this->definitions = array(
256: 'time' => array(
257:
258: new Horde_Date_Parser_Handler(array(':separator_at?', ':repeater_time', ':repeater_day_portion?'), null),
259: new Horde_Date_Parser_Handler(array(':separator_at?', ':time', ':repeater_time'), null),
260: 261: 262: 263: 264:
265: ),
266:
267: 'date' => array(
268: new Horde_Date_Parser_Handler(array(':repeater_day_name', ':repeater_month_name', ':scalar_day', ':repeater_time', ':timezone', ':scalar_year'), 'handle_rdn_rmn_sd_t_tz_sy'),
269: new Horde_Date_Parser_Handler(array(':repeater_month_name', ':scalar_day', ':scalar_year'), 'handle_rmn_sd_sy'),
270: new Horde_Date_Parser_Handler(array(':repeater_month_name', ':scalar_day', ':scalar_year', ':separator_at?', 'time?'), 'handle_rmn_sd_sy'),
271: new Horde_Date_Parser_Handler(array(':repeater_month_name', ':scalar_day', ':separator_at?', 'time?'), 'handle_rmn_sd'),
272: new Horde_Date_Parser_Handler(array(':repeater_month_name', ':ordinal_day', ':separator_at?', 'time?'), 'handle_rmn_od'),
273: new Horde_Date_Parser_Handler(array(':repeater_month_name', ':scalar_year'), 'handle_rmn_sy'),
274: new Horde_Date_Parser_Handler(array(':scalar_day', ':repeater_month_name', ':scalar_year', ':separator_at?', 'time?'), 'handle_sd_rmn_sy'),
275: new Horde_Date_Parser_Handler(array(':scalar_month', ':separator_slash_or_dash', ':scalar_day', ':separator_slash_or_dash', ':scalar_year', ':separator_at?', 'time?'), 'handle_sm_sd_sy'),
276: new Horde_Date_Parser_Handler(array(':scalar_day', ':separator_slash_or_dash', ':scalar_month', ':separator_slash_or_dash', ':scalar_year', ':separator_at?', 'time?'), 'handle_sd_sm_sy'),
277: new Horde_Date_Parser_Handler(array(':scalar_year', ':separator_slash_or_dash', ':scalar_month', ':separator_slash_or_dash', ':scalar_day', ':separator_at?', 'time?'), 'handle_sy_sm_sd'),
278: new Horde_Date_Parser_Handler(array(':scalar_month', ':separator_slash_or_dash', ':scalar_year'), 'handle_sm_sy'),
279: new Horde_Date_Parser_Handler(array(':scalar_day', ':separator_at?', ':repeater_month_name', ':separator_at?', ':scalar_year', ':separator_at?', 'time?'), 'handle_sd_rmn_sy'),
280: 281: 282: 283: 284: 285: 286:
287: new Horde_Date_Parser_Handler(array(':scalar_day', ':separator_slash_or_dash', ':scalar_month', ':separator_slash_or_dash', ':scalar_year', ':separator_at?', 'time?'), 'handle_sd_sm_sy'),
288: new Horde_Date_Parser_Handler(array(':scalar_year', ':separator_slash_or_dash', ':scalar_month', ':separator_slash_or_dash', ':scalar_day', ':separator_at?', 'time?'), 'handle_sy_sm_sd'),
289: new Horde_Date_Parser_Handler(array(':scalar_month', ':separator_slash_or_dash', ':scalar_year'), 'handle_sm_sy'),
290: ),
291:
292:
293: 'anchor' => array(
294: new Horde_Date_Parser_Handler(array(':grabber?', ':repeater', ':separator_at?', ':repeater?', ':repeater?'), 'handle_r'),
295: new Horde_Date_Parser_Handler(array(':grabber?', ':repeater', ':repeater', ':separator_at?', ':repeater?', ':repeater?'), 'handle_r'),
296: new Horde_Date_Parser_Handler(array(':repeater', ':grabber', ':repeater'), 'handle_r_g_r'),
297: ),
298:
299:
300: 'arrow' => array(
301: new Horde_Date_Parser_Handler(array(':scalar', ':repeater', ':pointer'), 'handle_s_r_p'),
302: new Horde_Date_Parser_Handler(array(':pointer', ':scalar', ':repeater'), 'handle_p_s_r'),
303: new Horde_Date_Parser_Handler(array(':scalar', ':repeater', ':pointer', 'anchor'), 'handle_s_r_p_a'),
304: ),
305:
306:
307: 'narrow' => array(
308: new Horde_Date_Parser_Handler(array(':ordinal', ':repeater', ':separator_in', ':repeater'), 'handle_o_r_s_r'),
309: new Horde_Date_Parser_Handler(array(':ordinal', ':repeater', ':grabber', ':repeater'), 'handle_o_r_g_r'),
310: ),
311: );
312: }
313:
314: public function handle_rdn($tokens, $options)
315: {
316: $day = $tokens[0]->getTag('repeater_day_name');
317:
318: try {
319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336:
337: $dayStart = new Horde_Date(time());
338: return $this->dayOrTime($dayStart, array($tokens[0]), $options);
339: } catch (Exception $e) {
340: return null;
341: }
342: }
343:
344:
345: public function handle_sd_rmn($tokens, $options)
346: {
347: return $this->handle_m_d($tokens[1]->getTag('repeater_month_name'), $tokens[0]->getTag('scalar_day'), array_slice($tokens, 2), $options);
348: }
349:
350: public function handle_od_rmn($tokens, $options)
351: {
352: return $this->handle_m_d($tokens[1]->getTag('repeater_month_name'), $tokens[0]->getTag('ordinal_day'), array_slice($tokens, 2), $options);
353: }
354:
355:
356: }
357: