Skip to content

Commit

Permalink
Handle ArrayObject in Message Body
Browse files Browse the repository at this point in the history
  • Loading branch information
amayer5125 committed Mar 20, 2023
1 parent 9324a7f commit c3bbe88
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pkg/enqueue/Client/Extension/PrepareBodyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Client\Extension;

use ArrayObject;
use Enqueue\Client\Message;
use Enqueue\Client\PreSend;
use Enqueue\Client\PreSendCommandExtensionInterface;
Expand All @@ -28,7 +29,14 @@ private function prepareBody(Message $message): void
if (is_scalar($body) || null === $body) {
$contentType = $contentType ?: 'text/plain';
$body = (string) $body;
} elseif (is_array($body)) {
} elseif (is_array($body) || $body instanceof ArrayObject) {
// convert ArrayObjects to arrays
array_walk_recursive($body, function (&$value) {
if ($value instanceof ArrayObject) {
$value = (array) $value;
}
});

// only array of scalars is allowed.
array_walk_recursive($body, function ($value) {
if (!is_scalar($value) && null !== $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Tests\Client\Extension;

use ArrayObject;
use Enqueue\Client\DriverInterface;
use Enqueue\Client\Extension\PrepareBodyExtension;
use Enqueue\Client\Message;
Expand Down Expand Up @@ -120,6 +121,10 @@ public static function provideMessages()

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

yield [new ArrayObject(['foo' => 'fooVal']), null, '{"foo":"fooVal"}', 'application/json'];

yield [['foo' => 'fooVal', 'bar' => new ArrayObject(['barOne', 'barTwo'])], null, '{"foo":"fooVal","bar":["barOne","barTwo"]}', 'application/json'];

yield [new JsonSerializableObject(), null, '{"foo":"fooVal"}', 'application/json'];

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

0 comments on commit c3bbe88

Please sign in to comment.