123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- <?php
- class Services_Twilio_TwimlException extends Exception {}
- class Services_Twilio_Twiml {
- protected $element;
-
- public function __construct($arg = null) {
- switch (true) {
- case $arg instanceof SimpleXmlElement:
- $this->element = $arg;
- break;
- case $arg === null:
- $this->element = new SimpleXmlElement('<Response/>');
- break;
- case is_array($arg):
- $this->element = new SimpleXmlElement('<Response/>');
- foreach ($arg as $name => $value) {
- $this->element->addAttribute($name, $value);
- }
- break;
- default:
- throw new TwimlException('Invalid argument');
- }
- }
-
- public function __call($verb, array $args)
- {
- list($noun, $attrs) = $args + array('', array());
- if (is_array($noun)) {
- list($attrs, $noun) = array($noun, '');
- }
-
- $decoded = html_entity_decode($noun, ENT_COMPAT, 'UTF-8');
- $normalized = htmlspecialchars($decoded, ENT_COMPAT, 'UTF-8', false);
- $child = empty($noun)
- ? $this->element->addChild(ucfirst($verb))
- : $this->element->addChild(ucfirst($verb), $normalized);
- foreach ($attrs as $name => $value) {
-
- if (is_bool($value)) {
- $value = ($value === true) ? 'true' : 'false';
- }
- $child->addAttribute($name, $value);
- }
- return new static($child);
- }
-
- public function __toString()
- {
- $xml = $this->element->asXml();
- return str_replace(
- '<?xml version="1.0"?>',
- '<?xml version="1.0" encoding="UTF-8"?>', $xml);
- }
- }
|