diff --git a/src/Illuminate/Container/Attributes/Input.php b/src/Illuminate/Container/Attributes/Input.php new file mode 100644 index 000000000000..d0a303c8345a --- /dev/null +++ b/src/Illuminate/Container/Attributes/Input.php @@ -0,0 +1,26 @@ +make('request')->input($attribute->key, $attribute->default); + } +} diff --git a/src/Illuminate/Container/Attributes/Post.php b/src/Illuminate/Container/Attributes/Post.php new file mode 100644 index 000000000000..35a245f69782 --- /dev/null +++ b/src/Illuminate/Container/Attributes/Post.php @@ -0,0 +1,26 @@ +make('request')->post($attribute->key, $attribute->default); + } +} diff --git a/src/Illuminate/Container/Attributes/Query.php b/src/Illuminate/Container/Attributes/Query.php new file mode 100644 index 000000000000..02b4465f1d32 --- /dev/null +++ b/src/Illuminate/Container/Attributes/Query.php @@ -0,0 +1,26 @@ +make('request')->query($attribute->key, $attribute->default); + } +} diff --git a/src/Illuminate/Container/Attributes/Validated.php b/src/Illuminate/Container/Attributes/Validated.php new file mode 100644 index 000000000000..a9ec45149684 --- /dev/null +++ b/src/Illuminate/Container/Attributes/Validated.php @@ -0,0 +1,26 @@ +make('request.validated')->validated($attribute->key, $attribute->default); + } +} diff --git a/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php b/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php index d456ce0dd971..5f4eaefd4d4b 100644 --- a/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php @@ -34,6 +34,8 @@ public function boot() $request = FormRequest::createFrom($app['request'], $request); $request->setContainer($app)->setRedirector($app->make(Redirector::class)); + + $app['request.validated'] = $request; }); } } diff --git a/tests/Container/ContextualAttributeBindingTest.php b/tests/Container/ContextualAttributeBindingTest.php index 9d87cdd22a24..0b9ba253e979 100644 --- a/tests/Container/ContextualAttributeBindingTest.php +++ b/tests/Container/ContextualAttributeBindingTest.php @@ -15,10 +15,14 @@ use Illuminate\Container\Attributes\CurrentUser; use Illuminate\Container\Attributes\Database; use Illuminate\Container\Attributes\Give; +use Illuminate\Container\Attributes\Input; use Illuminate\Container\Attributes\Log; +use Illuminate\Container\Attributes\Post; +use Illuminate\Container\Attributes\Query; use Illuminate\Container\Attributes\RouteParameter; use Illuminate\Container\Attributes\Storage; use Illuminate\Container\Attributes\Tag; +use Illuminate\Container\Attributes\Validated; use Illuminate\Container\Container; use Illuminate\Container\RewindableGenerator; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; @@ -29,6 +33,7 @@ use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model; use Illuminate\Filesystem\FilesystemManager; +use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Request; use Illuminate\Log\Context\Repository as ContextRepository; use Illuminate\Log\LogManager; @@ -354,6 +359,67 @@ public function testTagAttribute() $this->assertEquals([1, 2], iterator_to_array($value)); } + + public function testInputAttribute() + { + $container = new Container; + $container->bind('request', function () { + return Request::create('/?name=Foo+Bar', 'POST', [ + 'age' => '32', + ]); + }); + + $data = $container->make(TestInputData::class); + + $this->assertEquals('Foo Bar', $data->name); + $this->assertEquals(32, $data->age); + } + + public function testPostAttribute() + { + $container = new Container; + $container->bind('request', function () { + return Request::create('/', 'POST', [ + 'name' => 'Foo Bar', + 'age' => '32', + ]); + }); + + $data = $container->make(TestPostData::class); + + $this->assertEquals('Foo Bar', $data->name); + $this->assertEquals(32, $data->age); + } + + public function testQueryAttribute() + { + $container = new Container; + $container->bind('request', function () { + return Request::create('/?name=Foo+Bar&age=32', 'GET'); + }); + + $data = $container->make(TestQueryData::class); + + $this->assertEquals('Foo Bar', $data->name); + $this->assertEquals(32, $data->age); + } + + public function testValidatedAttribute() + { + $container = new Container; + $container->bind('request.validated', function () { + $request = m::mock(FormRequest::class); + $request->shouldReceive('validated')->with('name', null)->andReturn('Foo Bar'); + $request->shouldReceive('validated')->with('age', 24)->andReturn(32); + + return $request; + }); + + $data = $container->make(TestValidatedData::class); + + $this->assertEquals('Foo Bar', $data->name); + $this->assertEquals(32, $data->age); + } } #[Attribute(Attribute::TARGET_PARAMETER)] @@ -575,3 +641,39 @@ public function __construct( // } } + +final readonly class TestInputData +{ + public function __construct( + #[Input('name')] public string $name, + #[Input('age', 24)] public int $age, + ) { + } +} + +final readonly class TestQueryData +{ + public function __construct( + #[Query('name')] public string $name, + #[Query('age', 24)] public int $age, + ) { + } +} + +final readonly class TestPostData +{ + public function __construct( + #[Post('name')] public string $name, + #[Post('age', 24)] public int $age, + ) { + } +} + +final readonly class TestValidatedData +{ + public function __construct( + #[Validated('name')] public string $name, + #[Validated('age', 24)] public int $age, + ) { + } +}