1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Ariadne Component Library. |
4
|
|
|
* |
5
|
|
|
* (c) Muze <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace arc\xml; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This trait is used by the XML Writer to represent a list of child nodes |
15
|
|
|
*/ |
16
|
|
|
trait NodeListTrait { |
17
|
|
|
|
18
|
|
|
protected $writer = null; |
19
|
|
|
protected $invalidChars = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $list |
23
|
|
|
* @param Writer $writer |
24
|
|
|
*/ |
25
|
6 |
|
public function __construct( $list = null, $writer = null ) |
26
|
|
|
{ |
27
|
6 |
|
parent::__construct( $list ); |
28
|
6 |
|
$this->writer = $writer; |
29
|
6 |
|
} |
30
|
|
|
|
31
|
6 |
|
public function __call( $name, $args ) |
32
|
|
|
{ |
33
|
6 |
|
$tagName = $name; |
34
|
6 |
|
list( $attributes, $content ) = $this->parseArgs( $args ); |
35
|
6 |
|
parent::offsetSet( null, $this->element( $tagName, $attributes, $content ) ); |
|
|
|
|
36
|
6 |
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
6 |
|
public function __toString() |
40
|
|
|
{ |
41
|
6 |
|
$indent = ''; |
42
|
6 |
|
if (!is_object( $this->writer ) || $this->writer->indent ) { |
43
|
6 |
|
$indent = "\r\n"; // element() will indent each line with whatever indent string is in the writer |
44
|
|
|
} |
45
|
6 |
|
return join( $indent, (array) $this ); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
protected static function indent( $content, $indent="\t", $newLine="\r\n" ) |
49
|
|
|
{ |
50
|
6 |
|
if ($indent && ( strpos( $content, '<' ) !== false )) { |
51
|
6 |
|
$indent = ( is_string( $indent ) ? $indent : "\t" ); |
52
|
6 |
|
return $newLine . preg_replace( '/^(\s*[^\<]*)</m', $indent.'$1<', $content ) . $newLine; |
53
|
|
|
} else { |
54
|
4 |
|
return $content; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
protected function escape( $contents ) { |
59
|
6 |
|
$contents = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $contents); |
60
|
6 |
|
return htmlspecialchars( $contents, ENT_XML1, 'UTF-8'); |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
protected function parseArgs( $args ) |
64
|
|
|
{ |
65
|
6 |
|
$attributes = array(); |
66
|
6 |
|
$content = ''; |
67
|
6 |
|
foreach ($args as $arg ) { |
68
|
6 |
|
if (is_string( $arg ) ) { |
69
|
6 |
|
$content .= $this->escape($arg); |
70
|
6 |
|
} else if (is_array( $arg )) { |
71
|
6 |
|
foreach( $arg as $key => $subArg ) { |
72
|
6 |
|
if (is_numeric( $key )) { |
73
|
|
|
list( $subattributes, $subcontent ) = $this->parseArgs( $subArg ); |
74
|
|
|
$attributes = array_merge( $attributes, $subattributes); |
75
|
|
|
$content = \arc\xml::raw( $content . $subcontent ); |
76
|
|
|
} else { |
77
|
6 |
|
$attributes[ $key ] = $subArg; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
6 |
|
$content .= $arg; |
82
|
|
|
} |
83
|
|
|
} |
84
|
6 |
|
return [ $attributes, $content ]; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
protected function element( $tagName, $attributes, $content ) |
88
|
|
|
{ |
89
|
6 |
|
$tagName = \arc\xml::name( $tagName ); |
90
|
6 |
|
$el = '<' . $tagName; |
91
|
6 |
|
$el .= $this->getAttributes( $attributes ); |
92
|
6 |
|
if ($this->hasContent( $content )) { |
93
|
6 |
|
$el .= '>' . self::indent( $content, $this->writer->indent, $this->writer->newLine ); |
94
|
6 |
|
$el .= '</' . $tagName . '>'; |
95
|
|
|
} else { |
96
|
|
|
$el .= '/>'; |
97
|
|
|
} |
98
|
6 |
|
return $el; |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
protected function getAttributes( $attributes ) |
102
|
|
|
{ |
103
|
6 |
|
$result = ''; |
104
|
6 |
|
if (count( $attributes )) { |
105
|
6 |
|
foreach ($attributes as $name => $value ) { |
106
|
6 |
|
$result .= \arc\xml::attribute( $name, $value ); |
107
|
|
|
} |
108
|
|
|
} |
109
|
6 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
protected function hasContent( $content ) |
113
|
|
|
{ |
114
|
6 |
|
return ( trim( $content ) != '' ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
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.