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 class allows you to create valid and nicely indented XML strings |
15
|
|
|
* Any method not explicitly defined is interpreted as a new XML element to create. |
16
|
|
|
*/ |
17
|
|
|
class Writer { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string $indent The string to ident each level with. Default is a tab. |
21
|
|
|
*/ |
22
|
|
|
public $indent = "\t"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string $newLine The string to use as a new line or linebreak. Defaults to \r\n. |
26
|
|
|
*/ |
27
|
|
|
public $newLine = "\r\n"; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $options allows you to set the indent and newLine |
31
|
|
|
* options immediately upon construction |
32
|
|
|
*/ |
33
|
1 |
|
public function __construct( $options = []) |
34
|
|
|
{ |
35
|
1 |
|
$optionList = [ 'indent', 'newLine' ]; |
36
|
1 |
|
foreach( $options as $option => $optionValue) { |
37
|
|
|
if (in_array( $option, $optionList )) { |
38
|
|
|
$this->{$option} = $optionValue; |
39
|
|
|
} |
40
|
1 |
|
} |
41
|
1 |
|
} |
42
|
|
|
|
43
|
2 |
|
public function __call( $name, $args) |
44
|
|
|
{ |
45
|
2 |
|
return call_user_func_array( [ new NodeList( [], $this ), $name ], $args ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns a guaranteed valid XML name. Removes illegal characters from the name. |
50
|
|
|
* @param string $name |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
2 |
|
public static function name( $name) |
54
|
|
|
{ |
55
|
2 |
|
return preg_replace( '/^[^:a-z_]*/isU', '', |
56
|
2 |
|
preg_replace( '/[^-.0-9:a-z_]/isU', '', $name |
57
|
2 |
|
) ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a guaranteed valid XML attribute value. Removes illegal characters. |
62
|
|
|
* @param string|array|bool $value |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
3 |
|
public static function value( $value) |
66
|
|
|
{ |
67
|
3 |
|
if (is_array( $value )) { |
68
|
|
|
$content = array_reduce( $value, function( $result, $value) |
69
|
|
|
{ |
70
|
|
|
return $result . ' ' . self::value( $value ); |
71
|
|
|
} ); |
72
|
3 |
|
} else if (is_bool( $value )) { |
73
|
|
|
$content = $value ? 'true' : 'false'; |
74
|
|
|
} else { |
75
|
3 |
|
$value = (string) $value; |
76
|
3 |
|
if (preg_match( '/^\s*<!\[CDATA\[/', $value )) { |
77
|
|
|
$content = $value; |
78
|
|
|
} else { |
79
|
3 |
|
$content = htmlspecialchars( $value, ENT_QUOTES, 'UTF-8' ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
3 |
|
return $content; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns a guaranteed valid XML attribute. Removes illegal characters. |
87
|
|
|
* @param string $name |
88
|
|
|
* @param string|array|bool $value |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
2 |
|
public static function attribute( $name, $value) |
92
|
|
|
{ |
93
|
2 |
|
return ' ' . self::name( $name ) . '="' . self::value( $value ) . '"'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns a guaranteed valid XML comment. Removes illegal characters. |
98
|
|
|
* @param string $content |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public static function comment( $content) |
102
|
|
|
{ |
103
|
1 |
|
return '<!-- ' . self::value( $content ) . ' -->'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns a guaranteed valid XML CDATA string. Removes illegal characters. |
108
|
|
|
* @param string $content |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
1 |
|
public static function cdata( $content) |
112
|
|
|
{ |
113
|
1 |
|
return '<![CDATA[' . str_replace( ']]>', ']]>', $content ) . ']]>'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns an XML preamble. |
118
|
|
|
* @param string $version Defaults to '1.0' |
119
|
|
|
* @param string $encoding Defaults to null |
120
|
|
|
* @param string $standalone Defaults to null |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
1 |
|
public static function preamble( $version = '1.0', $encoding = null, $standalone = null) |
124
|
|
|
{ |
125
|
1 |
|
if (isset($standalone)) { |
126
|
|
|
if ($standalone === 'false') { |
127
|
|
|
$standalone = 'no'; |
128
|
|
|
} else if ($standalone !== 'no') { |
129
|
|
|
$standalone = ( $standalone ? 'yes' : 'no' ); |
130
|
|
|
} |
131
|
|
|
$standalone = self::attribute( 'standalone', $standalone ); |
132
|
|
|
} else { |
133
|
1 |
|
$standalone = ''; |
134
|
|
|
} |
135
|
1 |
|
$preamble = '<?xml version="' . self::value($version) . '"'; |
136
|
1 |
|
if (isset( $encoding )) { |
137
|
|
|
$preamble .= ' " encoding="' . self::value($encoding) . '"'; |
138
|
|
|
} |
139
|
1 |
|
$preamble .= $standalone . ' ?>'; |
140
|
1 |
|
return $preamble; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|