1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\AnyDataset\Core\Formatter; |
4
|
|
|
|
5
|
|
|
use ByJG\AnyDataset\Core\GenericIterator; |
6
|
|
|
use ByJG\Util\XmlUtil; |
7
|
|
|
|
8
|
|
|
class XmlFormatter extends BaseFormatter |
9
|
|
|
{ |
10
|
4 |
|
protected function anydatasetXml($collection) |
11
|
|
|
{ |
12
|
4 |
|
$anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset></anydataset>"); |
13
|
4 |
|
$nodeRoot = $anyDataSet->getElementsByTagName("anydataset")->item(0); |
14
|
4 |
|
foreach ($collection as $sr) { |
15
|
4 |
|
$row = $this->rowXml($sr); |
16
|
4 |
|
$nodeRow = $row->getElementsByTagName("row")->item(0); |
17
|
4 |
|
$newRow = XmlUtil::createChild($nodeRoot, "row"); |
18
|
4 |
|
XmlUtil::addNodeFromNode($newRow, $nodeRow); |
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
return $anyDataSet; |
22
|
|
|
} |
23
|
|
|
|
24
|
5 |
|
protected function rowXml($row) |
25
|
|
|
{ |
26
|
5 |
|
$node = XmlUtil::createXmlDocumentFromStr("<row></row>"); |
27
|
5 |
|
$root = $node->getElementsByTagName("row")->item(0); |
28
|
5 |
|
foreach ($row as $key => $value) { |
29
|
5 |
|
if (!is_array($value)) { |
30
|
5 |
|
$field = XmlUtil::createChild($root, "field", $value); |
31
|
5 |
|
XmlUtil::addAttribute($field, "name", $key); |
32
|
|
|
} else { |
33
|
1 |
|
foreach ($value as $valueItem) { |
34
|
1 |
|
$field = XmlUtil::createChild($root, "field", $valueItem); |
35
|
1 |
|
XmlUtil::addAttribute($field, "name", $key); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
5 |
|
return $node; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns the AnyDataset XmlDocument representive object |
45
|
|
|
* |
46
|
|
|
* @return \DOMDocument XmlDocument object |
47
|
|
|
* @throws \ByJG\Util\Exception\XmlUtilException |
48
|
|
|
*/ |
49
|
5 |
|
public function raw() |
50
|
|
|
{ |
51
|
5 |
|
if ($this->object instanceof GenericIterator) { |
52
|
4 |
|
return $this->anydatasetXml($this->object->toArray()); |
53
|
|
|
} |
54
|
1 |
|
return $this->rowXml($this->object->toArray()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
3 |
|
public function toText() |
59
|
|
|
{ |
60
|
3 |
|
return $this->raw()->saveXML(); |
61
|
|
|
} |
62
|
|
|
} |