Twiml.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Twilio;
  3. use Twilio\Exceptions\TwimlException;
  4. /**
  5. * Twiml response generator.
  6. */
  7. class Twiml {
  8. protected $element;
  9. /**
  10. * Constructs a Twiml response.
  11. *
  12. * @param \SimpleXmlElement|array $arg
  13. * @throws TwimlException
  14. * :param SimpleXmlElement|array $arg: Can be any of
  15. *
  16. * - the element to wrap
  17. * - attributes to add to the element
  18. * - if null, initialize an empty element named 'Response'
  19. */
  20. public function __construct($arg = null) {
  21. switch (true) {
  22. case $arg instanceof \SimpleXMLElement:
  23. $this->element = $arg;
  24. break;
  25. case $arg === null:
  26. $this->element = new \SimpleXMLElement('<Response/>');
  27. break;
  28. case is_array($arg):
  29. $this->element = new \SimpleXMLElement('<Response/>');
  30. foreach ($arg as $name => $value) {
  31. $this->element->addAttribute($name, $value);
  32. }
  33. break;
  34. default:
  35. throw new TwimlException('Invalid argument');
  36. }
  37. }
  38. /**
  39. * Converts method calls into Twiml verbs.
  40. *
  41. * A basic example:
  42. *
  43. * .. code-block:: php
  44. *
  45. * php> print $this->say('hello');
  46. * <Say>hello</Say>
  47. *
  48. * An example with attributes:
  49. *
  50. * .. code-block:: php
  51. *
  52. * print $this->say('hello', array('voice' => 'woman'));
  53. * <Say voice="woman">hello</Say>
  54. *
  55. * You could even just pass in an attributes array, omitting the noun:
  56. *
  57. * .. code-block:: php
  58. *
  59. * print $this->gather(array('timeout' => '20'));
  60. * <Gather timeout="20"/>
  61. *
  62. * @param string $verb The Twiml verb.
  63. * @param mixed[] $args
  64. * @return self
  65. * :param string $verb: The Twiml verb.
  66. * :param array $args:
  67. * - (noun string)
  68. * - (noun string, attributes array)
  69. * - (attributes array)
  70. *
  71. * :return: A SimpleXmlElement
  72. * :rtype: SimpleXmlElement
  73. */
  74. public function __call($verb, array $args) {
  75. list($noun, $attrs) = $args + array('', array());
  76. if (is_array($noun)) {
  77. list($attrs, $noun) = array($noun, '');
  78. }
  79. /* addChild does not escape XML, while addAttribute does. This means if
  80. * you pass unescaped ampersands ("&") to addChild, you will generate
  81. * an error.
  82. *
  83. * Some inexperienced developers will pass in unescaped ampersands, and
  84. * we want to make their code work, by escaping the ampersands for them
  85. * before passing the string to addChild. (with htmlentities)
  86. *
  87. * However other people will know what to do, and their code
  88. * already escapes ampersands before passing them to addChild. We don't
  89. * want to break their existing code by turning their &amp;'s into
  90. * &amp;amp;
  91. *
  92. * We also want to use numeric entities, not named entities so that we
  93. * are fully compatible with XML
  94. *
  95. * The following lines accomplish the desired behavior.
  96. */
  97. $decoded = html_entity_decode($noun, ENT_COMPAT, 'UTF-8');
  98. $normalized = htmlspecialchars($decoded, ENT_COMPAT, 'UTF-8', false);
  99. $hasNoun = is_scalar($noun) && strlen($noun);
  100. $child = $hasNoun
  101. ? $this->element->addChild(ucfirst($verb), $normalized)
  102. : $this->element->addChild(ucfirst($verb));
  103. if (is_array($attrs)) {
  104. foreach ($attrs as $name => $value) {
  105. /* Note that addAttribute escapes raw ampersands by default, so we
  106. * haven't touched its implementation. So this is the matrix for
  107. * addAttribute:
  108. *
  109. * & turns into &amp;
  110. * &amp; turns into &amp;amp;
  111. */
  112. if (is_bool($value)) {
  113. $value = ($value === true) ? 'true' : 'false';
  114. }
  115. $child->addAttribute($name, $value);
  116. }
  117. }
  118. return new static($child);
  119. }
  120. /**
  121. * Returns the object as XML.
  122. *
  123. * :return: The response as an XML string
  124. * :rtype: string
  125. */
  126. public function __toString() {
  127. $xml = $this->element->asXML();
  128. return (string)str_replace(
  129. '<?xml version="1.0"?>',
  130. '<?xml version="1.0" encoding="UTF-8"?>', $xml);
  131. }
  132. }