提交 9aa5eda9 authored 作者: Mitch Capper's avatar Mitch Capper
<?php
/**
* @todo add unit tests for all methods in all classes
* @author Raymond Chandler (intralanman@gmail.com)
......@@ -10,7 +11,8 @@
* @author Raymond Chandler (intralanman@gmail.com)
*
*/
class NotImplementedException extends Exception {
class NotImplementedException extends Exception {
}
/**
......@@ -19,65 +21,69 @@
*
*/
class NullDataException extends Exception {
}
class MissingRequiredAttribute extends Exception {
}
class phttapi {
private $logptr;
public $xml;
public function __construct() {
$this->logptr = fopen( '/tmp/phttapi.log', 'a' );
$this->dlog( print_r( $_REQUEST, true ) );
$this->logptr = fopen('/tmp/phttapi.log', 'a');
$this->dlog(print_r($_REQUEST, true));
$this->xml = new XMLWriter();
$this->xml->openMemory();
$this->xml->setIndent( true );
$this->xml->setIndentString( " " );
$this->open( 'document' );
$this->attr( 'type', 'text/freeswitch-httapi' );
$this->xml->setIndent(true);
$this->xml->setIndentString(" ");
$this->open('document');
$this->attr('type', 'text/freeswitch-httapi');
}
public function output() {
$this->xml->endElement(); // </document>
$xml = $this->xml->outputMemory();
$this->dlog( $xml );
$this->dlog($xml);
return $xml;
}
public function open( $tag ) {
$this->xml->startElement( $tag );
public function open($tag) {
$this->xml->startElement($tag);
}
public function text( $text ) {
$this->xml->text( $text );
public function text($text) {
$this->xml->text($text);
}
public function close() {
$this->xml->endElement();
}
public function attr( $key, $val ) {
$this->xml->writeAttribute( $key, $val );
public function attr($key, $val) {
$this->xml->writeAttribute($key, $val);
}
public function raw( $xml ) {
$this->xml->writeRaw( $xml );
public function raw($xml) {
$this->xml->writeRaw($xml);
}
public function comment( $comment ) {
$this->xml->writeComment( $comment );
public function comment($comment) {
$this->xml->writeComment($comment);
}
public function dlog( $debug = null ) {
fwrite( $this->logptr, $debug );
public function dlog($debug = null) {
fwrite($this->logptr, $debug);
}
public function start_work() {
$this->open( 'work' );
$this->open('work');
}
public function end_work() {
......@@ -85,12 +91,12 @@ class phttapi {
}
public function start_variables() {
$this->open( 'variables' );
$this->open('variables');
}
public function add_variable( $var, $val ) {
$this->open( $var );
$this->text( $val );
public function add_variable($var, $val) {
$this->open($var);
$this->text($val);
$this->close();
}
......@@ -99,12 +105,12 @@ class phttapi {
}
public function start_params() {
$this->open( 'params' );
$this->open('params');
}
public function add_param( $param, $value ) {
$this->open( $param );
$this->text( $value );
public function add_param($param, $value) {
$this->open($param);
$this->text($value);
$this->close();
}
......@@ -112,37 +118,38 @@ class phttapi {
$this->close();
}
public function add_action( $action ) {
if ( is_object( $action ) && $action instanceof phttapi_action ) {
if ( property_exists( $action, 'action_name' ) && $action->action_name ) {
public function add_action($action) {
if (is_object($action) && $action instanceof phttapi_action) {
if (property_exists($action, 'action_name') && $action->action_name) {
$action_name = $action->action_name;
} else {
$action_name = preg_replace( '/^phttapi_/', '', get_class( $action ) );
$action_name = preg_replace('/^phttapi_/', '', get_class($action));
}
$this->open( $action_name );
$this->open($action_name);
foreach ( $action->defined_attributes as $attr_name => $attr_value ) {
$this->attr( $attr_name, $attr_value );
foreach ($action->defined_attributes as $attr_name => $attr_value) {
$this->attr($attr_name, $attr_value);
}
if ( property_exists( $action, 'bindings' ) ) {
foreach ( $action->bindings as $binding ) {
$this->open( 'bind' );
if ( $binding->strip ) {
$this->attr( 'strip', $binding->strip );
if (property_exists($action, 'bindings')) {
foreach ($action->bindings as $binding) {
$this->open('bind');
if ($binding->strip) {
$this->attr('strip', $binding->strip);
}
$this->text( $binding->match );
$this->text($binding->match);
$this->close();
}
}
if ( $action->action_text ) {
$this->text( $action->action_text );
if ($action->action_text) {
$this->text($action->action_text);
}
$this->close();
}
}
}
/**
......@@ -152,62 +159,58 @@ class phttapi {
*
*/
class phttapi_action {
public $action_text = null;
public $action_name = null;
public $attributes = array ();
public $defined_attributes = array ();
public $required_attributes = array ();
public function __call( $method, $args ) {
$attribute = preg_replace( '/_/', '-', $method );
if ( property_exists( $this, 'attributes' ) && is_array( $this->attributes ) && array_key_exists( $attribute, $this->attributes ) && ! empty( $this->attributes [$attribute] ) ) {
if ( ! count( $args ) ) {
throw new NullDataException( "$method requires an argument" );
public $attributes = array();
public $defined_attributes = array();
public $required_attributes = array();
public function __call($method, $args) {
$attribute = preg_replace('/_/', '-', $method);
if (property_exists($this, 'attributes') && is_array($this->attributes)
&& array_key_exists($attribute, $this->attributes) && !empty($this->attributes [$attribute])) {
if (!count($args)) {
throw new NullDataException("$method requires an argument");
}
$value = $args [0];
$this->attr( $attribute, $value );
$this->attr($attribute, $value);
} else {
throw new NotImplementedException( sprintf( "%s doesn't implement %s", get_class( $this ), $method ) );
throw new NotImplementedException(sprintf("%s doesn't implement %s", get_class($this), $method));
}
}
public function attr( $attr_name, $attr_value ) {
public function attr($attr_name, $attr_value) {
$this->defined_attributes [$attr_name] = $attr_value;
}
public function action( $value ) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value );
}
public function temp_action( $value ) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value );
}
public function name( $value ) {
$this->attr( preg_replace( '/_/', '-', __FUNCTION__ ), $value );
public function name($value) {
$this->attr(preg_replace('/_/', '-', __FUNCTION__), $value);
}
public function output() {
if ( $this->action_text ) {
$this->text( $this->action_text );
if ($this->action_text) {
$this->text($this->action_text);
}
}
}
class phttapi_action_binding {
public $match;
public $strip;
public function __construct( $text = null ) {
if ( ! $text ) {
throw new NullDataException( 'match digits must be passed' );
public function __construct($text = null) {
if (!$text) {
throw new NullDataException('match digits must be passed');
}
$this->match = $text;
}
public function strip( $value = NULL ) {
if ( $value ) {
public function strip($value = NULL) {
if ($value) {
$this->strip = $value;
} else {
return $this->strip;
......@@ -217,17 +220,20 @@ class phttapi_action_binding {
}
class phttapi_prompt extends phttapi_action {
public $bindings = array ();
public function add_binding( $binding ) {
if ( is_object( $binding ) && $binding instanceof phttapi_action_binding ) {
array_push( $this->bindings, &$binding );
public $bindings = array();
public function add_binding($binding) {
if (is_object($binding) && $binding instanceof phttapi_action_binding) {
array_push($this->bindings, $binding);
}
}
}
// ACTIONS
class phttapi_break extends phttapi_action {
}
/**
......@@ -237,12 +243,15 @@ class phttapi_break extends phttapi_action {
*
*/
class phttapi_conference extends phttapi_action {
public $attributes = array (
'profile' => true
public $attributes = array(
'profile' => true
);
}
class phttapi_continue extends phttapi_action {
}
/**
......@@ -255,19 +264,21 @@ class phttapi_continue extends phttapi_action {
*
*/
class phttapi_dial extends phttapi_action {
public $attributes = array (
'context' => true,
'dialplan' => true,
'caller_id_name' => true,
'caller_id_number' => true
public $attributes = array(
'context' => true,
'dialplan' => true,
'caller_id_name' => true,
'caller_id_number' => true
);
public function __construct( $text = null ) {
if ( ! $text ) {
throw new NullDataException( "no data passed" );
public function __construct($text = null) {
if (!$text) {
throw new NullDataException("no data passed");
}
$this->action_text = $text;
}
}
/**
......@@ -277,11 +288,15 @@ class phttapi_dial extends phttapi_action {
*
*/
class phttapi_execute extends phttapi_action {
public $attributes = array (
'application' => true
public $attributes = array(
'application' => true
);
}
if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
if (class_exists('PHPUnit_Framework_TestCase')) {
class phttapi_executeTest extends PHPUnit_Framework_TestCase {
/**
......@@ -295,10 +310,12 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
* @expectedException NotImplementedException
*/
public function testFileThrowsException() {
$e = new phttapi_execute( 'text' );
$e = new phttapi_execute('text');
$e->file();
}
}
}
/**
......@@ -307,17 +324,19 @@ if ( class_exists( 'PHPUnit_Framework_TestCase' ) ) {
*
*/
class phttapi_getVariable extends phttapi_action {
public $attributes = array (
'permanent' => true
public $attributes = array(
'permanent' => true
);
public function __construct( $var = null ) {
if ( ! $var ) {
throw new NullDataException( "getVariable must be instantiated with a variable name" );
public function __construct($var = null) {
if (!$var) {
throw new NullDataException("getVariable must be instantiated with a variable name");
} else {
$this->attr( 'name', $var );
$this->attr('name', $var);
}
}
}
/**
......@@ -327,9 +346,11 @@ class phttapi_getVariable extends phttapi_action {
*
*/
class phttapi_hangup extends phttapi_action {
public $attributes = array (
'cause' => true
public $attributes = array(
'cause' => true
);
}
/**
......@@ -340,10 +361,12 @@ class phttapi_hangup extends phttapi_action {
*
*/
class phttapi_log extends phttapi_action {
public $attributes = array (
'level' => true,
'clean' => true
public $attributes = array(
'level' => true,
'clean' => true
);
}
/**
......@@ -353,9 +376,11 @@ class phttapi_log extends phttapi_action {
*
*/
class phttapi_pause extends phttapi_action {
public $attributes = array (
'milliseconds' => true
public $attributes = array(
'milliseconds' => true
);
}
/**
......@@ -372,16 +397,18 @@ class phttapi_pause extends phttapi_action {
*
*/
class phttapi_playback extends phttapi_prompt {
public $attributes = array (
'file' => true,
'error-file' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true,
'name' => true,
'asr-engine' => true,
'asr-grammar' => true
public $attributes = array(
'file' => true,
'error-file' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true,
'name' => true,
'asr-engine' => true,
'asr-grammar' => true
);
}
/**
......@@ -390,18 +417,22 @@ class phttapi_playback extends phttapi_prompt {
* @method void digit_timeout
* @method void input_timeout
* @method void loops
* @method void beep_file
*
* @author Raymond Chandler (intralanman@gmail.com)
*
*/
class phttapi_record extends phttapi_prompt {
public $attributes = array (
'file' => true,
'error-file' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true
public $attributes = array(
'beep-file' => true,
'file' => true,
'error-file' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true
);
}
/**
......@@ -411,9 +442,11 @@ class phttapi_record extends phttapi_prompt {
*
*/
class phttapi_recordCall extends phttapi_action {
public $attributes = array (
'limit' => true
public $attributes = array(
'limit' => true
);
}
/**
......@@ -426,12 +459,14 @@ class phttapi_recordCall extends phttapi_action {
*
*/
class phttapi_say extends phttapi_prompt {
public $attributes = array (
'gender' => true,
'method' => true,
'type' => true,
'language' => true
public $attributes = array(
'gender' => true,
'method' => true,
'type' => true,
'language' => true
);
}
/**
......@@ -445,14 +480,16 @@ class phttapi_say extends phttapi_prompt {
*
*/
class phttapi_speak extends phttapi_prompt {
public $attributes = array (
'engine' => true,
'voice' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true,
'file' => true,
public $attributes = array(
'engine' => true,
'voice' => true,
'digit-timeout' => true,
'input-timeout' => true,
'loops' => true,
'file' => true,
);
}
/**
......@@ -462,15 +499,17 @@ class phttapi_speak extends phttapi_prompt {
*
*/
class phttapi_sms extends phttapi_action {
public $attributes = array (
'to' => true
public $attributes = array(
'to' => true
);
public function __construct( $text = null ) {
if ( ! $text ) {
throw new Exception( "no data passed" );
public function __construct($text = null) {
if (!$text) {
throw new Exception("no data passed");
}
}
}
/**
......@@ -484,13 +523,15 @@ class phttapi_sms extends phttapi_action {
*
*/
class phttapi_voicemail extends phttapi_action {
public $attributes = array (
'auth-only' => true,
'domain' => true,
'profile' => true,
'check' => true,
'id' => true
public $attributes = array(
'auth-only' => true,
'domain' => true,
'profile' => true,
'check' => true,
'id' => true
);
}
/**
......@@ -498,8 +539,6 @@ class phttapi_voicemail extends phttapi_action {
* but prove to be pretty useful
*/
/**
* phttapi_google_tts offers a simple way to use Google's TTS api to pull a sound file to be played
*
......@@ -507,22 +546,24 @@ class phttapi_voicemail extends phttapi_action {
*
*/
class phttapi_google_tts extends phttapi_playback {
private $lang = 'en';
public function __construct() {
$this->action_name = 'playback';
}
public function lang($lang) {
$this->lang = $lang;
}
public function text( $text ) {
public function text($text) {
$lang = $this->lang;
$text = preg_replace( '/\s/', '+', $text );
$text = preg_replace('/\s/', '+', $text);
$url = "http://(ext=mp3,user_agent='Mozilla/5.0 (X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1')translate.google.com/translate_tts?tl=$lang&q=$text";
$this->file( $url );
$this->file($url);
}
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论