Skip to content

Commit f983179

Browse files
committed
Add Response::getCommandOutput() helper
1 parent 8cf23ad commit f983179

5 files changed

Lines changed: 23 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ This is a shortcut to get the value of the "ActionID" field.
183183
The `Response` value object represents the incoming response received from the AMI.
184184
It shares all properties of the [`Message`](#message) parent class.
185185

186+
The `getCommandOutput()` method can be used to get the resulting output of
187+
a "command" [`Action`](#action).
188+
This value is only available if this is actually a response to a "command" action,
189+
otherwise it defaults to `null`.
190+
191+
```php
192+
$sender->command('help')->then(function (Response $response) {
193+
echo $response->getCommandOutput();
194+
});
195+
```
196+
186197
#### Action
187198

188199
The `Action` value object represents an outgoing action message to be sent to the AMI.

examples/commands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
$api->command($line)->then(
3636
function (Response $response) {
37-
echo $response->getFieldValue('_') . PHP_EOL;
37+
echo $response->getCommandOutput() . PHP_EOL;
3838
},
3939
function (Exception $error) use ($line) {
4040
echo 'Error executing "' . $line . '": ' . $error->getMessage() . PHP_EOL;

src/Protocol/Parser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function parseMessage($message)
4747
foreach ($lines as $i => $line) {
4848
$pos = strlen($line) - self::LCOMMAND_END - 1;
4949
if ($i === $last && substr($line, -self::LCOMMAND_END) === self::COMMAND_END && ($pos < 0 || $line[$pos] === "\n")) {
50-
$key = '_';
50+
$key = Response::FIELD_COMMAND_OUTPUT;
5151
$value = $line;
5252
} else {
5353
$pos = strpos($line, ': ');

src/Protocol/Response.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44

55
class Response extends Message
66
{
7+
/** @internal */
8+
const FIELD_COMMAND_OUTPUT = '_';
9+
710
public function __construct(array $fields)
811
{
912
$this->fields = $fields;
1013
}
14+
15+
public function getCommandOutput()
16+
{
17+
return $this->getFieldValue(self::FIELD_COMMAND_OUTPUT);
18+
}
1119
}

tests/Protocol/ParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function testParsingCommandResponse()
7878

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

8484
public function testParsingCommandResponseEmpty()
@@ -94,7 +94,7 @@ public function testParsingCommandResponseEmpty()
9494

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

100100
public function testParsingResponseIsNotCommandResponse()

0 commit comments

Comments
 (0)