Twiml.php 4.3 KB

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