1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 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: namespace MOC\Core\Xml;
39: use MOC\Api;
40: use MOC\Generic\Device\Core;
41:
42: 43: 44:
45: class Node implements Core {
46: 47: 48: 49: 50: 51:
52: public static function InterfaceDepending() {
53: return Api::Core()->Depending();
54: }
55:
56: 57: 58: 59: 60: 61:
62: public static function InterfaceInstance() {
63: return new Node();
64: }
65:
66: 67: 68: 69: 70: 71:
72: public static function InterfaceChangelog() {
73: return Api::Core()->Changelog()->Create( __CLASS__ );
74: }
75:
76: const TYPE_STRUCTURE = 1;
77: const TYPE_CONTENT = 2;
78: const TYPE_CDATA = 3;
79: const = 4;
80:
81: private $Type = self::TYPE_CONTENT;
82:
83: private $Position = null;
84: private $Parent = null;
85: private $ChildList = array();
86:
87: private $Name = null;
88: private $Content = null;
89: private $AttributeList = array();
90:
91: 92: 93: 94: 95:
96: public function Setup( $NameOrToken ) {
97: if( is_object( $NameOrToken ) && $NameOrToken instanceof Token ) {
98: $this->SetName( $NameOrToken->GetName() );
99: $this->SetAttributeList( $NameOrToken->GetAttributeList() );
100: $this->SetPosition( $NameOrToken->GetPosition() );
101: unset( $NameOrToken );
102: } else {
103: $this->SetName( $NameOrToken );
104: $this->SetType( self::TYPE_STRUCTURE );
105: }
106: return $this;
107: }
108:
109: 110: 111: 112: 113:
114: public function SetType( $Value ) {
115: $this->Type = $Value;
116: return $this;
117: }
118:
119: 120: 121:
122: public function GetType() {
123: return $this->Type;
124: }
125:
126: 127: 128: 129: 130:
131: public function SetPosition( $Value ) {
132: $this->Position = $Value;
133: return $this;
134: }
135:
136: 137: 138:
139: public function GetPosition() {
140: return $this->Position;
141: }
142:
143: 144: 145: 146: 147:
148: public function SetParent( Node $Value ) {
149: $this->Parent = $Value;
150: return $this;
151: }
152:
153: 154: 155:
156: public function GetParent() {
157: return $this->Parent;
158: }
159:
160: 161: 162: 163: 164:
165: public function AddChild( Node $Value ) {
166: $Value->SetParent( $this );
167: array_push( $this->ChildList, $Value );
168: $this->Content = null;
169: $this->SetType( self::TYPE_STRUCTURE );
170: return $this;
171: }
172:
173: 174: 175: 176: 177: 178: 179: 180:
181: public function GetChild( $Name, $AttributeList = null, $Index = null, $Recursive = true ) {
182:
183: foreach( $this->ChildList as $Node ) {
184: if( $Node->GetName() == $Name ) {
185: if( $AttributeList === null && $Index === null ) {
186: return $Node;
187: } else if( $Index === null ) {
188: if( $Node->GetAttributeList() == $AttributeList ) {
189: return $Node;
190: }
191: } else if( $AttributeList === null ) {
192: if( $Index === 0 ) {
193: return $Node;
194: } else {
195: $Index--;
196: }
197: } else {
198: if( $Node->GetAttributeList() == $AttributeList && $Index === 0 ) {
199: return $Node;
200: } else if( $Node->GetAttributeList() == $AttributeList ) {
201: $Index--;
202: }
203: }
204: }
205: if( true === $Recursive && !empty( $Node->ChildList ) ) {
206: if( false !== ( $Result = $Node->GetChild( $Name, $AttributeList, $Index, $Recursive ) ) ) {
207: if( !is_object( $Result ) ) {
208: $Index = $Result;
209: } else {
210: return $Result;
211: }
212: }
213: }
214: }
215: if( $Index !== null ) {
216: return $Index;
217: } else {
218: return false;
219: }
220: }
221:
222: 223: 224:
225: public function GetChildList() {
226: return $this->ChildList;
227: }
228:
229: 230: 231: 232: 233:
234: public function SetName( $Value ) {
235: $this->Name = $Value;
236: return $this;
237: }
238:
239: 240: 241:
242: public function GetName() {
243: return $this->Name;
244: }
245:
246: 247: 248: 249: 250:
251: public function SetContent( $Value = null ) {
252: if( preg_match( '![<>&]!is', $Value ) ) {
253: $this->SetType( self::TYPE_CDATA );
254: } else {
255: $this->SetType( self::TYPE_CONTENT );
256: }
257: if( strlen( $Value ) == 0 ) {
258: $this->Content = null;
259: } else {
260: $this->Content = $Value;
261: }
262: $this->ChildList = array();
263: return $this;
264: }
265:
266: 267: 268:
269: public function GetContent() {
270: return $this->Content;
271: }
272:
273: 274: 275: 276: 277: 278:
279: public function SetAttribute( $Name, $Value = null ) {
280: if( $Value === null ) {
281: unset( $this->AttributeList[$Name] );
282: } else {
283: $this->AttributeList[$Name] = $Value;
284: }
285: return $this;
286: }
287:
288: 289: 290: 291: 292:
293: public function GetAttribute( $Name ) {
294: if( isset( $this->AttributeList[$Name] ) ) {
295: return $this->AttributeList[$Name];
296: } else {
297: return null;
298: }
299: }
300:
301: 302: 303:
304: public function SetAttributeList( $Array ) {
305: $this->AttributeList = $Array;
306: }
307:
308: 309: 310:
311: public function GetAttributeList() {
312: return $this->AttributeList;
313: }
314:
315: 316: 317:
318: public function GetAttributeString() {
319: $AttributeList = $this->AttributeList;
320: array_walk( $AttributeList, create_function( '&$Value,$Key', '$Value = $Key.\'="\'.$Value.\'"\';' ) );
321: return implode(' ',$AttributeList);
322: }
323:
324: 325: 326:
327: public function Code() {
328: $FuncArgs = func_get_args();
329: if( empty( $FuncArgs) ) {
330: $FuncArgs[0] = false;
331: $FuncArgs[1] = 0;
332: }
333:
334: $Result = ''
335: .( !$FuncArgs[0]?'<?xml version="1.0" encoding="utf-8" standalone="yes"?>'."\n":"\n" )
336: .str_repeat( "\t", $FuncArgs[1] );
337: if( $this->GetType() == self::TYPE_COMMENT ) {
338: $Result .= '<!-- '.$this->GetContent().' //-->';
339: } else {
340: $Result .= '<'.trim($this->GetName().' '.$this->GetAttributeString());
341: }
342: if( $this->GetContent() === null && empty( $this->ChildList ) ) {
343: $Result .= ' />';
344: }
345: else if( $this->GetType() == self::TYPE_CONTENT ) {
346: $Result .= '>'.$this->GetContent().'</'.$this->GetName().'>';
347: }
348: else if( $this->GetType() == self::TYPE_CDATA ) {
349: $Result .= '><![CDATA['.$this->GetContent().']]></'.$this->GetName().'>';
350: }
351: else if( $this->GetType() == self::TYPE_STRUCTURE ) {
352: $Result .= '>';
353:
354: foreach( $this->ChildList as $Node ) {
355: $Result .= $Node->Code(true, $FuncArgs[1] + 1 );
356: }
357: $Result .= "\n".str_repeat( "\t", $FuncArgs[1] ).'</'.$this->GetName().'>';
358: }
359:
360: return $Result;
361: }
362:
363: public function __destruct() {
364:
365: unset( $this->Parent );
366: foreach( (array)$this->ChildList as $Node ) {
367: $Node->__destruct();
368: }
369: unset( $this );
370: }
371: }
372: