public function addChild($child) { if (!$this->allowChildren) { throw ExceptionFactory::createChildrenNotAllowed(__FILE__, __LINE__, $this->getId()); } $this->children[] = $child; }
public function parse() { $token = null; while (($token = $this->scanner->nextToken()) != false) { switch ($token->getType()) { case Token::T_REGISTER: // T_REGISTER has to be parsed differently of the other tokens $this->componentBuilder->setOpenTag($token); $register = null; while (($register = $this->scanner->nextToken()) !== false) { switch ($register->getType()) { case Token::T_ATTRIBUTE: $this->componentBuilder->addAttr($register); break; case Token::T_VALUE: $this->componentBuilder->addValue($register); break; case Token::T_CLOSE: $this->componentBuilder->build()->registerNS(); break 3; default: throw ExceptionFactory::createNoChildsException(__FILE__, __LINE__, $this->scanner->getFile()->getFileName(), $this->scanner->getFile()->getCurrentLine(), $token->getName(), $token->getNamespace()); } } break; case Token::T_OPEN_TAG: // Push onto the stack for aftermost comparison $this->stack->push($token); // Start building the component $this->componentBuilder->setOpenTag($token); break; case Token::T_CLOSE_TAG: case Token::T_CLOSE: // Match the current token with the previous T_OPEN_TAG if ($this->matchTokens($this->stack->top(), $token)) { $this->stack->pop(); } else { throw ExceptionFactory::createUnexpectedToken(__FILE__, __LINE__, $this->scanner->getFile()->getFileName(), $this->scanner->getFile()->getCurrentLine(), $token); } // Build the component and add into the tree // T_CLOSE_TAG doesn't need to be pushed into the tree nor built if ($token->getType() == Token::T_CLOSE) { // It's not a child, so put it into the Tree if (count($this->stack) == 0) { $this->tree->addNoChild($this->componentBuilder->build()); } else { $this->tree->addNoChild($this->componentBuilder->build(), $this->tree->top()); } // We removed the token from the stack, so we have to set the new top component } else { $this->tree->setTop($this->tree->top()->getParent()); } break; case Token::T_END: // Build the component and add into the tree if (count($this->stack) == 1) { $this->tree->add($this->componentBuilder->build()); } else { $this->tree->add($this->componentBuilder->build(), $this->tree->top()); } // This components does not allow children if (!$this->tree->top()->isChildrenAllowed()) { throw ExceptionFactory::createChildrenNotAllowed(__FILE__, __LINE__, $this->tree->top()->getId()); } break; case Token::T_ATTRIBUTE: // Add the T_ATTRIBUTE into the ComponentBuilder $this->componentBuilder->addAttr($token); break; case Token::T_VALUE: // Add the T_VALUE into the ComponentBuilder $this->componentBuilder->addValue($token); break; case Token::T_TEXT: // Add the value of T_TEXT into the Tree if ($this->stack->isEmpty()) { $this->tree->addText($token); } else { $this->tree->addText($token, $this->tree->top()); } break; } } // Check if there is no tokens left into the stack $this->verifyStack(); return $this->tree; }