diff --git a/src/Illuminate/Container/Attributes/Input.php b/src/Illuminate/Container/Attributes/Input.php new file mode 100644 index 000000000000..b99eb2f2e29d --- /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/tests/Container/ContextualAttributeBindingTest.php b/tests/Container/ContextualAttributeBindingTest.php index 9d87cdd22a24..04d326cb68df 100644 --- a/tests/Container/ContextualAttributeBindingTest.php +++ b/tests/Container/ContextualAttributeBindingTest.php @@ -15,7 +15,10 @@ 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; @@ -354,6 +357,50 @@ 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); + } } #[Attribute(Attribute::TARGET_PARAMETER)] @@ -575,3 +622,30 @@ 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, + ) { + } +}