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