1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Classes en rapport avec xml |
4
|
|
|
* @author Simon Willison |
5
|
|
|
* @author (to php5) Vermeulen Maxime <[email protected]> |
6
|
|
|
* @version 1.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace BFW; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Permet de générer un fichier xml |
13
|
|
|
* @package bfw |
14
|
|
|
*/ |
15
|
|
|
class XmlWriterCustom implements \BFWInterface\IXmlWriterCustom |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var $_kernel L'instance du Kernel |
19
|
|
|
*/ |
20
|
|
|
protected $_kernel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var $xml Le contenu xml |
24
|
|
|
*/ |
25
|
|
|
protected $xml; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var $indent Par quoi on indente |
29
|
|
|
*/ |
30
|
|
|
protected $indent = ' '; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var $stack Array contenant les balises entourat les balises d'éléments |
34
|
|
|
*/ |
35
|
|
|
protected $stack = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructeur |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->_kernel = getKernel(); |
43
|
|
|
|
44
|
|
|
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Indente pour les "sous balise" |
49
|
|
|
*/ |
50
|
|
|
protected function _indent() |
51
|
|
|
{ |
52
|
|
|
for($i = 0, $j = count($this->stack); $i < $j; $i++) |
53
|
|
|
{ |
54
|
|
|
$this->xml .= $this->indent; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Créer une balise avec ces attributs (les balises principales, avec d'autres balise dedans en général) |
60
|
|
|
* |
61
|
|
|
* @param string $element Nom de la balise |
62
|
|
|
* @param array $attributes (default: array()) Les attributs de la balise |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function push($element, $attributes = array()) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$this->_indent(); |
67
|
|
|
$this->xml .= '<'.$element; |
68
|
|
|
|
69
|
|
|
foreach ($attributes as $key => $value) |
70
|
|
|
{ |
71
|
|
|
$this->xml .= ' '.$key.'="'.utf8_encode($value).'"'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->xml .= ">\n"; |
75
|
|
|
$this->stack[] = $element; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Créer une balise simple, avec ces attributs et son contenu |
80
|
|
|
* |
81
|
|
|
* @param string $element Nom de la balise |
82
|
|
|
* @param string $content Le contenu de la balise |
83
|
|
|
* @param array $attributes (default: array()) Les attributs de la balise |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function element($element, $content, $attributes = array()) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$this->_indent(); |
88
|
|
|
$this->xml .= '<'.$element; |
89
|
|
|
|
90
|
|
|
foreach ($attributes as $key => $value) |
91
|
|
|
{ |
92
|
|
|
$this->xml .= ' '.$key.'="'.utf8_encode($value).'"'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->xml .= '>'.utf8_encode($content).'</'.$element.'>'."\n"; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Créer une balise avec ![CDATA pour mettre du xhtml dedans |
100
|
|
|
* |
101
|
|
|
* @param string $element Nom de la balise |
102
|
|
|
* @param string $content Le contenu de la balise |
103
|
|
|
* @param array $attributes (default: array()) Les attributs de la balise |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function element_cdata($element, $content, $attributes = array()) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$this->_indent(); |
108
|
|
|
$this->xml .= '<'.$element; |
109
|
|
|
|
110
|
|
|
foreach ($attributes as $key => $value) |
111
|
|
|
{ |
112
|
|
|
$this->xml .= ' '.$key.'="'.utf8_encode($value).'"'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->xml .= '><![CDATA['.utf8_encode($content).']]></'.$element.'>'."\n"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Créer une balise autofermante |
120
|
|
|
* |
121
|
|
|
* @param string $element Nom de la balise |
122
|
|
|
* @param array $attributes (default: array()) Les attributs de la balise |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function emptyelement($element, $attributes = array()) |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$this->_indent(); |
127
|
|
|
$this->xml .= '<'.$element; |
128
|
|
|
|
129
|
|
|
foreach ($attributes as $key => $value) |
130
|
|
|
{ |
131
|
|
|
$this->xml .= ' '.$key.'="'.utf8_encode($value).'"'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->xml .= " />\n"; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Ferme une balise ouverte avec push() |
139
|
|
|
*/ |
140
|
|
|
public function pop() |
141
|
|
|
{ |
142
|
|
|
$element = array_pop($this->stack); |
143
|
|
|
$this->_indent(); |
144
|
|
|
$this->xml .= "</$element>\n"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retourne le résultat du xml |
149
|
|
|
* |
150
|
|
|
* @return string Le xml |
151
|
|
|
*/ |
152
|
|
|
public function getXml() |
153
|
|
|
{ |
154
|
|
|
return $this->xml; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.