Skip to content

[2.x] Allow deepMerge on custom properties #732

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

Merged
merged 2 commits into from
Jun 13, 2025
Merged
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
4 changes: 3 additions & 1 deletion src/MergeProp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ class MergeProp implements Mergeable

/**
* @param mixed $value
* @param string[] $mergeStrategies
*/
public function __construct($value)
public function __construct($value, array $mergeStrategies = [])
{
$this->value = $value;
$this->merge = true;
$this->mergeStrategies = $mergeStrategies;
}

public function __invoke()
Expand Down
7 changes: 7 additions & 0 deletions src/MergesProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ trait MergesProps

protected bool $deepMerge = false;

protected array $mergeStrategies = [];

public function merge(): static
{
$this->merge = true;
Expand All @@ -31,4 +33,9 @@ public function shouldDeepMerge(): bool
{
return $this->deepMerge;
}

public function mergeStrategies(): array
{
return $this->mergeStrategies;
}
}
10 changes: 10 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,23 @@ public function resolveMergeProps(Request $request): array
->filter(fn ($prop) => $prop->shouldDeepMerge())
->keys();

$mergeStrategies = $mergeProps
->map(function ($prop, $key) {
return collect($prop->mergeStrategies())
->map(fn ($strategy) => $key.".".$strategy)
->toArray();
})
->flatten()
->values();

$mergeProps = $mergeProps
->filter(fn ($prop) => ! $prop->shouldDeepMerge())
->keys();

return array_filter([
'mergeProps' => $mergeProps->toArray(),
'deepMergeProps' => $deepMergeProps->toArray(),
'mergeStrategies' => $mergeStrategies->toArray(),
], fn ($prop) => count($prop) > 0);
}

Expand Down
5 changes: 3 additions & 2 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ public function merge($value): MergeProp

/**
* @param mixed $value
* @parram null|string|string[] $mergeStrategies
*/
public function deepMerge($value): MergeProp
public function deepMerge($value, $mergeStrategies = null): MergeProp
{
return (new MergeProp($value))->deepMerge();
return (new MergeProp($value, Arr::wrap($mergeStrategies)))->deepMerge();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/DeepMergePropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ public function test_can_resolve_bindings_when_invoked(): void

$this->assertInstanceOf(Request::class, $mergeProp());
}

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

$this->assertEquals(['key'], $mergeProp->mergeStrategies());
}
}
39 changes: 39 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,45 @@ public function test_server_response_with_deep_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;deepMergeProps&quot;:[&quot;foo&quot;,&quot;bar&quot;]}"></div>', $view->render());
}

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

$user = ['name' => 'Jonathan'];
$response = new Response(
'User/Edit',
[
'user' => $user,
'foo' => (new MergeProp('foo value', ['foo-key']))->deepMerge(),
'bar' => (new MergeProp('bar value', ['bar-key']))->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->assertSame([
'foo.foo-key',
'bar.bar-key',
], $page['mergeStrategies']);
$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;],&quot;mergeStrategies&quot;:[&quot;foo.foo-key&quot;,&quot;bar.bar-key&quot;]}"></div>', $view->render());
}

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