Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ArrayObject in Message Body #1288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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