-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDOMNodeListWrapper.php
More file actions
executable file
·114 lines (97 loc) · 2.12 KB
/
DOMNodeListWrapper.php
File metadata and controls
executable file
·114 lines (97 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @class DOMNodeListWrapper
* @author Robert McLeod <hamstar@telescum.co.nz>
* @date 23/04/2010
* @version 0.1b
* @copyright 2009 Robert McLeod
*/
class DOMNodeListWrapper {
public $length = 0;
private $DOMNodeList = array();
/**
* This constructor can convert a DOMNodeList that it is
* given into a DOMNodeListWrapper with DOMNodeWrapper objects
* inside. This gives expanded features to the list.
*
* @param object $DOMNodeList A DOMNodeList object to be converted
*/
function __construct( $DOMNodeList=null ) {
if ( !is_null($DOMNodeList) ) {
// Run through each node
foreach ( $DOMNodeList as $n ) {
if ( get_class($n) == 'DOMNodeWrapper' ) {
$this->DOMNodeList[] = $n; // node already wrapped
} else {
$this->DOMNodeList[] = new DOMNodeWrapper($n); // wrap the node
}
$this->length++;
}
}
}
/**
* This list can be put in a foreach loop!!
*/
function __invoke($i=null) {
if ( !is_null($i) ) {
return $this->DOMNodeList[$i];
}
return $this->DOMNodeList;
}
/**
* For backwards compatibility - returns list index specified
*
* @param int $i List index to return
*
* @return object
*/
function item($i) {
return isset($this->DOMNodeList[$i]) ? $this->DOMNodeList[$i]: false;
}
/**
* Add a node to the list
*
* @param object $DOMNode The DOM Node to add
*
* @return object
*/
function addNode( $DOMNode ) {
$this->DOMNodeList[] = new DOMNodeWrapper( $DOMNode );
$this->length++;
return $this;
}
/**
* Remove a node from the list
*
* @param int $i The list index to remove
*
* @return object
*/
function removeNode( $i ) {
unset( $this->DOMNodeList[$i] );
$this->length--;
}
/**
* Return the first item
*
* @return object
*/
function first() {
return $this->item(0);
}
/**
* Return the last item
*
* @return object
*/
function last() {
return $this->item( count($this->DOMNodeList) -1 );
}
/**
* Shortcut method for the item method
*/
function nth( $i ) {
return $this->item($i);
}
}
?>