Overview

Namespaces

  • MOC
    • Adapter
    • Core
      • Changelog
      • Depending
      • Drive
        • Directory
        • File
      • Error
        • Register
        • Type
      • Journal
      • Proxy
      • Template
      • Xml
    • Extension
      • Excel
      • Flot
      • Mail
      • Pdf
      • Word
      • Xml
      • YUICompressor
      • Zip
    • Generic
      • Common
      • Device
        • Extension
        • Widget
    • Module
      • Database
        • Driver
      • Drive
      • Image
        • Font
      • Installer
      • Network
        • Ftp
          • Directory
          • File
          • Transport
        • Http
        • ParcelTracker
          • Carrier
      • Office
        • Chart
          • Axis
        • Document
          • Excel
            • Cell
              • Format
              • Style
                • Border
                  • Bottom
                  • Left
                  • Right
                  • Top
                • Font
            • Close
            • Page
            • Worksheet
          • Pdf
            • Close
            • Font
            • Page
              • Margin
              • Position
            • Text
          • Xml
            • Close
        • Image
        • Mail
          • Address
          • Content
      • Packer
        • Yui
        • Zip
      • Template
    • Plugin
      • Gateway
      • Repository
      • Shared
  • PHP

Classes

  • Node
  • Parser
  • Token
  • Tokenizer
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * LICENSE (BSD)
  4:  *
  5:  * Copyright (c) 2013, Gerd Christian Kunze
  6:  * All rights reserved.
  7:  *
  8:  * Redistribution and use in source and binary forms, with or without
  9:  * modification, are permitted provided that the following conditions are
 10:  * met:
 11:  *
 12:  *  * Redistributions of source code must retain the above copyright
 13:  *    notice, this list of conditions and the following disclaimer.
 14:  *
 15:  *  * Redistributions in binary form must reproduce the above copyright
 16:  *    notice, this list of conditions and the following disclaimer in the
 17:  *    documentation and/or other materials provided with the distribution.
 18:  *
 19:  *  * Neither the name of Gerd Christian Kunze nor the names of the
 20:  *    contributors may be used to endorse or promote products derived from
 21:  *    this software without specific prior written permission.
 22:  *
 23:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 24:  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 25:  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 26:  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 27:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 28:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 29:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 30:  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 31:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 32:  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 33:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 34:  *
 35:  * Node
 36:  * 06.01.2013 15:09
 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:      * Get Dependencies
 48:      *
 49:      * @static
 50:      * @return \MOC\Core\Depending
 51:      */
 52:     public static function InterfaceDepending() {
 53:         return Api::Core()->Depending();
 54:     }
 55: 
 56:     /**
 57:      * Get Singleton/Instance
 58:      *
 59:      * @static
 60:      * @return Node
 61:      */
 62:     public static function InterfaceInstance() {
 63:         return new Node();
 64:     }
 65: 
 66:     /**
 67:      * Get Changelog
 68:      *
 69:      * @static
 70:      * @return \MOC\Core\Changelog
 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 TYPE_COMMENT = 4;
 80: 
 81:     private $Type = self::TYPE_CONTENT;
 82:     /** @var null|int $Position */
 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:      * @param $NameOrToken
 93:      *
 94:      * @return Node
 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:      * @param $Value
111:      *
112:      * @return Node
113:      */
114:     public function SetType( $Value ) {
115:         $this->Type = $Value;
116:         return $this;
117:     }
118: 
119:     /**
120:      * @return int
121:      */
122:     public function GetType() {
123:         return $this->Type;
124:     }
125: 
126:     /**
127:      * @param $Value
128:      *
129:      * @return Node
130:      */
131:     public function SetPosition( $Value ) {
132:         $this->Position = $Value;
133:         return $this;
134:     }
135: 
136:     /**
137:      * @return int|null
138:      */
139:     public function GetPosition() {
140:         return $this->Position;
141:     }
142: 
143:     /**
144:      * @param Node $Value
145:      *
146:      * @return Node
147:      */
148:     public function SetParent( Node $Value ) {
149:         $this->Parent = $Value;
150:         return $this;
151:     }
152: 
153:     /**
154:      * @return null
155:      */
156:     public function GetParent() {
157:         return $this->Parent;
158:     }
159: 
160:     /**
161:      * @param Node $Value
162:      *
163:      * @return Node
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:      * @param      $Name
175:      * @param null $AttributeList
176:      * @param null $Index
177:      * @param bool $Recursive
178:      *
179:      * @return bool|Node|object
180:      */
181:     public function GetChild( $Name, $AttributeList = null, $Index = null, $Recursive = true ) {
182:         /** @var Node $Node */
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:      * @return array
224:      */
225:     public function GetChildList() {
226:         return $this->ChildList;
227:     }
228: 
229:     /**
230:      * @param $Value
231:      *
232:      * @return Node
233:      */
234:     public function SetName( $Value ) {
235:         $this->Name = $Value;
236:         return $this;
237:     }
238: 
239:     /**
240:      * @return null
241:      */
242:     public function GetName() {
243:         return $this->Name;
244:     }
245: 
246:     /**
247:      * @param null $Value
248:      *
249:      * @return Node
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:      * @return null
268:      */
269:     public function GetContent() {
270:         return $this->Content;
271:     }
272: 
273:     /**
274:      * @param      $Name
275:      * @param null $Value
276:      *
277:      * @return Node
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:      * @param $Name
290:      *
291:      * @return null
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:      * @param $Array
303:      */
304:     public function SetAttributeList( $Array ) {
305:         $this->AttributeList = $Array;
306:     }
307: 
308:     /**
309:      * @return array
310:      */
311:     public function GetAttributeList() {
312:         return $this->AttributeList;
313:     }
314: 
315:     /**
316:      * @return string
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:      * @return string
326:      */
327:     public function Code() {
328:         $FuncArgs = func_get_args();
329:         if( empty( $FuncArgs) ) {
330:             $FuncArgs[0] = false;
331:             $FuncArgs[1] = 0;
332:         }
333:         // BUILD STRUCTURE STRING
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:             /** @var Node $Node */
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:         // RETURN STRUCTURE
360:         return $Result;
361:     }
362: 
363:     public function __destruct() {
364:         /** @var Node $Node */
365:         unset( $this->Parent );
366:         foreach( (array)$this->ChildList as $Node ) {
367:             $Node->__destruct();
368:         }
369:         unset( $this );
370:     }
371: }
372: 
API documentation generated by ApiGen 2.8.0