|
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
|
2 |
|
*/ |
|
25
|
|
|
public function __construct( $list = null, $writer = null ) |
|
26
|
2 |
|
{ |
|
27
|
2 |
|
parent::__construct( $list ); |
|
28
|
2 |
|
$this->writer = $writer; |
|
29
|
|
|
} |
|
30
|
2 |
|
|
|
31
|
|
|
public function __call( $name, $args ) |
|
32
|
2 |
|
{ |
|
33
|
2 |
|
$tagName = $name; |
|
|
|
|
|
|
34
|
2 |
|
list( $attributes, $content ) = $this->parseArgs( $args ); |
|
35
|
2 |
|
parent::offsetSet( null, $this->element( $tagName, $attributes, $content ) ); |
|
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
2 |
|
|
|
39
|
|
|
public function __toString() |
|
40
|
2 |
|
{ |
|
41
|
2 |
|
$indent = ''; |
|
42
|
2 |
|
if (!is_object( $this->writer ) || $this->writer->indent ) { |
|
43
|
|
|
$indent = "\r\n"; // element() will indent each line with whatever indent string is in the writer |
|
44
|
2 |
|
} |
|
45
|
|
|
return join( $indent, (array) $this ); |
|
46
|
|
|
} |
|
47
|
2 |
|
|
|
48
|
|
|
protected static function indent( $content, $indent="\t", $newLine="\r\n" ) |
|
49
|
2 |
|
{ |
|
50
|
2 |
|
if ($indent && ( strpos( $content, '<' ) !== false )) { |
|
51
|
2 |
|
$indent = ( is_string( $indent ) ? $indent : "\t" ); |
|
52
|
|
|
return $newLine . preg_replace( '/^(\s*[^\<]*)</m', $indent.'$1<', $content ) . $newLine; |
|
53
|
2 |
|
} else { |
|
54
|
|
|
return $content; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
2 |
|
|
|
58
|
|
|
protected function escape( $contents ) { |
|
59
|
2 |
|
$contents = preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', '', $contents); |
|
60
|
2 |
|
return htmlspecialchars( $contents, ENT_XML1, 'UTF-8'); |
|
61
|
2 |
|
} |
|
62
|
2 |
|
|
|
63
|
2 |
|
protected function parseArgs( $args ) |
|
64
|
2 |
|
{ |
|
65
|
2 |
|
$attributes = array(); |
|
66
|
2 |
|
$content = ''; |
|
|
|
|
|
|
67
|
|
|
foreach ($args as $arg ) { |
|
68
|
|
|
if (is_string( $arg ) ) { |
|
69
|
|
|
$content .= $this->escape($arg); |
|
70
|
|
|
} else if (is_array( $arg )) { |
|
71
|
2 |
|
foreach( $arg as $key => $subArg ) { |
|
72
|
|
|
if (is_numeric( $key )) { |
|
73
|
|
|
list( $subattributes, $subcontent ) = $this->parseArgs( $subArg ); |
|
74
|
|
|
$attributes = array_merge( $attributes, $subattributes); |
|
|
|
|
|
|
75
|
2 |
|
$content = \arc\xml::raw( $content . $subcontent ); |
|
|
|
|
|
|
76
|
|
|
} else { |
|
77
|
|
|
$attributes[ $key ] = $subArg; |
|
78
|
2 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
2 |
|
$content .= $arg; |
|
82
|
|
|
} |
|
83
|
2 |
|
} |
|
84
|
2 |
|
return [ $attributes, $content ]; |
|
85
|
2 |
|
} |
|
86
|
2 |
|
|
|
87
|
2 |
|
protected function element( $tagName, $attributes, $content ) |
|
88
|
2 |
|
{ |
|
89
|
|
|
$tagName = \arc\xml::name( $tagName ); |
|
90
|
|
|
$el = '<' . $tagName; |
|
|
|
|
|
|
91
|
|
|
$el .= $this->getAttributes( $attributes ); |
|
|
|
|
|
|
92
|
2 |
|
if ($this->hasContent( $content )) { |
|
93
|
|
|
$el .= '>' . self::indent( $content, $this->writer->indent, $this->writer->newLine ); |
|
94
|
|
|
$el .= '</' . $tagName . '>'; |
|
95
|
2 |
|
} else { |
|
96
|
|
|
$el .= '/>'; |
|
97
|
2 |
|
} |
|
98
|
2 |
|
return $el; |
|
99
|
2 |
|
} |
|
100
|
2 |
|
|
|
101
|
|
|
protected function getAttributes( $attributes ) |
|
102
|
|
|
{ |
|
103
|
2 |
|
$result = ''; |
|
104
|
|
|
if (count( $attributes )) { |
|
105
|
|
|
foreach ($attributes as $name => $value ) { |
|
106
|
2 |
|
$result .= \arc\xml::attribute( $name, $value ); |
|
107
|
|
|
} |
|
108
|
2 |
|
} |
|
109
|
|
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function hasContent( $content ) |
|
113
|
|
|
{ |
|
114
|
|
|
return ( trim( $content ) != '' ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.