Skip to content

Commit b732a5c

Browse files
authored
Merge pull request #747 from inertiajs/refactor-merge-strategies
[2.x] Refactor `mergeStrategies` argument to `matchOn()` method
2 parents f7d630a + 4a78f78 commit b732a5c

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

src/MergeProp.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ class MergeProp implements Mergeable
1313

1414
/**
1515
* @param mixed $value
16-
* @param string[] $mergeStrategies
1716
*/
18-
public function __construct($value, array $mergeStrategies = [])
17+
public function __construct($value)
1918
{
2019
$this->value = $value;
2120
$this->merge = true;
22-
$this->mergeStrategies = $mergeStrategies;
2321
}
2422

2523
public function __invoke()

src/MergesProps.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Inertia;
44

5+
use Illuminate\Support\Arr;
6+
57
trait MergesProps
68
{
79
protected bool $merge = false;
810

911
protected bool $deepMerge = false;
1012

11-
protected array $mergeStrategies = [];
13+
protected array $matchOn = [];
1214

1315
public function merge(): static
1416
{
@@ -24,6 +26,13 @@ public function deepMerge(): static
2426
return $this->merge();
2527
}
2628

29+
public function matchOn(string|array $matchOn): static
30+
{
31+
$this->matchOn = Arr::wrap($matchOn);
32+
33+
return $this;
34+
}
35+
2736
public function shouldMerge(): bool
2837
{
2938
return $this->merge;
@@ -34,8 +43,8 @@ public function shouldDeepMerge(): bool
3443
return $this->deepMerge;
3544
}
3645

37-
public function mergeStrategies(): array
46+
public function matchesOn(): array
3847
{
39-
return $this->mergeStrategies;
48+
return $this->matchOn;
4049
}
4150
}

src/Response.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ public function resolveMergeProps(Request $request): array
333333
->filter(fn ($prop) => $prop->shouldDeepMerge())
334334
->keys();
335335

336-
$mergeStrategies = $mergeProps
336+
$matchPropsOn = $mergeProps
337337
->map(function ($prop, $key) {
338-
return collect($prop->mergeStrategies())
338+
return collect($prop->matchesOn())
339339
->map(fn ($strategy) => $key.'.'.$strategy)
340340
->toArray();
341341
})
@@ -349,7 +349,7 @@ public function resolveMergeProps(Request $request): array
349349
return array_filter([
350350
'mergeProps' => $mergeProps->toArray(),
351351
'deepMergeProps' => $deepMergeProps->toArray(),
352-
'mergeStrategies' => $mergeStrategies->toArray(),
352+
'matchPropsOn' => $matchPropsOn->toArray(),
353353
], fn ($prop) => count($prop) > 0);
354354
}
355355

src/ResponseFactory.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,10 @@ public function merge($value): MergeProp
142142

143143
/**
144144
* @param mixed $value
145-
*
146-
* @parram null|string|string[] $mergeStrategies
147145
*/
148-
public function deepMerge($value, $mergeStrategies = null): MergeProp
146+
public function deepMerge($value): MergeProp
149147
{
150-
return (new MergeProp($value, Arr::wrap($mergeStrategies)))->deepMerge();
148+
return (new MergeProp($value))->deepMerge();
151149
}
152150

153151
/**

tests/DeepMergePropTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@ public function test_can_resolve_bindings_when_invoked(): void
2828
$this->assertInstanceOf(Request::class, $mergeProp());
2929
}
3030

31-
public function test_can_use_single_string_as_merge_strategy(): void
31+
public function test_can_use_single_string_as_key_to_match_on(): void
3232
{
33-
$mergeProp = (new MergeProp(['key' => 'value'], ['key']))->deepMerge();
33+
$mergeProp = (new MergeProp(['key' => 'value']))->matchOn('key');
3434

35-
$this->assertEquals(['key'], $mergeProp->mergeStrategies());
35+
$this->assertEquals(['key'], $mergeProp->matchesOn());
36+
}
37+
38+
public function test_can_use_an_array_of_strings_as_keys_to_match_on(): void
39+
{
40+
$mergeProp = (new MergeProp(['key' => 'value']))->matchOn(['key', 'anotherKey']);
41+
42+
$this->assertEquals(['key', 'anotherKey'], $mergeProp->matchesOn());
3643
}
3744
}

tests/ResponseTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ public function test_server_response_with_merge_strategies(): void
210210
'User/Edit',
211211
[
212212
'user' => $user,
213-
'foo' => (new MergeProp('foo value', ['foo-key']))->deepMerge(),
214-
'bar' => (new MergeProp('bar value', ['bar-key']))->deepMerge(),
213+
'foo' => (new MergeProp('foo value'))->matchOn('foo-key')->deepMerge(),
214+
'bar' => (new MergeProp('bar value'))->matchOn('bar-key')->deepMerge(),
215215
],
216216
'app',
217217
'123'
@@ -231,13 +231,14 @@ public function test_server_response_with_merge_strategies(): void
231231
'foo',
232232
'bar',
233233
], $page['deepMergeProps']);
234+
234235
$this->assertSame([
235236
'foo.foo-key',
236237
'bar.bar-key',
237-
], $page['mergeStrategies']);
238+
], $page['matchPropsOn']);
238239
$this->assertFalse($page['clearHistory']);
239240
$this->assertFalse($page['encryptHistory']);
240-
$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());
241+
$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());
241242
}
242243

243244
public function test_server_response_with_defer_and_merge_props(): void

0 commit comments

Comments
 (0)