Skip to content

Commit

Permalink
Add answer to TROPO JSON helper library with support for RPID paramet…
Browse files Browse the repository at this point in the history
…ers - WebAPI PHP
  • Loading branch information
pengxli committed Aug 30, 2017
1 parent edd4f77 commit d56e70b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/AnswerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
use PHPUnit\Framework\TestCase;
require_once 'tropo.class.php';

class AnswerTest extends PHPUnit_Framework_TestCase
{

public function testCallWithMinOptions() {
$tropo = new Tropo();
$tropo->answer();
$params = array("name"=>"say");
$tropo->say("Hello, you were the first to answer.",$params);
$this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}');
}

public function testCallWithMinOptions1() {
$tropo = new Tropo();
$tropo->answer(null);
$params = array("name"=>"say");
$tropo->say("Hello, you were the first to answer.",$params);
$this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}');
}

public function testCallWithExtraToOptiions() {
$tropo = new Tropo();
$params = array(
'headers' => array(
'P-Header' => 'value goes here',
'Remote-Party-ID' => '"John Doe"<sip:[email protected]>;party=calling;id-type=subscriber;privacy=full;screen=yes'
));
$tropo->answer($params);
$params = array("name"=>"say");
$tropo->say("Hello, you were the first to answer.",$params);
$this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{"headers":{"P-Header":"value goes here","Remote-Party-ID":"\"John Doe\"<sip:[email protected]>;party=calling;id-type=subscriber;privacy=full;screen=yes"}}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}');
}

public function testCreateMinObject() {
$tropo = new Tropo();
$answer = new Answer();
$tropo->answer($answer);
$params = array("name"=>"say");
$tropo->say("Hello, you were the first to answer.",$params);
$this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}');
}

public function testCreateMinObject1() {
$tropo = new Tropo();
$headers = array(
'P-Header' => 'value goes here',
'Remote-Party-ID' => '"John Doe"<sip:[email protected]>;party=calling;id-type=subscriber;privacy=full;screen=yes'
);
$answer = new Answer($headers);
$tropo->answer($answer);
$params = array("name"=>"say");
$tropo->say("Hello, you were the first to answer.",$params);
$this->assertEquals(sprintf($tropo), '{"tropo":[{"answer":{"headers":{"P-Header":"value goes here","Remote-Party-ID":"\"John Doe\"<sip:[email protected]>;party=calling;id-type=subscriber;privacy=full;screen=yes"}}},{"say":[{"value":"Hello, you were the first to answer.","name":"say"}]}]}');
}

}
?>
48 changes: 48 additions & 0 deletions tropo.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,31 @@ public function generalLogSecurity($state) {
}
}

public function answer($answer=NULL) {
if (!isset($answer)) {
$answer = "{}";
} elseif ($answer instanceof Answer) {
} elseif (is_array($answer)) {

$params = $answer;
$p = array('headers');
foreach ($p as $option) {
$$option = null;
if (array_key_exists($option, $params)) {
$$option = $params[$option];
}
}
$answer = new Answer($headers);

} else {

throw new Exception("Argument 1 passed to Tropo::answer() must be a array or an instance of Answer.");

}
$this->answer = sprintf('%s', $answer);

}

/**
* Launches a new session with the Tropo Session API.
* (Pass through to SessionAPI class.)
Expand Down Expand Up @@ -2845,6 +2870,29 @@ public function __toString() {
}
}

class Answer extends BaseClass {

private $_headers;

/**
* Class constructor
*
* @param array $headers
*/
public function __construct(Array $headers=NULL) {
$this->_headers = $headers;
}

/**
* Renders object in JSON format.
*
*/
public function __toString() {
if(count($this->_headers)) { $this->headers = $this->_headers; }
return json_encode($this);
}
}

/**
* Defnies an endoint for transfer and redirects.
* @package TropoPHP_Support
Expand Down

0 comments on commit d56e70b

Please sign in to comment.