|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\Output; |
|
14
|
|
|
|
|
15
|
|
|
class Xml extends AbstractOutput |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
* @see http://www.ietf.org/rfc/rfc3023.txt |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $content_type = 'text/xml'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $version = '1.0'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $encoding = 'UTF-8'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $item_key = 'item'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $items_key = 'items'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Factory encoder. |
|
46
|
|
|
* @codeCoverageIgnore |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
public function encode(array $data, $rootNode='root') |
|
50
|
|
|
{ |
|
51
|
|
|
if (extension_loaded('xmlwriter')) { |
|
52
|
|
|
$xml = new Xml\XmlWriter(); |
|
53
|
|
|
} else { |
|
54
|
|
|
// SimpleXml is a default PHP extension |
|
55
|
|
|
$xml = new Xml\SimpleXml(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $xml->encode($data, $rootNode); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Converts a boolean value to its string representation. |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $var |
|
65
|
|
|
* @return string|mixed String Either 'True', 'False' as string or the initial value as is. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function booleanString($var) |
|
68
|
|
|
{ |
|
69
|
|
|
return is_bool($var) |
|
70
|
|
|
? ($var ? 'True' : 'False') |
|
71
|
|
|
: $var; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/* |
|
|
|
|
|
|
77
|
|
|
if ( !function_exists( 'xmlentities' ) ) { |
|
78
|
|
|
|
|
79
|
|
|
public function xmlentities($string) |
|
80
|
|
|
{ |
|
81
|
|
|
$not_in_list = "A-Z0-9a-z\s_-"; |
|
82
|
|
|
|
|
83
|
|
|
return preg_replace_callback( "/[^{$not_in_list}]/" , function ($CHAR) { |
|
84
|
|
|
if ( !is_string( $CHAR[0] ) || ( strlen( $CHAR[0] ) > 1 ) ) { |
|
85
|
|
|
die( "function: 'get_xml_entity_at_index_zero' requires data type: 'char' (single character). '{$CHAR[0]}' does not match this type." ); |
|
86
|
|
|
} |
|
87
|
|
|
switch ($CHAR[0]) { |
|
88
|
|
|
case "'": case '"': case '&': case '<': case '>': case ':': case '/': |
|
89
|
|
|
return htmlspecialchars( $CHAR[0], ENT_QUOTES ); break; |
|
90
|
|
|
default: |
|
91
|
|
|
return numeric_entity_4_char($CHAR[0]); break; |
|
92
|
|
|
} |
|
93
|
|
|
}, $string ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function get_xml_entity_at_index_zero($CHAR) |
|
97
|
|
|
{ |
|
98
|
|
|
if ( !is_string( $CHAR[0] ) || ( strlen( $CHAR[0] ) > 1 ) ) { |
|
99
|
|
|
die( "function: 'get_xml_entity_at_index_zero' requires data type: 'char' (single character). '{$CHAR[0]}' does not match this type." ); |
|
100
|
|
|
} |
|
101
|
|
|
switch ($CHAR[0]) { |
|
102
|
|
|
case "'": case '"': case '&': case '<': case '>': |
|
103
|
|
|
return htmlspecialchars( $CHAR[0], ENT_QUOTES ); break; |
|
104
|
|
|
default: |
|
105
|
|
|
return numeric_entity_4_char($CHAR[0]); break; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function numeric_entity_4_char($char) |
|
110
|
|
|
{ |
|
111
|
|
|
return "&#".str_pad(ord($char), 3, '0', STR_PAD_LEFT).";"; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
*/ |
|
115
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.