1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
TODO: implement simplexml access methods to the proxy |
|
|
|
|
4
|
|
|
probably needs to extend ArrayObject in some way to do this properly |
5
|
|
|
*/ |
6
|
|
|
namespace arc\xml; |
7
|
|
|
|
8
|
|
|
class Proxy extends \ArrayObject { |
9
|
|
|
|
10
|
|
|
use \arc\traits\Proxy { |
11
|
|
|
\arc\traits\Proxy::__construct as private ProxyConstruct; |
12
|
|
|
\arc\traits\Proxy::__call as private ProxyCall; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
private $parser = null; |
16
|
2 |
|
|
17
|
2 |
|
public function __construct( $node, $parser) { |
18
|
2 |
|
$this->ProxyConstruct( $node ); |
19
|
2 |
|
$this->parser = $parser; |
20
|
|
|
} |
21
|
1 |
|
|
22
|
1 |
|
public function __toString() { |
23
|
|
|
return $this->target->asXML(); |
24
|
|
|
} |
25
|
2 |
|
|
26
|
2 |
|
public function __get( $name) { |
27
|
2 |
|
if ($name == 'nodeValue') { |
28
|
|
|
return $this->target.''; |
29
|
1 |
|
} |
30
|
1 |
|
$value = $this->target->{$name}; |
31
|
1 |
|
if (is_object( $value )) { |
32
|
|
|
return new static( $value, $this->parser ); |
33
|
|
|
} else { |
34
|
|
|
return $value; |
35
|
|
|
} |
36
|
|
|
} |
37
|
1 |
|
|
38
|
1 |
|
public function __call( $name, $args ) { |
39
|
1 |
|
if ( !method_exists($this->target, $name) ) { |
40
|
1 |
|
$dom = dom_import_simplexml($this->target); |
|
|
|
|
41
|
1 |
|
$result = call_user_func_array( [ $dom, $name], $args ); |
42
|
|
|
if ( isset($result) && is_object($result) ) { |
43
|
1 |
|
if ( $result instanceof \DOMNode ) { |
44
|
|
|
return new static( $result, $this->parser ); |
45
|
|
|
} |
46
|
|
|
if ( $result instanceof \DOMNodeList ) { |
47
|
|
|
$resultArray = []; |
48
|
|
|
for ( $i=0, $l=$result->length; $i<$l; $i ++ ) { |
49
|
|
|
$resultArray[$i] = new static( simplexml_import_dom($result->item($i)), $this->parser ); |
50
|
|
|
} |
51
|
|
|
return $resultArray; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return $result; |
55
|
|
|
} else { |
56
|
|
|
return $this->ProxyCall($name, $args); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function find( $query) { |
61
|
|
|
$xpath = \arc\xml::css2Xpath( $query ); |
62
|
|
|
$temp = $this->target->xpath( $xpath ); |
|
|
|
|
63
|
|
|
foreach ($temp as $key => $value) { |
64
|
|
|
$temp[ $key ] = new static( $value, $this->parser ); |
65
|
|
|
} |
66
|
|
|
return $temp; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function offsetGet( $offset ) |
70
|
|
|
{ |
71
|
|
|
return (string) $this->target[$offset]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function offsetSet( $offset, $value ) |
75
|
|
|
{ |
76
|
|
|
$this->target[$offset] = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function offsetUnset( $offset ) |
80
|
|
|
{ |
81
|
|
|
unset( $this->target[$offset] ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|