Example #1
0
 private function buildTree($in, DocumentBuilder $doc)
 {
     $pattern = '$(\\[(?:/[a-z]*|[a-z]+[^\\]]*)\\])(\\n){0,1}$i';
     // TOKENIZE input stream
     $tokens = preg_split($pattern, $in, -1, PREG_SPLIT_DELIM_CAPTURE);
     for ($i = 0; $i < count($tokens); $i++) {
         $token = $tokens[$i];
         /*
          * If token is an open tag create a TAG object
          * - Append tag to parent
          * - Set node to take ownership of children
          */
         if ($this->isOpenToken($token) && ($attributes = $this->parseAttributes($token)) !== null) {
             $tag = $this->getTagFromToken($token);
             $doc->writeStartElement($tag, $token);
             foreach ($attributes as $key => $value) {
                 $doc->writeAttribute($key, $value);
             }
             if (array_key_exists($tag, $this->selfClosing)) {
                 $doc->writeEndElement();
             }
         } elseif ($this->isCloseToken($token)) {
             /*
              * If close tag matches open tag remove it from the stack
              * Set node to parent
              */
             $tagName = $this->getTagFromToken($token);
             if (!$doc->writeEndElement($tagName, $token)) {
                 $doc->writeText($token);
             }
         } else {
             $doc->writeText($token);
         }
     }
     return $doc->getDOMDocument();
 }