NodeClass.php 4.1 KB
<?php

/**
 * @class NodeClass
 * @brief Generic definition of a node
 * @details
 */
class NodeClass
{
	private $name;
	private $value;
	private $attributes;
	private $children;

	public function __construct($name)
	{
		$this->name = $name;
		$this->attributes = array();
		$this->children = array();
		$this->value = "";
	}

	public function getName()
	{
		return $this->name;
	}

	protected function setValue($val)
	{
		$this->value = $val;
	}
	
	protected function getValue()
	{
		return $this->value;
	}

	protected function setAttribute($attName, $attVal)
	{
		$this->attributes[$attName] = $attVal;
	}

	public function getAttribute($attName)
	{
		if (array_key_exists($attName,$this->attributes)) {
			return $this->attributes[$attName];
		}
		return "";
	}

	protected function hasChildren()
	{
		return (count($this->children) != 0);
	}

	protected function addChild($child)
	{
		$this->children[] = $child;
	}
	
	protected function addChildBefore($child, $nodeName)
	{
		for ($i = 0; $i < count($this->children);++$i)
		{
			if ($this->children[$i]->getName() == $nodeName)
			{
				array_splice($this->children, $i, 0, array($child));
				return;
			}
		}
		
		$this->addChild($child);
	}

	public function getChildren()
	{
		return $this->children;
	}

	protected function getChildrenByName($name)
	{
		$result = array();

		foreach ($this->children as $child)
		if ($child->getName() == $name)
			$result[] = $child;

		return $result;
	}

	protected function getFirstChildByName($name)
	{
		foreach ($this->children as $child)
		if ($child->getName() == $name)
			return $child;

		return NULL;
	}

	public function getChildInstanceByName($name, $createIfNoExist = false)
	{
		$node = $this->getFirstChildByName($name);
		if (!isset($node) && $createIfNoExist)
		{
			$node = new NodeClass($name);
			$this->addChild($node);
		}
		return $node;
	}

	/*
	 * @brief Export node to a XML node
	*/
	public function toXMLNode($doc)
	{
		$xmlNode = $doc->createElement($this->getName());

		if ($this->getValue() != "")
			$xmlNode->nodeValue = htmlspecialchars($this->getValue());

		foreach ($this->attributes as $key => $value)
			$xmlNode->setAttribute($key,$value);

		foreach ($this->children as $child)
		{
			$xmlChildNode = $child->toXMLNode($doc);
			$xmlNode->appendChild($xmlChildNode);
		}

		return $xmlNode;
	}
	
	/*
	 * @brief Method to load a XML node
	 */
	public function getXmlNodeAttribute($xmlNode, $attribute)
	{
		return $xmlNode->getAttribute($attribute);
	}
	
	public function getXmlNodeValue($xmlNode)
	{
		return $xmlNode->nodeValue;
	}
	
	public function getXmlNodeName($xmlNode)
	{
		return $xmlNode->nodeName;
	}
	
	public function getXmlNodeChildren($xmlNode)
	{
		return $xmlNode->childNodes;
	}
	
	public function getXmlNodeChildrenByTagName($xmlNode, $childrenName)
	{
		$result = array();
		for ($n = $xmlNode->firstChild; $n !== null; $n = $n->nextSibling) {
			if ($n instanceof DOMElement && $n->tagName == $childrenName) {
				$result[] = $n;
			}
		}
		return $result;
	}
	
	public function getXmlNodeChildByTagName($xmlNode, $childName)
	{
		$xmlChildrenNodes = $this->getXmlNodeChildrenByTagName($xmlNode, $childName);
		if (count($xmlChildrenNodes) <= 0)
			return NULL;
		return $xmlChildrenNodes[0];
	}
	
	public function loadFromNode($xmlNode) {
		/*//Attributes
		
		//Value
		if ($this->getXmlNodeChildren($xmlNode) == NULL) {
			if ($this->getXmlNodeValue($xmlNode)) {
				$result[$childXmlNode->nodeName] = $this->getXmlNodeValue($xmlNode);
			}
			return;
		}
		//Children
		foreach ($this->getXmlNodeChildren($xmlNode) as $childXmlNode) {
			echo $childXmlNode->nodeName.PHP_EOL;
			$nodeObject = NodeFactory::getNodeObject ($childXmlNode->nodeName, $this->getContext());
			if (isset($nodeObject)) {
				$cloneNodeObject = clone $nodeObject;
				if (!array_key_exists($childXmlNode->nodeName,$result))
					$result[$childXmlNode->nodeName] = array();
				$result[$childXmlNode->nodeName][] = array();
				$cloneNodeObject->loadFromNode($childXmlNode, $result[$childXmlNode->nodeName][count($result[$childXmlNode->nodeName])-1]);
			}
			else
				echo "[WARNING] ".$childXmlNode->nodeName." ".$this->getContext().PHP_EOL;
		}*/
	}
}

?>