Skip to content
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: 1 addition & 3 deletions src/MergeProp.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ class MergeProp implements Mergeable

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

public function __invoke()
Expand Down
15 changes: 12 additions & 3 deletions src/MergesProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Inertia;

use Illuminate\Support\Arr;

trait MergesProps
{
protected bool $merge = false;

protected bool $deepMerge = false;

protected array $mergeStrategies = [];
protected array $matchOn = [];

public function merge(): static
{
Expand All @@ -24,6 +26,13 @@ public function deepMerge(): static
return $this->merge();
}

public function matchOn(string|array $matchOn): static
{
$this->matchOn = Arr::wrap($matchOn);

return $this;
}

public function shouldMerge(): bool
{
return $this->merge;
Expand All @@ -34,8 +43,8 @@ public function shouldDeepMerge(): bool
return $this->deepMerge;
}

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

$mergeStrategies = $mergeProps
$matchPropsOn = $mergeProps
->map(function ($prop, $key) {
return collect($prop->mergeStrategies())
return collect($prop->matchesOn())
->map(fn ($strategy) => $key.'.'.$strategy)
->toArray();
})
Expand All @@ -349,7 +349,7 @@ public function resolveMergeProps(Request $request): array
return array_filter([
'mergeProps' => $mergeProps->toArray(),
'deepMergeProps' => $deepMergeProps->toArray(),
'mergeStrategies' => $mergeStrategies->toArray(),
'matchPropsOn' => $matchPropsOn->toArray(),
], fn ($prop) => count($prop) > 0);
}

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

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

/**
Expand Down
13 changes: 10 additions & 3 deletions tests/DeepMergePropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ 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
public function test_can_use_single_string_as_key_to_match_on(): void
{
$mergeProp = (new MergeProp(['key' => 'value'], ['key']))->deepMerge();
$mergeProp = (new MergeProp(['key' => 'value']))->matchOn('key');

$this->assertEquals(['key'], $mergeProp->mergeStrategies());
$this->assertEquals(['key'], $mergeProp->matchesOn());
}

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

$this->assertEquals(['key', 'anotherKey'], $mergeProp->matchesOn());
}
}
9 changes: 5 additions & 4 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ public function test_server_response_with_merge_strategies(): void
'User/Edit',
[
'user' => $user,
'foo' => (new MergeProp('foo value', ['foo-key']))->deepMerge(),
'bar' => (new MergeProp('bar value', ['bar-key']))->deepMerge(),
'foo' => (new MergeProp('foo value'))->matchOn('foo-key')->deepMerge(),
'bar' => (new MergeProp('bar value'))->matchOn('bar-key')->deepMerge(),
],
'app',
'123'
Expand All @@ -231,13 +231,14 @@ public function test_server_response_with_merge_strategies(): void
'foo',
'bar',
], $page['deepMergeProps']);

$this->assertSame([
'foo.foo-key',
'bar.bar-key',
], $page['mergeStrategies']);
], $page['matchPropsOn']);
$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());
$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;matchPropsOn&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
Expand Down