Skip to content

Commit f070933

Browse files
committed
Unify accessing fields with multiple values
1 parent 6891586 commit f070933

3 files changed

Lines changed: 61 additions & 4 deletions

File tree

src/Protocol/Action.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ class Action extends Message
66
{
77
public function __construct(array $fields = array())
88
{
9+
foreach ($fields as $key => &$value) {
10+
if (is_array($value)) {
11+
foreach ($value as $k => &$v) {
12+
if ($v === null) {
13+
unset($value[$k]);
14+
} elseif (!is_int($k)) {
15+
$v = $k . '=' . $v;
16+
}
17+
}
18+
$value = array_values($value);
19+
}
20+
21+
if ($value === null || $value === array()) {
22+
unset($fields[$key]);
23+
}
24+
}
925
$this->fields = $fields;
1026
}
1127

@@ -17,9 +33,6 @@ public function getMessageSerialized()
1733
$values = array($values);
1834
}
1935
foreach ($values as $i => $value) {
20-
if (!is_int($i)) {
21-
$value = $i . '=' . $value;
22-
}
2336
$message .= $key . ': ' . $value . "\r\n";
2437
}
2538
}

src/Protocol/Message.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,23 @@ public function getActionId()
1111
return $this->getFieldValue('ActionId');
1212
}
1313

14+
/**
15+
* Returns the first value for the field with the given $key
16+
*
17+
* @param string $key
18+
* @return string|NULL
19+
*/
1420
public function getFieldValue($key)
1521
{
1622
$key = strtolower($key);
1723

1824
foreach ($this->fields as $part => $value) {
1925
if (strtolower($part) === $key) {
20-
return $value;
26+
if (is_array($value)) {
27+
return reset($value);
28+
} else {
29+
return $value;
30+
}
2131
}
2232
}
2333

tests/Protocol/ActionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public function testIdCanBeSet()
1616
$action = new Action(array('ActionID' => '123'));
1717

1818
$this->assertEquals('123', $action->getActionId());
19+
20+
$this->assertEquals('123', $action->getFieldValue('ActionID'));
21+
$this->assertEquals('123', $action->getFieldValue('aCtIoNiD'));
22+
23+
$this->assertNull($action->getFieldValue('unknown'));
1924
}
2025

2126
public function testOneFieldValue()
@@ -32,17 +37,46 @@ public function testMultipleFieldsSingleValue()
3237
$this->assertEquals("Action: name\r\nKey: Value\r\n\r\n", $action->getMessageSerialized());
3338
}
3439

40+
public function testOneFieldNoValue()
41+
{
42+
$action = new Action(array('Key' => null));
43+
44+
$this->assertEquals("\r\n", $action->getMessageSerialized());
45+
46+
$this->assertNull($action->getFieldValue('Key'));
47+
}
48+
49+
public function testOneFieldNoValues()
50+
{
51+
$action = new Action(array('Key' => array()));
52+
53+
$this->assertEquals("\r\n", $action->getMessageSerialized());
54+
55+
$this->assertNull($action->getFieldValue('Key'));
56+
}
57+
3558
public function testOneFieldMultipleValues()
3659
{
3760
$action = new Action(array('Key' => array('Value1', 'Value2')));
3861

3962
$this->assertEquals("Key: Value1\r\nKey: Value2\r\n\r\n", $action->getMessageSerialized());
4063
}
4164

65+
public function testOneFieldMultipleValuesIgnoreNulls()
66+
{
67+
$action = new Action(array('Key' => array(null, 'value', null)));
68+
69+
$this->assertEquals("Key: value\r\n\r\n", $action->getMessageSerialized());
70+
71+
$this->assertEquals('value', $action->getFieldValue('Key'));
72+
}
73+
4274
public function testOneFieldMultipleKeyValues()
4375
{
4476
$action = new Action(array('Variables' => array('first' => 'on', 'second' => 'off')));
4577

4678
$this->assertEquals("Variables: first=on\r\nVariables: second=off\r\n\r\n", $action->getMessageSerialized());
79+
80+
$this->assertEquals('first=on', $action->getFieldValue('Variables'));
4781
}
4882
}

0 commit comments

Comments
 (0)