@@ -59,7 +59,7 @@ class XMLWriter
5959 * @param int $pTemporaryStorage Temporary storage location
6060 * @param string $pTemporaryStorageDir Temporary storage folder
6161 */
62- public function __construct ($ pTemporaryStorage = self ::STORAGE_MEMORY , $ pTemporaryStorageDir = './ ' )
62+ public function __construct ($ pTemporaryStorage = self ::STORAGE_MEMORY , $ pTemporaryStorageDir = './ ' , $ compatibility = false )
6363 {
6464 // Create internal XMLWriter
6565 $ this ->xmlWriter = new \XMLWriter ();
@@ -75,8 +75,13 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora
7575 $ this ->xmlWriter ->openUri ($ this ->tempFileName );
7676 }
7777
78- // Set default values
79- $ this ->xmlWriter ->setIndent (true );
78+ if ($ compatibility ) {
79+ $ this ->xmlWriter ->setIndent (false );
80+ $ this ->xmlWriter ->setIndentString ('' );
81+ } else {
82+ $ this ->xmlWriter ->setIndent (true );
83+ $ this ->xmlWriter ->setIndentString (' ' );
84+ }
8085 }
8186
8287 /**
@@ -126,4 +131,66 @@ public function getData()
126131 return file_get_contents ($ this ->tempFileName );
127132 }
128133 }
134+
135+
136+ /**
137+ * Write simple element and attribute(s) block
138+ *
139+ * There are two options:
140+ * 1. If the `$attributes` is an array, then it's an associative array of attributes
141+ * 2. If not, then it's a simple attribute-value pair
142+ *
143+ * @param string $element
144+ * @param string|array $attributes
145+ * @param string $value
146+ * @return void
147+ */
148+ public function writeElementBlock ($ element , $ attributes , $ value = null )
149+ {
150+ $ this ->xmlWriter ->startElement ($ element );
151+ if (!is_array ($ attributes )) {
152+ $ attributes = array ($ attributes => $ value );
153+ }
154+ foreach ($ attributes as $ attribute => $ value ) {
155+ $ this ->xmlWriter ->writeAttribute ($ attribute , $ value );
156+ }
157+ $ this ->xmlWriter ->endElement ();
158+ }
159+
160+ /**
161+ * Write element if ...
162+ *
163+ * @param bool $condition
164+ * @param string $element
165+ * @param string $attribute
166+ * @param mixed $value
167+ * @return void
168+ */
169+ public function writeElementIf ($ condition , $ element , $ attribute = null , $ value = null )
170+ {
171+ if ($ condition == true ) {
172+ if (is_null ($ attribute )) {
173+ $ this ->xmlWriter ->writeElement ($ element , $ value );
174+ } else {
175+ $ this ->xmlWriter ->startElement ($ element );
176+ $ this ->xmlWriter ->writeAttribute ($ attribute , $ value );
177+ $ this ->xmlWriter ->endElement ();
178+ }
179+ }
180+ }
181+
182+ /**
183+ * Write attribute if ...
184+ *
185+ * @param bool $condition
186+ * @param string $attribute
187+ * @param mixed $value
188+ * @return void
189+ */
190+ public function writeAttributeIf ($ condition , $ attribute , $ value )
191+ {
192+ if ($ condition == true ) {
193+ $ this ->xmlWriter ->writeAttribute ($ attribute , $ value );
194+ }
195+ }
129196}
0 commit comments