1 | <?php |
||
18 | class Proxy extends \ArrayObject implements DOMElement, SimpleXMLElement { |
||
19 | |||
20 | use \arc\traits\Proxy { |
||
21 | \arc\traits\Proxy::__construct as private ProxyConstruct; |
||
22 | \arc\traits\Proxy::__call as private ProxyCall; |
||
23 | } |
||
24 | |||
25 | private $parser = null; |
||
26 | |||
27 | 6 | public function __construct( $node, $parser) { |
|
31 | |||
32 | 1 | public function __toString() { |
|
35 | |||
36 | 2 | private function _isDomProperty( $name ) { |
|
37 | $domProperties = [ |
||
38 | 2 | 'tagName', 'nodeType', 'parentNode', |
|
39 | 'firstChild', 'lastChild', 'previousSibling', 'nextSibling', |
||
40 | 'ownerDocument', 'namespaceURI', 'prefix', |
||
41 | 'localName', 'baseURI', 'textContent' |
||
42 | ]; |
||
43 | 2 | return in_array( $name, $domProperties ); |
|
44 | } |
||
45 | |||
46 | 2 | private function _getTargetProperty($name) { |
|
47 | 2 | $value = null; |
|
48 | 2 | if ( !$this->_isDomProperty($name) ) { |
|
49 | 2 | $value = $this->target->{$name}; |
|
50 | } else { |
||
51 | 1 | $dom = dom_import_simplexml($this->target); |
|
52 | 1 | if ( isset($dom) ) { |
|
53 | 1 | $value = $dom->{$name}; |
|
54 | } |
||
55 | } |
||
56 | 2 | return $value; |
|
57 | } |
||
58 | |||
59 | 2 | private function _proxyResult( $value ) { |
|
60 | 2 | if ( $value instanceof \DOMElement ) { |
|
61 | $value = simplexml_import_dom($value); |
||
62 | } |
||
63 | 2 | if ( $value instanceof \SimpleXMLElement ) { |
|
64 | 2 | return new static( $value, $this->parser ); |
|
65 | } else { |
||
66 | 1 | return $value; |
|
67 | } |
||
68 | } |
||
69 | |||
70 | 5 | public function __get( $name) { |
|
76 | |||
77 | 2 | private function _domCall( $name, $args ) { |
|
78 | 2 | $dom = dom_import_simplexml($this->target); |
|
79 | 2 | foreach ( $args as $index => $arg ) { |
|
80 | 2 | if ( $arg instanceof \arc\xml\Proxy ) { |
|
81 | 1 | $args[$index] = dom_import_simplexml( $arg->nodeValue ); |
|
82 | 1 | } else if ( $arg instanceof \SimpleXMLElement ) { |
|
83 | 2 | $args[$index] = dom_import_simplexml( $arg ); |
|
84 | } |
||
85 | } |
||
86 | $importMethods = [ |
||
87 | 2 | 'appendChild', 'insertBefore', 'replaceChild' |
|
88 | ]; |
||
89 | 2 | if ( in_array( $name, $importMethods ) ) { |
|
90 | 1 | if ( isset($args[0]) && $args[0] instanceof \DOMNode ) { |
|
91 | 1 | if ( $args[0]->ownerDocument !== $this->ownerDocument ) { |
|
|
|||
92 | 1 | $args[0] = $this->ownerDocument->importNode( $args[0], true); |
|
93 | } |
||
94 | } |
||
95 | } |
||
96 | 2 | $result = call_user_func_array( [ $dom, $name], $args ); |
|
97 | 2 | if ( isset($result) && is_object($result) ) { |
|
98 | 1 | if ( $result instanceof \DOMElement ) { |
|
99 | 1 | return new static( $result, $this->parser ); |
|
100 | } |
||
101 | if ( $result instanceof \DOMNodeList ) { |
||
102 | $resultArray = []; |
||
103 | for ( $i=0, $l=$result->length; $i<$l; $i ++ ) { |
||
104 | $resultArray[$i] = new static( simplexml_import_dom($result->item($i)), $this->parser ); |
||
105 | } |
||
106 | return $resultArray; |
||
107 | } |
||
108 | } |
||
109 | 1 | return $result; |
|
110 | } |
||
111 | |||
112 | 2 | public function __call( $name, $args ) { |
|
119 | |||
120 | /** |
||
121 | * Search through the XML DOM with a single CSS selector |
||
122 | * @param string $query the CSS selector, most CSS 2 selectors work |
||
123 | * @return Proxy |
||
124 | */ |
||
125 | 3 | public function find( $query) { |
|
126 | 3 | $xpath = \arc\xml::css2Xpath( $query ); |
|
127 | 3 | $temp = $this->target->xpath( $xpath ); |
|
128 | 3 | foreach ($temp as $key => $value) { |
|
129 | 3 | $temp[ $key ] = new static( $value, $this->parser ); |
|
130 | } |
||
131 | 3 | return $temp; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Ssearches through the subtree for an element with the given id and returns it |
||
136 | * @param string $id |
||
137 | * @return Proxy |
||
138 | */ |
||
139 | 1 | public function getElementById( $id ) { |
|
142 | |||
143 | 2 | public function offsetGet( $offset ) |
|
147 | |||
148 | 1 | public function offsetSet( $offset, $value ) |
|
152 | |||
153 | public function offsetUnset( $offset ) |
||
154 | { |
||
155 | unset( $this->target[$offset] ); |
||
156 | } |
||
157 | } |
||
158 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.