Created
March 5, 2011 00:52
-
-
Save luciferous/855985 to your computer and use it in GitHub Desktop.
Generates Twiml using SimpleXmlElement.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright 2011 Neuman Vong. All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright notice, | |
* this list of conditions and the following disclaimer. | |
* | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in the | |
* documentation and/or other materials provided with the distribution. | |
* | |
*/ | |
class TwimlException extends Exception {} | |
/** | |
* Twiml response XML generator. | |
*/ | |
class Twiml { | |
protected $element; | |
/** | |
* Constructs a Twiml response. | |
* | |
* @param arg: | |
* - SimpleXmlElement The element to wrap | |
* - array An array of attributes to add to the element | |
* - null Initialize an empty element named 'Response' | |
*/ | |
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'); | |
} | |
} | |
/** | |
* Converts method calls into Twiml verbs. | |
* | |
* An basic example: | |
* | |
* php> print $this->say('hello'); | |
* <Say>hello</Say> | |
* | |
* An example with attributes: | |
* | |
* php> print $this->say('hello', array('voice' => 'woman')); | |
* <Say voice="woman">hello</Say> | |
* | |
* You could even just pass in an attributes array, omitting the noun: | |
* | |
* php> print $this->gather(array('timeout' => '20')); | |
* <Gather timeout="20"/> | |
* | |
* @param verb string The Twiml verb. | |
* @param args array: | |
* - (noun string) | |
* - (noun string, attributes array) | |
* - (attributes array) | |
*/ | |
public function __call($verb, array $args) { | |
list($noun, $attrs) = $args + array('', array()); | |
if (is_array($noun)) { | |
list($attrs, $noun) = array($noun, ''); | |
} | |
$child = empty($noun) | |
? $this->element->addChild(ucfirst($verb)) | |
: $this->element->addChild(ucfirst($verb), $noun); | |
foreach ($attrs as $name => $value) { | |
$child->addAttribute($name, $value); | |
} | |
return new self($child); | |
} | |
/** | |
* Returns the object as XML. | |
*/ | |
public function __toString() { | |
return $this->element->asXml(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TwimlTest extends PHPUnit_Framework_TestCase { | |
function testStuff() { | |
$r = new Twiml(); | |
$r->say('hello'); | |
$r->dial()->number('123', array('sendDigits' => '456')); | |
$r->gather(array('timeout' => 15)); | |
$doc = simplexml_load_string($r); | |
$this->assertEquals('Response', $doc->getName()); | |
$this->assertEquals('hello', (string)$doc->Say); | |
$this->assertEquals('456', (string)$doc->Dial->Number['sendDigits']); | |
$this->assertEquals('123', (string)$doc->Dial->Number); | |
$this->assertEquals('15', (string)$doc->Gather['timeout']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment