This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXMLGenerator.php
122 lines (106 loc) · 3.86 KB
/
XMLGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
class XMLGenerator
{
private $xmlSettings;
private $tabIndex = -1;
private $xmlString;
private $parentsNotEnded = "/";
public function __construct($xmlSettings = null)
{
$this->xmlString = "";
if ($xmlSettings == null)
{
$this->xmlSettings = new XMLSettings();
}
else
{
$this->xmlSettings = $xmlSettings;
}
}
public function writeStartDocument()
{
$this->tabIndex = -1;
$this->xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
$this->xmlString .= $this->xmlSettings->getNewLineChars();
}
public function writeStartElement($startElement)
{
$this->parentsNotEnded .= $startElement . "/";
$this->xmlString .= sprintf("%s<%s>%s", $this->getTabs(), $startElement, $this->xmlSettings->getNewLineChars());
$this->tabIndex++;
}
public function writeElementString($attribute, $value)
{
$this->tabIndex++;
$this->xmlString .= sprintf("%s<%s>%s</%s>%s", $this->getTabs(), $attribute, $value, $attribute, $this->xmlSettings->getNewLineChars());
$this->tabIndex--;
}
public function writeXmlTagWithSingleAttribute($tagName, $attribute, $attributeValue, $value = null)
{
$dictionary = new Dictionary();
$dictionary->add($attribute, $attributeValue);
$this->writeXmlTagWithAttributeArray($tagName, $dictionary->getDictionary(), $value);
}
public function writeXmlTagWithAttributeArray($tagName, $attributeDictionary, $value = null)
{
$this->tabIndex++;
$this->xmlString .= sprintf("%s<%s", $this->getTabs(), $tagName);
foreach ($attributeDictionary as $attribute)
{
$this->xmlString .= sprintf(" %s=\"%s\"", $attribute->getKey(), $attribute->getValue());
}
if (empty($value))
{
$this->xmlString .= " />";
}
else
{
$this->xmlString .= sprintf(">%s</%s>", $value, $tagName);
}
$this->xmlString .= $this->xmlSettings->getNewLineChars();
$this->tabIndex--;
}
public function writeEndElement()
{
$parents = explode("/", $this->parentsNotEnded);
$parents = array_values(array_filter($parents));
$lastElement = count($parents)-1;
$this->xmlString .= sprintf("%s</%s>%s", $this->getTabs(), $parents[$lastElement], $this->xmlSettings->getNewLineChars());
$this->parentsNotEnded = str_replace("/" . $parents[$lastElement] . "/", "/", $this->parentsNotEnded);
$this->tabIndex--;
}
public function addCustomLineToXML($xmlString)
{
$this->xmlString .= $xmlString . $this->xmlSettings->getNewLineChars();
}
private function getTabs()
{
$tab = "";
for ($i = 0; $i < $this->tabIndex; $i++)
{
$tab .= "\t";
}
return $tab;
}
public function returnXmlString()
{
return trim($this->xmlString);
}
}
class XMLSettings
{
private $indent = false;
private $newLineChars = "\n";
public function getIndent()
{
return $this->indent;
}
public function setNewLineChars($newLineChar)
{
$this->newLineChars($newLineChar);
}
public function getNewLineChars()
{
return $this->newLineChars;
}
}