1: <?php
2:
3:
4:
5: 6: 7: 8: 9: 10: 11:
12: abstract class Parser {
13: 14: 15:
16: protected $handlersInitNode = array();
17:
18: 19: 20: 21:
22: public function addHandlerInitNode(IInitNode $handler) {
23: array_unshift($this->handlersInitNode, $handler);
24: }
25:
26: 27: 28: 29: 30:
31: public function initNode($tagName, $attributes = NULL) {
32: if(sizeof($this->handlersInitNode)) {
33: $attributes = is_array($attributes) ? $attributes : $this->attributeStrToArray($attributes);
34: foreach($this->handlersInitNode as $handler) {
35: if(!is_null($node = $handler->initNode($tagName, $attributes))) {
36: return $node;
37: }
38: }
39: }
40: return new EmptyCompositeNode();
41: }
42:
43: 44: 45: 46:
47: protected function attributeStrToArray($attributeStr) {
48: $attr = array();
49: $attributeStr = trim($attributeStr);
50: $l = strlen($attributeStr);
51: $key = '';
52: $value = '';
53: $state = 0;
54: for($i = 0; $i < $l; $i ++) {
55: $char = substr($attributeStr, $i, 1);
56: if($state == 0 && $char == '=') {
57: $state = 1;
58: } else if($state == 1 && $char == '"') {
59: $state = 2;
60: } else if($state == 1 && $char == '\'') {
61: $state = 3;
62: } else if(($state == 3 && $char == '\'') || ($state == 2 && $char == '"')) {
63: $attr[trim($key)] = $value;
64: $key = '';
65: $value = '';
66: $state = 0;
67: } else if($state == 2 || $state == 3) {
68: $value .= $char;
69: } else if($state == 0) {
70: $key .= $char;
71: }
72: }
73: if($state != 0) {
74: throw new Exception('Attribute syntax error');
75: }
76: return $attr;
77: }
78: }