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

Add Inertia::deepMerge Method for Handling Complex Data Merges in Responses #679

Open
wants to merge 4 commits into
base: 2.x
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
1 change: 1 addition & 0 deletions src/Inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @method static \Inertia\DeferProp defer(callable $callback, string $group = 'default')
* @method static \Inertia\AlwaysProp always(mixed $value)
* @method static \Inertia\MergeProp merge(mixed $value)
* @method static \Inertia\MergeProp deepMerge(mixed $value)
* @method static \Inertia\Response render(string $component, array|\Illuminate\Contracts\Support\Arrayable $props = [])
* @method static \Symfony\Component\HttpFoundation\Response location(string|\Symfony\Component\HttpFoundation\RedirectResponse $url)
* @method static void macro(string $name, object|callable $macro)
Expand Down
15 changes: 15 additions & 0 deletions src/MergesProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ trait MergesProps
{
protected bool $merge = false;

protected bool $deepMerge = false;

public function merge(): static
{
$this->merge = true;
Expand All @@ -17,4 +19,17 @@ public function shouldMerge(): bool
{
return $this->merge;
}

public function deepMerge(): static
{

$this->deepMerge = true;

return $this->merge();
}

public function shouldDeepMerge(): bool
{
return $this->deepMerge;
}
}
20 changes: 19 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,31 @@ public function resolveMergeProps(Request $request): array
})
->filter(function ($prop) {
return $prop->shouldMerge();
});

$deepMergeProps = $mergeProps
->filter(function ($prop) {
return $prop->shouldDeepMerge();
})
->filter(function ($prop, $key) use ($resetProps) {
return ! $resetProps->contains($key);
})
->keys();

$mergeProps = $mergeProps
->filter(function ($prop) {
return !$prop->shouldDeepMerge();
})
->filter(function ($prop, $key) use ($resetProps) {
return ! $resetProps->contains($key);
})
->keys();

return $mergeProps->isNotEmpty() ? ['mergeProps' => $mergeProps->toArray()] : [];
$props = [];
if ($mergeProps->isNotEmpty()) $props['mergeProps'] = $mergeProps->toArray();
if ($deepMergeProps->isNotEmpty()) $props['deepMergeProps'] = $deepMergeProps->toArray();

return $props;
}

public function resolveDeferredProps(Request $request): array
Expand Down
8 changes: 8 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public function merge($value): MergeProp
return new MergeProp($value);
}

/**
* @param mixed $value
*/
public function deepMerge($value): MergeProp
{
return (new MergeProp($value))->deepMerge();
}

/**
* @param mixed $value
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/DeepMergePropTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Inertia\Tests;

use Illuminate\Http\Request;
use Inertia\MergeProp;

class DeepMergePropTest extends TestCase
{
public function test_can_invoke_with_a_callback(): void
{
$mergeProp = new MergeProp(function () {
return 'A merge prop value';
});
$mergeProp->deepMerge();

$this->assertSame('A merge prop value', $mergeProp());
}

public function test_can_invoke_with_a_non_callback(): void
{
$mergeProp = new MergeProp(['key' => 'value']);
$mergeProp->deepMerge();

$this->assertSame(['key' => 'value'], $mergeProp());
}

public function test_can_resolve_bindings_when_invoked(): void
{
$mergeProp = new MergeProp(function (Request $request) {
return $request;
});
$mergeProp->deepMerge();

$this->assertInstanceOf(Request::class, $mergeProp());
}
}
21 changes: 21 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ public function test_can_create_merged_prop(): void
$this->assertInstanceOf(MergeProp::class, $mergedProp);
}

public function test_can_create_deep_merged_prop(): void
{
$factory = new ResponseFactory;
$mergedProp = $factory->merge(function () {
return 'A merged value';
});
$mergedProp->deepMerge();

$this->assertInstanceOf(MergeProp::class, $mergedProp);
}

public function test_can_create_deferred_and_merged_prop(): void
{
$factory = new ResponseFactory;
Expand All @@ -266,6 +277,16 @@ public function test_can_create_deferred_and_merged_prop(): void
$this->assertInstanceOf(DeferProp::class, $deferredProp);
}

public function test_can_create_deferred_and_deep_merged_prop(): void
{
$factory = new ResponseFactory;
$deferredProp = $factory->defer(function () {
return 'A deferred + merged value';
})->deepMerge();

$this->assertInstanceOf(DeferProp::class, $deferredProp);
}

public function test_can_create_optional_prop(): void
{
$factory = new ResponseFactory;
Expand Down
75 changes: 75 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,41 @@ public function test_server_response_with_merge_props(): void
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;},&quot;foo&quot;:&quot;foo value&quot;,&quot;bar&quot;:&quot;bar value&quot;},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;mergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;]}"></div>', $view->render());
}

public function test_server_response_with_deep_merge_props(): void
{
$request = Request::create('/user/123', 'GET');

$user = ['name' => 'Jonathan'];
$response = new Response(
'User/Edit',
[
'user' => $user,
'foo' => (new MergeProp('foo value'))->deepMerge(),
'bar' => (new MergeProp('bar value'))->deepMerge(),
],
'app',
'123'
);
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$page = $view->getData()['page'];

$this->assertInstanceOf(BaseResponse::class, $response);
$this->assertInstanceOf(View::class, $view);

$this->assertSame('User/Edit', $page['component']);
$this->assertSame('Jonathan', $page['props']['user']['name']);
$this->assertSame('/user/123', $page['url']);
$this->assertSame('123', $page['version']);
$this->assertSame([
'foo',
'bar',
], $page['deepMergeProps']);
$this->assertFalse($page['clearHistory']);
$this->assertFalse($page['encryptHistory']);
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;},&quot;foo&quot;:&quot;foo value&quot;,&quot;bar&quot;:&quot;bar value&quot;},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;deepMergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;]}"></div>', $view->render());
}

public function test_server_response_with_defer_and_merge_props(): void
{
$request = Request::create('/user/123', 'GET');
Expand Down Expand Up @@ -205,6 +240,46 @@ public function test_server_response_with_defer_and_merge_props(): void
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;},&quot;bar&quot;:&quot;bar value&quot;},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;mergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;],&quot;deferredProps&quot;:{&quot;default&quot;:[&quot;foo&quot;]}}"></div>', $view->render());
}

public function test_server_response_with_defer_and_deep_merge_props(): void
{
$request = Request::create('/user/123', 'GET');

$user = ['name' => 'Jonathan'];
$response = new Response(
'User/Edit',
[
'user' => $user,
'foo' => (new DeferProp(function () {
return 'foo value';
}, 'default'))->deepMerge(),
'bar' => (new MergeProp('bar value'))->deepMerge(),
],
'app',
'123'
);
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$page = $view->getData()['page'];

$this->assertInstanceOf(BaseResponse::class, $response);
$this->assertInstanceOf(View::class, $view);

$this->assertSame('User/Edit', $page['component']);
$this->assertSame('Jonathan', $page['props']['user']['name']);
$this->assertSame('/user/123', $page['url']);
$this->assertSame('123', $page['version']);
$this->assertSame([
'default' => ['foo'],
], $page['deferredProps']);
$this->assertSame([
'foo',
'bar',
], $page['deepMergeProps']);
$this->assertFalse($page['clearHistory']);
$this->assertFalse($page['encryptHistory']);
$this->assertSame('<div id="app" data-page="{&quot;component&quot;:&quot;User\/Edit&quot;,&quot;props&quot;:{&quot;user&quot;:{&quot;name&quot;:&quot;Jonathan&quot;},&quot;bar&quot;:&quot;bar value&quot;},&quot;url&quot;:&quot;\/user\/123&quot;,&quot;version&quot;:&quot;123&quot;,&quot;clearHistory&quot;:false,&quot;encryptHistory&quot;:false,&quot;deepMergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;],&quot;deferredProps&quot;:{&quot;default&quot;:[&quot;foo&quot;]}}"></div>', $view->render());
}

public function test_xhr_response(): void
{
$request = Request::create('/user/123', 'GET');
Expand Down
Loading