forked from youngguns-nl/moneybird_php_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleXMLElement.php
65 lines (56 loc) · 1.8 KB
/
SimpleXMLElement.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
<?php
/*
* SimpleXMLElement class file
*/
namespace Moneybird;
/**
* Extension for SimpleXMLElement
* @author Alexandre FERAUD
*/
class SimpleXMLElement extends \SimpleXMLElement {
/**
* Add CDATA text in a node
* @param string $cdata_text The CDATA value to add
*/
protected function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
/**
* Create a child with CDATA value
* @param string $name The name of the child element to add.
* @param string $cdata_text The CDATA value of the child element.
*/
public function addChildCData($name, $cdata_text) {
$child = $this->addChild($name);
$child->addCData($cdata_text);
}
/**
* Adds a child element to the XML node
* @param string $name The name of the child element to add.
* @param string $value [optional] If specified, the value of the child element.
* @param string $namespace [optional] If specified, the namespace to which the child element belongs.
* @return SimpleXMLElement The addChild method returns a SimpleXMLElement object representing the child added to the XML node.
*/
public function addChild ($key, $value = null, $namespace = null) {
return parent::addChild($key, str_replace('&', '&', $value), $namespace);
}
/**
* Add SimpleXMLElement code into a SimpleXMLElement
* @param SimpleXMLElement $append
*/
public function appendXML(SimpleXMLElement $append) {
if (strlen(trim((string) $append)) == 0) {
$xml = $this->addChild($append->getName());
foreach ($append->children() as $child) {
$xml->appendXML($child);
}
} else {
$xml = $this->addChild($append->getName(), (string) $append);
}
foreach ($append->attributes() as $n => $v) {
$xml->addAttribute($n, $v);
}
}
}