13 public function __construct(array $options = array())
26 "encoding" => "UTF-8",
29 $options = array_merge_recursive($default, $options);
31 $this->comments = $options["comments"];
32 $this->cdata = $options["cdata"];
33 $this->attribute = $options["attribute"];
34 $this->version = $options["version"];
35 $this->encoding = $options["encoding"];
38 private function getFirstItem(&$array)
40 if (count($array) !== 1) {
44 $element = array_keys($array)[0];
46 return $array[$element];
49 private function arrayIsNumeric(&$array)
51 if (count($array) !== 1) {
55 $keys = array_keys($this->getFirstItem($array));
57 foreach ($keys as $key) {
58 if ( !is_numeric($key)) {
66 private function arrayIsComment(&$array)
68 if (count($array) !== 1) {
72 $key = array_keys($array)[0];
74 return in_array($key, $this->comments);
77 private function arrayIsCData(&$array)
79 if (count($array) !== 1) {
83 $key = array_keys($array)[0];
85 return in_array($key, $this->cdata);
88 private function escapeCData(&$value)
90 $cdataEsc = '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>';
92 return htmlentities($cdataEsc);
95 private function getAttributes(&$array)
97 $attributes = array();
99 if (is_array($array) && array_key_exists($this->attribute, $array)) {
100 $attributes = $array[$this->attribute];
102 unset($array[$this->attribute]);
104 $value = array_shift($array);
110 foreach ($attributes as $name => $val) {
111 $val = htmlentities($val, ENT_QUOTES);
113 $attributeStr .= " $name='$val'";
116 return ltrim($attributeStr);
119 private function dataArray2xml(&$array, $element)
123 foreach ($array as $index => $value) {
124 $attribute = $this->getAttributes($value);
126 if (is_array($value) && $this->arrayIsComment($value)) {
127 $xml .= "<!-- " . htmlentities(array_values($value)[0]) . " -->";
128 } else if (is_array($value) && !empty($value)) {
129 $xml .= "<$element $attribute>" . $this->array2xml($value) . "</$element>";
130 } elseif (empty($value)) {
131 $xml .= "<$element $attribute/>";
133 $xml .= "<$element $attribute>" . htmlentities($value) . "</$element>";
140 private function array2xml(&$array, $prevElement = "")
144 foreach ($array as $element => $value) {
145 if (is_numeric($element) && empty($value)) {
149 if (is_numeric($element) && !empty($prevElement)) {
150 $element = $prevElement;
153 if (is_numeric($element)) {
154 $element = "_$element";
157 $element = htmlentities($element);
159 $attribute = $this->getAttributes($value);
162 $xml .= "<$element $attribute/>";
163 } else if (is_array($value) && $this->arrayIsComment($value)) {
164 $xml .= "<!-- " . htmlentities(array_values($value)[0]) . " -->";
165 } else if (is_array($value) && $this->arrayIsCData($value)) {
166 $xml .= "<$element $attribute><![CDATA[" . $this->escapeCData(array_values($value)[0]) . "]]></$element>";
167 } else if (is_array($value) && $this->arrayIsNumeric($array)) {
168 $xml = $this->dataArray2xml($value, $element);
169 } else if (is_array($value) && !empty($value)) {
170 $xml .= "<$element $attribute>" . $this->array2xml($value, $element) . "</$element>";
172 $xml .= "<$element $attribute>" . htmlentities($value, ENT_QUOTES) . "</$element>";
179 public function createXml(&$data, $root = "root")
181 $array = array($root => $data);
182 $xmlStr = $this->array2xml($array);
184 simplexml_load_string($xmlStr);
186 return "<?xml version='{$this->version}' encoding='{$this->encoding}'?>$xmlStr";