Skip to content

Commit 656661c

Browse files
fix!: rename LazyProp to OptionalProp and add static helpers
1 parent fa627c9 commit 656661c

File tree

8 files changed

+78
-53
lines changed

8 files changed

+78
-53
lines changed

src/Http/InertiaResponse.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use NeoIsRecursive\Inertia\PageData;
1111
use NeoIsRecursive\Inertia\Props\AlwaysProp;
1212
use NeoIsRecursive\Inertia\Props\DeferProp;
13-
use NeoIsRecursive\Inertia\Props\LazyProp;
13+
use NeoIsRecursive\Inertia\Props\OptionalProp;
1414
use NeoIsRecursive\Inertia\Support\Header;
1515
use NeoIsRecursive\Inertia\Views\InertiaBaseView;
1616
use Tempest\Http\IsResponse;
@@ -105,7 +105,10 @@ private static function resolvePartialProps(Request $request, string $component,
105105
$headers = $request->headers;
106106

107107
if (!static::isPartial($request, $component)) {
108-
return array_filter($props, static fn($prop) => !($prop instanceof LazyProp || $prop instanceof DeferProp));
108+
return array_filter(
109+
$props,
110+
static fn($prop) => !($prop instanceof OptionalProp || $prop instanceof DeferProp),
111+
);
109112
}
110113

111114
$only = array_filter(explode(

src/Inertia.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
namespace NeoIsRecursive\Inertia;
66

7-
use Exception;
7+
use Closure;
88
use NeoIsRecursive\Inertia\Http\InertiaResponse;
99
use NeoIsRecursive\Inertia\InertiaConfig;
10+
use NeoIsRecursive\Inertia\Props\AlwaysProp;
11+
use NeoIsRecursive\Inertia\Props\DeferProp;
12+
use NeoIsRecursive\Inertia\Props\OptionalProp;
1013
use NeoIsRecursive\Inertia\Support\Header;
1114
use Tempest\Container\Container;
1215
use Tempest\Container\Singleton;
@@ -16,6 +19,8 @@
1619
use Tempest\Http\Responses\Redirect;
1720
use Tempest\Http\Session\Session;
1821
use Tempest\Http\Status;
22+
use Tempest\Reflection\FunctionReflector;
23+
use Tempest\Reflection\MethodReflector;
1924

2025
#[Singleton]
2126
final class Inertia
@@ -36,6 +41,21 @@ public function share(string|array $key, mixed $value = null): self
3641
return $this;
3742
}
3843

44+
public static function optional(MethodReflector|FunctionReflector|Closure $callback): OptionalProp
45+
{
46+
return new OptionalProp($callback);
47+
}
48+
49+
public static function defer(MethodReflector|FunctionReflector|Closure $callback): DeferProp
50+
{
51+
return new DeferProp($callback);
52+
}
53+
54+
public static function always(mixed $value): AlwaysProp
55+
{
56+
return new AlwaysProp($value);
57+
}
58+
3959
public function flushShared(): self
4060
{
4161
$this->config->flushSharedProps();

src/Props/AlwaysProp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class AlwaysProp implements CallableProp, MergeableProp
1919
use IsMergeableProp;
2020

2121
public function __construct(
22-
public readonly MethodReflector|FunctionReflector|string|Closure|array $value,
22+
public readonly mixed $value,
2323
public private(set) bool $shouldMerge = false,
2424
) {}
2525

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use function Tempest\invoke;
1616

17-
final class LazyProp implements CallableProp, MergeableProp
17+
final class OptionalProp implements CallableProp, MergeableProp
1818
{
1919
use IsMergeableProp;
2020

tests/Fixtures/TestController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use NeoIsRecursive\Inertia\Http\Middleware\EncryptHistory;
99
use NeoIsRecursive\Inertia\Inertia;
1010
use NeoIsRecursive\Inertia\Props\AlwaysProp;
11-
use NeoIsRecursive\Inertia\Props\LazyProp;
11+
use NeoIsRecursive\Inertia\Props\OptionalProp;
1212
use NeoIsRecursive\Inertia\Tests\Fixtures\Requests\CreatePerson;
1313
use Tempest\Http\Responses\Ok;
1414
use Tempest\Http\Responses\Redirect;
@@ -60,7 +60,9 @@ public function testAllSortsOfProps(Inertia $inertia): InertiaResponse
6060
)->render(
6161
component: 'User/Edit',
6262
props: [
63-
new AlwaysProp(fn() => 'baz'),
63+
'always' => Inertia::always(fn() => 'always'),
64+
'optional' => Inertia::optional(fn() => 'optional'),
65+
'defer' => Inertia::defer(fn() => 'defer'),
6466
],
6567
);
6668
}
@@ -115,7 +117,7 @@ public function testCanMergeProps(Inertia $inertia): InertiaResponse
115117
component: 'test',
116118
props: [
117119
'foo' => new AlwaysProp(fn() => 'bar')->merge(),
118-
'baz' => new LazyProp(fn() => 'qux')->merge(),
120+
'baz' => new OptionalProp(fn() => 'qux')->merge(),
119121
],
120122
);
121123
}

tests/Integration/LazyPropTest.php

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NeoIsRecursive\Inertia\Tests\Integration;
6+
7+
use NeoIsRecursive\Inertia\Props\OptionalProp;
8+
use NeoIsRecursive\Inertia\Tests\TestCase;
9+
use Tempest\Http\Request;
10+
11+
final class OptionalPropTest extends TestCase
12+
{
13+
public function test_can_invoke(): void
14+
{
15+
$optionalProp = new OptionalProp(function (): string {
16+
return 'A optional value';
17+
});
18+
19+
static::assertSame(
20+
expected: 'A optional value',
21+
actual: $optionalProp(),
22+
);
23+
}
24+
25+
public function test_can_accept_scalar_values(): void
26+
{
27+
$optionalProp = new OptionalProp(fn() => 'A optional value');
28+
29+
static::assertSame(
30+
expected: 'A optional value',
31+
actual: $optionalProp(),
32+
);
33+
}
34+
35+
public function test_can_resolve_bindings_when_invoked(): void
36+
{
37+
$optionalProp = new OptionalProp(fn(Request $request) => $request);
38+
39+
static::assertInstanceOf(Request::class, $optionalProp());
40+
}
41+
}

tests/Integration/ResponseTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use NeoIsRecursive\Inertia\PageData;
99
use NeoIsRecursive\Inertia\Props\AlwaysProp;
1010
use NeoIsRecursive\Inertia\Props\DeferProp;
11-
use NeoIsRecursive\Inertia\Props\LazyProp;
11+
use NeoIsRecursive\Inertia\Props\OptionalProp;
1212
use NeoIsRecursive\Inertia\Support\Header;
1313
use NeoIsRecursive\Inertia\Tests\TestCase;
1414
use NeoIsRecursive\Inertia\Views\InertiaBaseView;
@@ -366,7 +366,7 @@ public function test_lazy_props_are_not_included_by_default(): void
366366
{
367367
$request = $this->createInertiaRequest(Method::GET, uri: '/users');
368368

369-
$lazyProp = new LazyProp(fn() => 'A lazy value');
369+
$lazyProp = new OptionalProp(fn() => 'A lazy value');
370370

371371
$response = new InertiaResponse(
372372
$request,
@@ -394,7 +394,7 @@ public function test_lazy_props_are_included_in_partial_reload(): void
394394
Header::PARTIAL_ONLY => 'lazy',
395395
]);
396396

397-
$lazyProp = new LazyProp(function (): string {
397+
$lazyProp = new OptionalProp(function (): string {
398398
return 'A lazy value';
399399
});
400400

@@ -425,7 +425,7 @@ public function test_always_props_are_included_on_partial_reload(): void
425425
]);
426426

427427
$props = [
428-
'user' => new LazyProp(function (): array {
428+
'user' => new OptionalProp(function (): array {
429429
return [
430430
'name' => 'Jonathan Reinink',
431431
'email' => '[email protected]',

0 commit comments

Comments
 (0)