Skip to content

Commit 2eea95d

Browse files
committed
Support serializing action keys with multiple values
1 parent 20f3d6a commit 2eea95d

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Protocol/Action.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ public function __construct($action, array $parts = array())
1717
public function getMessageSerialized()
1818
{
1919
$message = 'Action: ' . $this->action . "\r\n";
20-
foreach ($this->parts as $key => $value) {
21-
$message .= $key . ': ' . $value . "\r\n";
20+
foreach ($this->parts as $key => $values) {
21+
if (!is_array($values)) {
22+
$values = array($values);
23+
}
24+
foreach ($values as $i => $value) {
25+
if (!is_int($i)) {
26+
$value = $i . '=' . $value;
27+
}
28+
$message .= $key . ': ' . $value . "\r\n";
29+
}
2230
}
2331
$message .= "\r\n";
2432

tests/Protocol/ActionTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Clue\React\Ami\Protocol\Action;
4+
class ActionTest extends TestCase
5+
{
6+
public function testSerializeSimple()
7+
{
8+
$action = new Action('name');
9+
$id = $action->getActionId();
10+
11+
$this->assertEquals("Action: name\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
12+
}
13+
14+
public function testSerializeKeySingle()
15+
{
16+
$action = new Action('name', array('Key' => 'Value'));
17+
$id = $action->getActionId();
18+
19+
$this->assertEquals("Action: name\r\nKey: Value\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
20+
}
21+
22+
public function testSerializeKeyMultipleValues()
23+
{
24+
$action = new Action('name', array('Key' => array('Value1', 'Value2')));
25+
$id = $action->getActionId();
26+
27+
$this->assertEquals("Action: name\r\nKey: Value1\r\nKey: Value2\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
28+
}
29+
30+
public function testSerializeKeyMultipleKeyValues()
31+
{
32+
$action = new Action('name', array('Variables' => array('first' => 'on', 'second' => 'off')));
33+
$id = $action->getActionId();
34+
35+
$this->assertEquals("Action: name\r\nVariables: first=on\r\nVariables: second=off\r\nActionID: $id\r\n\r\n", $action->getMessageSerialized());
36+
}
37+
}

0 commit comments

Comments
 (0)