name = $name; $this->value = $value; $this->attributes = $attributes; $this->children = array(); } /** * Add a TwiML element. * * @param TwiML $twiml TwiML element to add * @return TwiML $this */ public function append($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) { $this->children[] = $twiml; return $twiml; } /** * Set TwiML attribute. * * @param string $key name of attribute * @param string $value value of attribute * @return TwiML $this */ public function setAttribute($key, $value) { return $this->attributes[$key] = $value; } /** * Convert TwiML to XML string. * * @return string TwiML XML representation */ public function asXML() { return $this->__toString(); } /** * Convert TwiML to XML string. * * @return string TwiML XML representation */ public function __toString() { return str_replace( '', '', $this->xml()->asXML() ); } /** * Build TwiML children. * * @param TwiML[] $children Children to build * @param \SimpleXMLElement $element Base XML element */ private function buildChildren($children, $element) { foreach ($children as $child) { $childElement = $element->addChild($child->name); self::buildElement($child, $childElement); self::buildChildren($child->children, $childElement); } } /** * Build TwiML element. * * @param TwiML $twiml TwiML element to build * @param \SimpleXMLElement $element Base XML element */ private function buildElement($twiml, $element) { if (is_string($twiml->value)) { $element[0] = $twiml->value; } foreach ($twiml->attributes as $name => $value) { if (is_bool($value)) { $value = ($value === true) ? 'true' : 'false'; } $element->addAttribute($name, $value); } } /** * Build XML element. * * @return \SimpleXMLElement Build TwiML element */ private function xml() { $element = new \SimpleXMLElement('<' . $this->name . '/>'); self::buildElement($this, $element); self::buildChildren($this->children, $element); return $element; } }