1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10: abstract class NodeAddeded {
11:
12: 13: 14: 15:
16: private $node;
17:
18: 19: 20: 21:
22: private $target;
23:
24: 25: 26: 27:
28: private $newNode;
29:
30: 31: 32: 33:
34: private $newTarget;
35:
36: 37: 38:
39: private $parser;
40:
41: 42: 43: 44: 45:
46: final public function __construct(Node $node, Node $target, Parser $parser) {
47: $this->node = $node;
48: $this->target = $target;
49: $this->newNode = $node;
50: $this->parser = $parser;
51: $this->newTarget = $target;
52: }
53:
54: 55: 56: 57: 58: 59:
60: public static function init(Node $node, Node $target, Parser $parser) {
61: $addeded = NULL;
62: try {
63: $ref = new ReflectionClass(get_class($node).'NodeAddeded');
64: $addeded = $ref->newInstance($node, $target, $parser);
65: } catch(ReflectionException $exp) {
66: $addeded = new DefNodeAddeded($node, $target, $parser);
67: }
68: $addeded->tryAdd();
69: return $addeded;
70: }
71:
72: 73: 74:
75: final public function getParser() {
76: return $this->parser;
77: }
78:
79: 80: 81: 82:
83: final public function getNode() {
84: return $this->node;
85: }
86:
87: 88: 89: 90:
91: final public function getTarget() {
92: return $this->target;
93: }
94:
95: 96: 97: 98:
99: final public function getNewNode() {
100: return $this->newNode;
101: }
102:
103: 104: 105: 106:
107: final public function setNewNode($newNode) {
108: $this->newNode = $newNode;
109: }
110:
111: 112: 113: 114:
115: final public function getNewTarget() {
116: return $this->newTarget;
117: }
118:
119: 120: 121: 122:
123: final public function setNewTarget($newTarget) {
124: $this->newTarget = $newTarget;
125: }
126:
127: 128: 129: 130: 131:
132: final public function tryAdd() {
133: $target = $this->target;
134: do{
135: if($this->addNode($this->getNode(), $target)) {
136: return;
137: }
138: } while(!is_null($target = $target->getParent()));
139: throw new Exception('The node "'.get_class($this->getNode()).'" can not be added to "'.get_class($this->target).'"');
140: }
141:
142: 143: 144:
145: abstract protected function addNode($node, $target);
146:
147: 148: 149: 150: 151:
152: protected function initNode($name) {
153: return $this->parser->initNode($name);
154: }
155: }