Skip to content

Commit

Permalink
Merge pull request #23 from clue-labs/command
Browse files Browse the repository at this point in the history
Add Response::getCommandOutput() helper
  • Loading branch information
clue committed Mar 31, 2015
2 parents 9515f90 + f983179 commit cbf0de4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ This is a shortcut to get the value of the "ActionID" field.
The `Response` value object represents the incoming response received from the AMI.
It shares all properties of the [`Message`](#message) parent class.

The `getCommandOutput()` method can be used to get the resulting output of
a "command" [`Action`](#action).
This value is only available if this is actually a response to a "command" action,
otherwise it defaults to `null`.

```php
$sender->command('help')->then(function (Response $response) {
echo $response->getCommandOutput();
});
```

#### Action

The `Action` value object represents an outgoing action message to be sent to the AMI.
Expand Down
2 changes: 1 addition & 1 deletion examples/commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

$sender->command($line)->then(
function (Response $response) {
echo $response->getFieldValue('_') . PHP_EOL;
echo $response->getCommandOutput() . PHP_EOL;
},
function (Exception $error) use ($line) {
echo 'Error executing "' . $line . '": ' . $error->getMessage() . PHP_EOL;
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function parseMessage($message)
foreach ($lines as $i => $line) {
$pos = strlen($line) - self::LCOMMAND_END - 1;
if ($i === $last && substr($line, -self::LCOMMAND_END) === self::COMMAND_END && ($pos < 0 || $line[$pos] === "\n")) {
$key = '_';
$key = Response::FIELD_COMMAND_OUTPUT;
$value = $line;
} else {
$pos = strpos($line, ': ');
Expand Down
8 changes: 8 additions & 0 deletions src/Protocol/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

class Response extends Message
{
/** @internal */
const FIELD_COMMAND_OUTPUT = '_';

public function __construct(array $fields)
{
$this->fields = $fields;
}

public function getCommandOutput()
{
return $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
}
}
4 changes: 2 additions & 2 deletions tests/Protocol/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testParsingCommandResponse()

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
$this->assertEquals('Follows', $first->getFieldValue('Response'));
$this->assertEquals("Testing: yes\nAnother Line\n--END COMMAND--", $first->getFieldValue('_'));
$this->assertEquals("Testing: yes\nAnother Line\n--END COMMAND--", $first->getCommandOutput());
}

public function testParsingCommandResponseEmpty()
Expand All @@ -94,7 +94,7 @@ public function testParsingCommandResponseEmpty()

$this->assertInstanceOf('Clue\React\Ami\Protocol\Response', $first);
$this->assertEquals('Follows', $first->getFieldValue('Response'));
$this->assertEquals("--END COMMAND--", $first->getFieldValue('_'));
$this->assertEquals("--END COMMAND--", $first->getCommandOutput());
}

public function testParsingResponseIsNotCommandResponse()
Expand Down

0 comments on commit cbf0de4

Please sign in to comment.