1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace arc\xml; |
4
|
|
|
|
5
|
|
|
class NodeList extends \ArrayObject { |
6
|
|
|
|
7
|
|
|
protected $writer = null; |
8
|
|
|
|
9
|
1 |
|
public function __construct( $list = null, $writer = null ) { |
10
|
1 |
|
parent::__construct( $list ); |
11
|
1 |
|
$this->writer = $writer; |
12
|
1 |
|
} |
13
|
|
|
|
14
|
1 |
|
public function __call( $name, $args ) { |
15
|
1 |
|
$tagName = $name; |
16
|
1 |
|
list( $attributes, $content ) = $this->parseArgs( $args ); |
17
|
1 |
|
parent::offsetSet( null, $this->element( $tagName, $attributes, $content ) ); |
|
|
|
|
18
|
1 |
|
return $this; |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
public function __toString() { |
22
|
1 |
|
$indent = ''; |
23
|
1 |
|
if (!is_object( $this->writer ) && $this->writer->indent ) { |
24
|
|
|
$indent = "\r\n"; // element() will indent each line with whatever indent string is in the writer |
25
|
|
|
} |
26
|
1 |
|
return join( $indent, (array) $this ); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public static function indent( $indent, $content ) { |
30
|
1 |
|
if ($indent && ( strpos( $content, '<' ) !== false )) { |
31
|
|
|
$indent = ( is_string( $indent ) ? $indent : "\t" ); |
32
|
|
|
return "\r\n" . preg_replace( '/^(\s*)</m', $indent.'$1<', $content ) . "\r\n"; |
33
|
|
|
} else { |
34
|
1 |
|
return $content; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
private function parseArgs( $args ) { |
39
|
1 |
|
$attributes = array(); |
40
|
1 |
|
$content = ''; |
41
|
1 |
|
foreach ($args as $arg ) { |
42
|
1 |
|
if (is_string( $arg )) { |
43
|
1 |
|
$content .= $arg; |
44
|
1 |
|
} else if (is_array( $arg )) { |
45
|
1 |
|
foreach( $arg as $key => $subArg ) { |
46
|
1 |
|
if (is_numeric( $key )) { |
47
|
|
|
list( $subattributes, $subcontent ) = $this->parseArgs( $subArg ); |
48
|
|
|
$attributes = $subattributes + $attributes; |
49
|
|
|
$content .= $subcontent; |
50
|
|
|
} else { |
51
|
1 |
|
$attributes[ $key ] = $subArg; |
52
|
|
|
} |
53
|
1 |
|
} |
54
|
1 |
|
} else { |
55
|
1 |
|
$content .= $arg; |
56
|
|
|
} |
57
|
1 |
|
} |
58
|
1 |
|
return [ $attributes, $content ]; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
protected function element( $tagName, $attributes, $content ) { |
62
|
1 |
|
$tagName = $this->writer->name( $tagName ); |
63
|
1 |
|
$el = '<' . $tagName; |
64
|
1 |
|
$el .= $this->getAttributes( $attributes ); |
65
|
1 |
|
if ($this->hasContent( $content )) { |
66
|
1 |
|
$el .= '>' . self::indent( $this->writer->indent, $content ); |
67
|
1 |
|
$el .= '</' . $tagName . '>'; |
68
|
1 |
|
} else { |
69
|
|
|
$el .= '/>'; |
70
|
|
|
} |
71
|
1 |
|
return $el; |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
protected function getAttributes( $attributes ) { |
75
|
1 |
|
$result = ''; |
76
|
1 |
|
if (count( $attributes )) { |
77
|
1 |
|
foreach ($attributes as $name => $value ) { |
78
|
1 |
|
$result .= ' ' . $this->writer->name( $name ); |
79
|
1 |
|
$value = $this->writer->value( $value ); |
80
|
1 |
|
if ($value !== $name ) { |
81
|
1 |
|
$result .= '="' . $value . '"'; |
82
|
1 |
|
} |
83
|
1 |
|
} |
84
|
1 |
|
} |
85
|
1 |
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
protected function hasContent( $content ) { |
89
|
1 |
|
return ( trim( $content ) != '' ); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.