forked from twilio/twilio-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwiML.php
136 lines (120 loc) · 3.46 KB
/
TwiML.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Twilio\TwiML;
use DOMDocument;
use DOMElement;
/**
* @property $name string XML element name
* @property $attributes array XML attributes
* @property $value string XML body
* @property $children TwiML[] nested TwiML elements
*/
abstract class TwiML {
protected $name;
protected $attributes;
protected $children;
/**
* TwiML constructor.
*
* @param string $name XML element name
* @param string $value XML value
* @param array $attributes XML attributes
*/
public function __construct(string $name, ?string $value = null, array $attributes = []) {
$this->name = $name;
$this->attributes = $attributes;
$this->children = [];
if ($value !== null) {
$this->children[] = $value;
}
}
/**
* Add a TwiML element.
*
* @param TwiML|string $twiml TwiML element to add
* @return TwiML $this
*/
public function append($twiml): TwiML {
$this->children[] = $twiml;
return $this;
}
/**
* Add a TwiML element.
*
* @param TwiML $twiml TwiML element to add
* @return TwiML added TwiML element
*/
public function nest(TwiML $twiml): TwiML {
$this->children[] = $twiml;
return $twiml;
}
/**
* Set TwiML attribute.
*
* @param string $key name of attribute
* @param string $value value of attribute
* @return static $this
*/
public function setAttribute(string $key, string $value): TwiML {
$this->attributes[$key] = $value;
return $this;
}
/**
* @param string $name XML element name
* @param string $value XML value
* @param array $attributes XML attributes
* @return TwiML
*/
public function addChild(string $name, ?string $value = null, array $attributes = []): TwiML {
return $this->nest(new GenericNode($name, $value, $attributes));
}
/**
* Convert TwiML to XML string.
*
* @return string TwiML XML representation
*/
public function asXML(): string {
return (string)$this;
}
/**
* Convert TwiML to XML string.
*
* @return string TwiML XML representation
*/
public function __toString(): string {
return $this->xml()->saveXML();
}
/**
* Build TwiML element.
*
* @param TwiML $twiml TwiML element to convert to XML
* @param DOMDocument $document XML document for the element
* @return DOMElement $element
*/
private function buildElement(TwiML $twiml, DOMDocument $document): DOMElement {
$element = $document->createElement($twiml->name);
foreach ($twiml->attributes as $name => $value) {
if (\is_bool($value)) {
$value = ($value === true) ? 'true' : 'false';
}
$element->setAttribute($name, $value);
}
foreach ($twiml->children as $child) {
if (\is_string($child)) {
$element->appendChild($document->createTextNode($child));
} else {
$element->appendChild($this->buildElement($child, $document));
}
}
return $element;
}
/**
* Build XML element.
*
* @return DOMDocument Build TwiML element
*/
private function xml(): DOMDocument {
$document = new DOMDocument('1.0', 'UTF-8');
$document->appendChild($this->buildElement($this, $document));
return $document;
}
}