Skip to content
Closed
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
16 changes: 15 additions & 1 deletion src/Edge/NativeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -1916,8 +1916,22 @@ protected function dispatchNativeEvent(array $event): void
}

if (is_array($payload)) {
$reflected = (new \ReflectionMethod($this, $method))->getParameters();

// A lone `array $payload` that names no payload key wants the
// whole payload. Named binding would find nothing for it and the
// container cannot resolve a bare array.
if (count($reflected) === 1
&& $reflected[0]->getType() instanceof \ReflectionNamedType
&& $reflected[0]->getType()->getName() === 'array'
&& ! array_key_exists($reflected[0]->getName(), $payload)) {
ComponentMethodInvoker::invoke($this, $method, [$reflected[0]->getName() => $payload]);

return;
}

$parameters = [];
foreach ((new \ReflectionMethod($this, $method))->getParameters() as $parameter) {
foreach ($reflected as $parameter) {
if (array_key_exists($parameter->getName(), $payload)) {
$parameters[$parameter->getName()] = $this->coerceNativePayloadValue(
$parameter,
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/Edge/NativeEventPayloadBindingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Native\Mobile\Testing\Native;
use Tests\Fixtures\Edge\PayloadListenerScreen;

beforeEach(function () {
app('view')->addLocation(__DIR__.'/../../Fixtures/views');
});

it('passes the whole payload to a single array parameter that names no payload key', function () {
Native::test(PayloadListenerScreen::class)
->emitNative('WholePayloadReceived', ['message' => 'hi', 'id' => 7])
->assertSet('wholePayload', ['message' => 'hi', 'id' => 7]);
});

it('keeps binding a single array parameter by name when the payload has that key', function () {
Native::test(PayloadListenerScreen::class)
->emitNative('ItemsReceived', ['items' => ['a', 'b'], 'total' => 2])
->assertSet('items', ['a', 'b']);
});
33 changes: 33 additions & 0 deletions tests/Fixtures/Edge/PayloadListenerScreen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Tests\Fixtures\Edge;

use Illuminate\View\View;
use Native\Mobile\Attributes\On;
use Native\Mobile\Edge\NativeComponent;

class PayloadListenerScreen extends NativeComponent
{
/** @var array<string, mixed> */
public array $wholePayload = [];

/** @var list<string> */
public array $items = [];

#[On('WholePayloadReceived')]
public function onWholePayload(array $payload): void
{
$this->wholePayload = $payload;
}

#[On('ItemsReceived')]
public function onItems(array $items): void
{
$this->items = $items;
}

public function render(): View
{
return view('payload-listener-screen');
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/views/payload-listener-screen.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<native:column>
<native:text>{{ count($wholePayload) }} / {{ count($items) }}</native:text>
</native:column>
Loading