Completed
Push — master ( a06a0a...5ed2e8 )
by Gilles
02:44
created
src/PHPHtmlParser/Dom/InnerNode.php 1 patch
Indentation   +274 added lines, -274 removed lines patch added patch discarded remove patch
@@ -13,278 +13,278 @@
 block discarded – undo
13 13
 abstract class InnerNode extends ArrayNode
14 14
 {
15 15
 
16
-    /**
17
-     * An array of all the children.
18
-     *
19
-     * @var array
20
-     */
21
-    protected $children = [];
22
-
23
-    /**
24
-     * Sets the encoding class to this node and propagates it
25
-     * to all its children.
26
-     *
27
-     * @param Encode $encode
28
-     * @return void
29
-     */
30
-    public function propagateEncoding(Encode $encode)
31
-    {
32
-        $this->encode = $encode;
33
-        $this->tag->setEncoding($encode);
34
-        // check children
35
-        foreach ($this->children as $id => $child) {
36
-            /** @var AbstractNode $node */
37
-            $node = $child['node'];
38
-            $node->propagateEncoding($encode);
39
-        }
40
-    }
41
-
42
-    /**
43
-     * Checks if this node has children.
44
-     *
45
-     * @return bool
46
-     */
47
-    public function hasChildren()
48
-    {
49
-        return ! empty($this->children);
50
-    }
51
-
52
-    /**
53
-     * Returns the child by id.
54
-     *
55
-     * @param int $id
56
-     * @return AbstractNode
57
-     * @throws ChildNotFoundException
58
-     */
59
-    public function getChild($id)
60
-    {
61
-        if ( ! isset($this->children[$id])) {
62
-            throw new ChildNotFoundException("Child '$id' not found in this node.");
63
-        }
64
-
65
-        return $this->children[$id]['node'];
66
-    }
67
-
68
-    /**
69
-     * Returns a new array of child nodes
70
-     *
71
-     * @return array
72
-     */
73
-    public function getChildren()
74
-    {
75
-        $nodes = [];
76
-        try {
77
-            $child = $this->firstChild();
78
-            do {
79
-                $nodes[] = $child;
80
-                $child   = $this->nextChild($child->id());
81
-            } while ( ! is_null($child));
82
-        } catch (ChildNotFoundException $e) {
83
-            // we are done looking for children
84
-        }
85
-
86
-        return $nodes;
87
-    }
88
-
89
-    /**
90
-     * Counts children
91
-     *
92
-     * @return int
93
-     */
94
-    public function countChildren()
95
-    {
96
-        return count($this->children);
97
-    }
98
-
99
-    /**
100
-     * Adds a child node to this node and returns the id of the child for this
101
-     * parent.
102
-     *
103
-     * @param AbstractNode $child
104
-     * @return bool
105
-     * @throws CircularException
106
-     */
107
-    public function addChild(AbstractNode $child)
108
-    {
109
-        $key = null;
110
-
111
-        // check integrity
112
-        if ($this->isAncestor($child->id())) {
113
-            throw new CircularException('Can not add child. It is my ancestor.');
114
-        }
115
-
116
-        // check if child is itself
117
-        if ($child->id() == $this->id) {
118
-            throw new CircularException('Can not set itself as a child.');
119
-        }
120
-
121
-        if ($this->hasChildren()) {
122
-            if (isset($this->children[$child->id()])) {
123
-                // we already have this child
124
-                return false;
125
-            }
126
-            $sibling                      = $this->lastChild();
127
-            $key                          = $sibling->id();
128
-            $this->children[$key]['next'] = $child->id();
129
-        }
130
-
131
-        // add the child
132
-        $this->children[$child->id()] = [
133
-            'node' => $child,
134
-            'next' => null,
135
-            'prev' => $key,
136
-        ];
137
-
138
-        // tell child I am the new parent
139
-        $child->setParent($this);
140
-
141
-        //clear any cache
142
-        $this->clear();
143
-
144
-        return true;
145
-    }
146
-
147
-    /**
148
-     * Removes the child by id.
149
-     *
150
-     * @param int $id
151
-     * @return $this
152
-     */
153
-    public function removeChild($id)
154
-    {
155
-        if ( ! isset($this->children[$id])) {
156
-            return $this;
157
-        }
158
-
159
-        // handle moving next and previous assignments.
160
-        $next = $this->children[$id]['next'];
161
-        $prev = $this->children[$id]['prev'];
162
-        if ( ! is_null($next)) {
163
-            $this->children[$next]['prev'] = $prev;
164
-        }
165
-        if ( ! is_null($prev)) {
166
-            $this->children[$prev]['next'] = $next;
167
-        }
168
-
169
-        // remove the child
170
-        unset($this->children[$id]);
171
-
172
-        //clear any cache
173
-        $this->clear();
174
-
175
-        return $this;
176
-    }
177
-
178
-    /**
179
-     * Attempts to get the next child.
180
-     *
181
-     * @param int $id
182
-     * @return AbstractNode
183
-     * @uses $this->getChild()
184
-     * @throws ChildNotFoundException
185
-     */
186
-    public function nextChild($id)
187
-    {
188
-        $child = $this->getChild($id);
189
-        $next  = $this->children[$child->id()]['next'];
190
-
191
-        return $this->getChild($next);
192
-    }
193
-
194
-    /**
195
-     * Attempts to get the previous child.
196
-     *
197
-     * @param int $id
198
-     * @return AbstractNode
199
-     * @uses $this->getChild()
200
-     * @throws ChildNotFoundException
201
-     */
202
-    public function previousChild($id)
203
-    {
204
-        $child = $this->getchild($id);
205
-        $next  = $this->children[$child->id()]['prev'];
206
-
207
-        return $this->getChild($next);
208
-    }
209
-
210
-    /**
211
-     * Checks if the given node id is a child of the
212
-     * current node.
213
-     *
214
-     * @param int $id
215
-     * @return bool
216
-     */
217
-    public function isChild($id)
218
-    {
219
-        foreach ($this->children as $childId => $child) {
220
-            if ($id == $childId) {
221
-                return true;
222
-            }
223
-        }
224
-
225
-        return false;
226
-    }
227
-
228
-    /**
229
-     * Removes the child with id $childId and replace it with the new child
230
-     * $newChild.
231
-     *
232
-     * @param int $childId
233
-     * @param AbstractNode $newChild
234
-     * @throws ChildNotFoundException
235
-     */
236
-    public function replaceChild($childId, AbstractNode $newChild)
237
-    {
238
-        $oldChild = $this->getChild($childId);
239
-        $keys = array_keys($this->children);
240
-        $index = array_search($childId, $keys, true);
241
-        $keys[$index] = $newChild->id();
242
-        $this->children = array_combine($keys, $this->children);
243
-        $this->children[$newChild->id()] = $newChild;
244
-        unset($oldChild);
245
-    }
246
-
247
-    /**
248
-     * Checks if the given node id is a descendant of the
249
-     * current node.
250
-     *
251
-     * @param int $id
252
-     * @return bool
253
-     */
254
-    public function isDescendant($id)
255
-    {
256
-        if ($this->isChild($id)) {
257
-            return true;
258
-        }
259
-
260
-        foreach ($this->children as $childId => $child) {
261
-            /** @var InnerNode $node */
262
-            $node = $child['node'];
263
-            if ($node instanceof InnerNode &&
264
-                $node->hasChildren() &&
265
-                $node->isDescendant($id)
266
-            ) {
267
-                return true;
268
-            }
269
-        }
270
-
271
-        return false;
272
-    }
273
-
274
-    /**
275
-     * Sets the parent node.
276
-     *
277
-     * @param InnerNode $parent
278
-     * @return $this
279
-     * @throws CircularException
280
-     */
281
-    public function setParent(InnerNode $parent)
282
-    {
283
-        // check integrity
284
-        if ($this->isDescendant($parent->id())) {
285
-            throw new CircularException('Can not add descendant "'.$parent->id().'" as my parent.');
286
-        }
287
-
288
-        return parent::setParent($parent);
289
-    }
16
+	/**
17
+	 * An array of all the children.
18
+	 *
19
+	 * @var array
20
+	 */
21
+	protected $children = [];
22
+
23
+	/**
24
+	 * Sets the encoding class to this node and propagates it
25
+	 * to all its children.
26
+	 *
27
+	 * @param Encode $encode
28
+	 * @return void
29
+	 */
30
+	public function propagateEncoding(Encode $encode)
31
+	{
32
+		$this->encode = $encode;
33
+		$this->tag->setEncoding($encode);
34
+		// check children
35
+		foreach ($this->children as $id => $child) {
36
+			/** @var AbstractNode $node */
37
+			$node = $child['node'];
38
+			$node->propagateEncoding($encode);
39
+		}
40
+	}
41
+
42
+	/**
43
+	 * Checks if this node has children.
44
+	 *
45
+	 * @return bool
46
+	 */
47
+	public function hasChildren()
48
+	{
49
+		return ! empty($this->children);
50
+	}
51
+
52
+	/**
53
+	 * Returns the child by id.
54
+	 *
55
+	 * @param int $id
56
+	 * @return AbstractNode
57
+	 * @throws ChildNotFoundException
58
+	 */
59
+	public function getChild($id)
60
+	{
61
+		if ( ! isset($this->children[$id])) {
62
+			throw new ChildNotFoundException("Child '$id' not found in this node.");
63
+		}
64
+
65
+		return $this->children[$id]['node'];
66
+	}
67
+
68
+	/**
69
+	 * Returns a new array of child nodes
70
+	 *
71
+	 * @return array
72
+	 */
73
+	public function getChildren()
74
+	{
75
+		$nodes = [];
76
+		try {
77
+			$child = $this->firstChild();
78
+			do {
79
+				$nodes[] = $child;
80
+				$child   = $this->nextChild($child->id());
81
+			} while ( ! is_null($child));
82
+		} catch (ChildNotFoundException $e) {
83
+			// we are done looking for children
84
+		}
85
+
86
+		return $nodes;
87
+	}
88
+
89
+	/**
90
+	 * Counts children
91
+	 *
92
+	 * @return int
93
+	 */
94
+	public function countChildren()
95
+	{
96
+		return count($this->children);
97
+	}
98
+
99
+	/**
100
+	 * Adds a child node to this node and returns the id of the child for this
101
+	 * parent.
102
+	 *
103
+	 * @param AbstractNode $child
104
+	 * @return bool
105
+	 * @throws CircularException
106
+	 */
107
+	public function addChild(AbstractNode $child)
108
+	{
109
+		$key = null;
110
+
111
+		// check integrity
112
+		if ($this->isAncestor($child->id())) {
113
+			throw new CircularException('Can not add child. It is my ancestor.');
114
+		}
115
+
116
+		// check if child is itself
117
+		if ($child->id() == $this->id) {
118
+			throw new CircularException('Can not set itself as a child.');
119
+		}
120
+
121
+		if ($this->hasChildren()) {
122
+			if (isset($this->children[$child->id()])) {
123
+				// we already have this child
124
+				return false;
125
+			}
126
+			$sibling                      = $this->lastChild();
127
+			$key                          = $sibling->id();
128
+			$this->children[$key]['next'] = $child->id();
129
+		}
130
+
131
+		// add the child
132
+		$this->children[$child->id()] = [
133
+			'node' => $child,
134
+			'next' => null,
135
+			'prev' => $key,
136
+		];
137
+
138
+		// tell child I am the new parent
139
+		$child->setParent($this);
140
+
141
+		//clear any cache
142
+		$this->clear();
143
+
144
+		return true;
145
+	}
146
+
147
+	/**
148
+	 * Removes the child by id.
149
+	 *
150
+	 * @param int $id
151
+	 * @return $this
152
+	 */
153
+	public function removeChild($id)
154
+	{
155
+		if ( ! isset($this->children[$id])) {
156
+			return $this;
157
+		}
158
+
159
+		// handle moving next and previous assignments.
160
+		$next = $this->children[$id]['next'];
161
+		$prev = $this->children[$id]['prev'];
162
+		if ( ! is_null($next)) {
163
+			$this->children[$next]['prev'] = $prev;
164
+		}
165
+		if ( ! is_null($prev)) {
166
+			$this->children[$prev]['next'] = $next;
167
+		}
168
+
169
+		// remove the child
170
+		unset($this->children[$id]);
171
+
172
+		//clear any cache
173
+		$this->clear();
174
+
175
+		return $this;
176
+	}
177
+
178
+	/**
179
+	 * Attempts to get the next child.
180
+	 *
181
+	 * @param int $id
182
+	 * @return AbstractNode
183
+	 * @uses $this->getChild()
184
+	 * @throws ChildNotFoundException
185
+	 */
186
+	public function nextChild($id)
187
+	{
188
+		$child = $this->getChild($id);
189
+		$next  = $this->children[$child->id()]['next'];
190
+
191
+		return $this->getChild($next);
192
+	}
193
+
194
+	/**
195
+	 * Attempts to get the previous child.
196
+	 *
197
+	 * @param int $id
198
+	 * @return AbstractNode
199
+	 * @uses $this->getChild()
200
+	 * @throws ChildNotFoundException
201
+	 */
202
+	public function previousChild($id)
203
+	{
204
+		$child = $this->getchild($id);
205
+		$next  = $this->children[$child->id()]['prev'];
206
+
207
+		return $this->getChild($next);
208
+	}
209
+
210
+	/**
211
+	 * Checks if the given node id is a child of the
212
+	 * current node.
213
+	 *
214
+	 * @param int $id
215
+	 * @return bool
216
+	 */
217
+	public function isChild($id)
218
+	{
219
+		foreach ($this->children as $childId => $child) {
220
+			if ($id == $childId) {
221
+				return true;
222
+			}
223
+		}
224
+
225
+		return false;
226
+	}
227
+
228
+	/**
229
+	 * Removes the child with id $childId and replace it with the new child
230
+	 * $newChild.
231
+	 *
232
+	 * @param int $childId
233
+	 * @param AbstractNode $newChild
234
+	 * @throws ChildNotFoundException
235
+	 */
236
+	public function replaceChild($childId, AbstractNode $newChild)
237
+	{
238
+		$oldChild = $this->getChild($childId);
239
+		$keys = array_keys($this->children);
240
+		$index = array_search($childId, $keys, true);
241
+		$keys[$index] = $newChild->id();
242
+		$this->children = array_combine($keys, $this->children);
243
+		$this->children[$newChild->id()] = $newChild;
244
+		unset($oldChild);
245
+	}
246
+
247
+	/**
248
+	 * Checks if the given node id is a descendant of the
249
+	 * current node.
250
+	 *
251
+	 * @param int $id
252
+	 * @return bool
253
+	 */
254
+	public function isDescendant($id)
255
+	{
256
+		if ($this->isChild($id)) {
257
+			return true;
258
+		}
259
+
260
+		foreach ($this->children as $childId => $child) {
261
+			/** @var InnerNode $node */
262
+			$node = $child['node'];
263
+			if ($node instanceof InnerNode &&
264
+				$node->hasChildren() &&
265
+				$node->isDescendant($id)
266
+			) {
267
+				return true;
268
+			}
269
+		}
270
+
271
+		return false;
272
+	}
273
+
274
+	/**
275
+	 * Sets the parent node.
276
+	 *
277
+	 * @param InnerNode $parent
278
+	 * @return $this
279
+	 * @throws CircularException
280
+	 */
281
+	public function setParent(InnerNode $parent)
282
+	{
283
+		// check integrity
284
+		if ($this->isDescendant($parent->id())) {
285
+			throw new CircularException('Can not add descendant "'.$parent->id().'" as my parent.');
286
+		}
287
+
288
+		return parent::setParent($parent);
289
+	}
290 290
 }
291 291
\ No newline at end of file
Please login to merge, or discard this patch.