Skip to content

[12.x] Add Request input contextual attributes #56217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: 12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Container/Attributes/Input.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Input implements ContextualAttribute
{
/**
* Create a new class instance.
*/
public function __construct(public ?string $key = null, public mixed $default = null)
{
}

/**
* Resolve the POST data from the request.
*/
public static function resolve(self $attribute, Container $container): mixed
{
return $container->make('request')->input($attribute->key, $attribute->default);
}
}
26 changes: 26 additions & 0 deletions src/Illuminate/Container/Attributes/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Post implements ContextualAttribute
{
/**
* Create a new class instance.
*/
public function __construct(public ?string $key = null, public mixed $default = null)
{
}

/**
* Resolve the POST data from the request.
*/
public static function resolve(self $attribute, Container $container): mixed
{
return $container->make('request')->post($attribute->key, $attribute->default);
}
}
26 changes: 26 additions & 0 deletions src/Illuminate/Container/Attributes/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Query implements ContextualAttribute
{
/**
* Create a new class instance.
*/
public function __construct(public ?string $key = null, public mixed $default = null)
{
}

/**
* Resolve the GET data from the request.
*/
public static function resolve(self $attribute, Container $container): mixed
{
return $container->make('request')->query($attribute->key, $attribute->default);
}
}
74 changes: 74 additions & 0 deletions tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
) {
}
}