1: <?php
2:
3: namespace PHPixie\Database\Conditions\Logic;
4:
5: abstract class Parser
6: {
7: protected $logicPrecedance = array(
8: 'and' => 2,
9: 'xor' => 1,
10: 'or' => 0
11: );
12:
13: protected function parseLogicLevel($iterator, $level = 0)
14: {
15: $current = $iterator->current();
16: $res = $this->normalize($current);
17:
18: while (true) {
19: if ($iterator->offsetExists($iterator->key()+1)) {
20: $iterator->seek($iterator->key()+1);
21: $next = $iterator->current();
22: } else {
23: break;
24: }
25:
26: if ($this->logicPrecedance[$next->logic()] < $level) {
27: $iterator->seek($iterator->key()-1);
28: break;
29: }
30:
31: $right = $this->parseLogicLevel($iterator, $this->logicPrecedance[$next->logic()] + 1);
32:
33: if ($right !== null)
34: $res = $this->merge($res, $right);
35:
36: }
37:
38: return $res;
39: }
40:
41: protected function parseLogic($group)
42: {
43: if (empty($group))
44: return null;
45:
46: $iterator = new \ArrayIterator($group);
47:
48: return $this->parseLogicLevel($iterator);
49: }
50:
51: abstract protected function normalize($condition);
52: abstract protected function merge($left, $right);
53:
54: }
55: