Skip to content

Commit c3bbe88

Browse files
committed
Handle ArrayObject in Message Body
1 parent 9324a7f commit c3bbe88

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

Diff for: pkg/enqueue/Client/Extension/PrepareBodyExtension.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Client\Extension;
44

5+
use ArrayObject;
56
use Enqueue\Client\Message;
67
use Enqueue\Client\PreSend;
78
use Enqueue\Client\PreSendCommandExtensionInterface;
@@ -28,7 +29,14 @@ private function prepareBody(Message $message): void
2829
if (is_scalar($body) || null === $body) {
2930
$contentType = $contentType ?: 'text/plain';
3031
$body = (string) $body;
31-
} elseif (is_array($body)) {
32+
} elseif (is_array($body) || $body instanceof ArrayObject) {
33+
// convert ArrayObjects to arrays
34+
array_walk_recursive($body, function (&$value) {
35+
if ($value instanceof ArrayObject) {
36+
$value = (array) $value;
37+
}
38+
});
39+
3240
// only array of scalars is allowed.
3341
array_walk_recursive($body, function ($value) {
3442
if (!is_scalar($value) && null !== $value) {

Diff for: pkg/enqueue/Tests/Client/Extension/PrepareBodyExtensionTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Tests\Client\Extension;
44

5+
use ArrayObject;
56
use Enqueue\Client\DriverInterface;
67
use Enqueue\Client\Extension\PrepareBodyExtension;
78
use Enqueue\Client\Message;
@@ -120,6 +121,10 @@ public static function provideMessages()
120121

121122
yield [['foo' => 'fooVal'], 'foo/bar', '{"foo":"fooVal"}', 'foo/bar'];
122123

124+
yield [new ArrayObject(['foo' => 'fooVal']), null, '{"foo":"fooVal"}', 'application/json'];
125+
126+
yield [['foo' => 'fooVal', 'bar' => new ArrayObject(['barOne', 'barTwo'])], null, '{"foo":"fooVal","bar":["barOne","barTwo"]}', 'application/json'];
127+
123128
yield [new JsonSerializableObject(), null, '{"foo":"fooVal"}', 'application/json'];
124129

125130
yield [new JsonSerializableObject(), 'foo/bar', '{"foo":"fooVal"}', 'foo/bar'];

0 commit comments

Comments
 (0)