Blame view

src/Request/ParamsRequestImpl/Nodes/NodeClass.php 4.08 KB
22521f1c   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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 = "";
	}

4ede4320   Benjamin Renard   Integration for s...
23
	public function getName()
22521f1c   Benjamin Renard   First commit
24
25
26
27
28
29
30
31
	{
		return $this->name;
	}

	protected function setValue($val)
	{
		$this->value = $val;
	}
966bd5f8   Benjamin Renard   Add request to ge...
32
	
22521f1c   Benjamin Renard   First commit
33
34
35
36
37
38
39
40
41
42
	protected function getValue()
	{
		return $this->value;
	}

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

966bd5f8   Benjamin Renard   Add request to ge...
43
	public function getAttribute($attName)
22521f1c   Benjamin Renard   First commit
44
	{
8ade06fe   Benjamin Renard   Improve config load
45
46
47
48
		if (array_key_exists($attName,$this->attributes)) {
			return $this->attributes[$attName];
		}
		return "";
22521f1c   Benjamin Renard   First commit
49
50
51
52
53
54
55
56
57
58
59
	}

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

	protected function addChild($child)
	{
		$this->children[] = $child;
	}
944199fe   Benjamin Renard   Use table definit...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	
	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);
	}
22521f1c   Benjamin Renard   First commit
74

966bd5f8   Benjamin Renard   Add request to ge...
75
	public function getChildren()
22521f1c   Benjamin Renard   First commit
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
	{
		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;
	}

966bd5f8   Benjamin Renard   Add request to ge...
100
	public function getChildInstanceByName($name, $createIfNoExist = false)
22521f1c   Benjamin Renard   First commit
101
102
	{
		$node = $this->getFirstChildByName($name);
966bd5f8   Benjamin Renard   Add request to ge...
103
		if (!isset($node) && $createIfNoExist)
22521f1c   Benjamin Renard   First commit
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		{
			$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 = $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;
	}
966bd5f8   Benjamin Renard   Add request to ge...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	
	/*
	 * @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;
		}*/
	}
22521f1c   Benjamin Renard   First commit
200
201
202
}

?>