Skip to content

Commit

Permalink
Add deepMerge method for enhanced property merging
Browse files Browse the repository at this point in the history
Introduce a deepMerge method to handle nested property merges more effectively. This addition includes updates to the Response, MergesProps, and ResponseFactory classes, enabling nested properties to be merged and tracked separately.
  • Loading branch information
HichemTab-tech committed Oct 26, 2024
1 parent a836013 commit 9d39da3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
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
14 changes: 14 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,16 @@ public function shouldMerge(): bool
{
return $this->merge;
}

public function deepMerge(): static
{
$this->deepMerge = true;

return $this;
}

public function shouldDeepMerge(): bool
{
return $this->deepMerge;
}
}
16 changes: 15 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,18 @@ public function resolveMergeProps(Request $request): array
$mergeProps = collect($this->props)
->filter(function ($prop) {
return $prop instanceof Mergeable;
});

$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->shouldMerge();
})
Expand All @@ -302,7 +313,10 @@ public function resolveMergeProps(Request $request): array
})
->keys();

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

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

0 comments on commit 9d39da3

Please sign in to comment.