1: <?php
2: class Horde_Date_Parser_Handler
3: {
4: public $pattern;
5: public $handlerMethod;
6:
7: public function __construct($pattern, $handlerMethod)
8: {
9: $this->pattern = $pattern;
10: $this->handlerMethod = $handlerMethod;
11: }
12:
13: public function match($tokens, $definitions)
14: {
15: $tokenIndex = 0;
16: foreach ($this->pattern as $name) {
17: $optional = substr($name, -1) == '?';
18: if ($optional) { $name = rtrim($name, '?'); }
19:
20: $tag = substr($name, 0, 1) == ':';
21: if ($tag) {
22: $name = substr($name, 1);
23:
24: $match = isset($tokens[$tokenIndex]) && $tokens[$tokenIndex]->getTag($name);
25: if (!$match && !$optional) { return false; }
26: if ($match) { $tokenIndex++; continue; }
27: if (!$match && $optional) { continue; }
28: } else {
29: if ($optional && $tokenIndex == count($tokens)) { return true; }
30: if (!isset($definitions[$name])) {
31: throw new Horde_Date_Parser_Exception("Invalid subset $name specified");
32: }
33: $subHandlers = $definitions[$name];
34: foreach ($subHandlers as $subHandler) {
35: if ($subHandler->match(array_slice($tokens, $tokenIndex), $definitions)) {
36: return true;
37: }
38: }
39: return false;
40: }
41: }
42:
43: if ($tokenIndex != count($tokens)) { return false; }
44: return true;
45: }
46:
47: }
48: