-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add the ability to strip extra params
- Loading branch information
Showing
6 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bag\Attributes; | ||
|
||
use Attribute; | ||
use Bag\Attributes\Attribute as AttributeInterface; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class StripExtraParameters implements AttributeInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bag\Pipelines\Pipes; | ||
|
||
use Bag\Bag; | ||
use Bag\Pipelines\Values\BagInput; | ||
|
||
readonly class StripExtraParameters | ||
{ | ||
/** | ||
* @template T of Bag | ||
* @param BagInput<T> $input | ||
* @return BagInput<T> | ||
*/ | ||
public function __invoke(BagInput $input): BagInput | ||
{ | ||
if ($input->variadic) { | ||
return $input; | ||
} | ||
|
||
$extra = collect(); | ||
$input->values->each(function (mixed $_, string $key) use ($input, $extra) { | ||
if ($input->params->has($key)) { | ||
return; | ||
} | ||
|
||
$extra->add($key); | ||
}); | ||
|
||
$input->values = $input->values->except($extra); | ||
|
||
return $input; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Fixtures\Values; | ||
|
||
use Bag\Attributes\StripExtraParameters; | ||
use Bag\Bag; | ||
|
||
#[StripExtraParameters] | ||
readonly class StripExtraParametersBag extends Bag | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public int $age, | ||
public string $email | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
use Bag\Pipelines\Pipes\ExtraParameters; | ||
use Bag\Pipelines\Pipes\IsVariadic; | ||
use Bag\Pipelines\Pipes\MapInput; | ||
use Bag\Pipelines\Pipes\ProcessArguments; | ||
use Bag\Pipelines\Pipes\ProcessParameters; | ||
use Bag\Pipelines\Pipes\StripExtraParameters; | ||
use Bag\Pipelines\Values\BagInput; | ||
use Illuminate\Support\Collection; | ||
use Tests\Fixtures\Values\StripExtraParametersBag; | ||
use Tests\Fixtures\Values\VariadicBag; | ||
|
||
covers(ExtraParameters::class, StripExtraParameters::class); | ||
|
||
test('it does not error on non variadic with extra parameters ignored', function () { | ||
$input = new BagInput(StripExtraParametersBag::class, collect([ | ||
'name' => 'Davey Shafik', | ||
'age' => 40, | ||
'email' => '[email protected]', | ||
'test' => true, | ||
])); | ||
$input = (new ProcessParameters())($input, fn (BagInput $input) => $input); | ||
$input = (new MapInput())($input, fn (BagInput $input) => $input); | ||
$input = (new IsVariadic())($input, fn (BagInput $input) => $input); | ||
$input = (new ExtraParameters())($input, fn (BagInput $input) => $input); | ||
|
||
$pipe = new StripExtraParameters(); | ||
$input = $pipe($input); | ||
|
||
expect($input) | ||
->toBeInstanceOf(BagInput::class) | ||
->and($input->values) | ||
->toBeInstanceOf(Collection::class) | ||
->and($input->values->toArray()) | ||
->toBe([ | ||
'name' => 'Davey Shafik', | ||
'age' => 40, | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
|
||
test('it does not error on non variadic with extra positional parameters ignore', function () { | ||
$input = new BagInput(StripExtraParametersBag::class, collect([ | ||
'Davey Shafik', | ||
40, | ||
'[email protected]', | ||
true, | ||
])); | ||
$input = (new ProcessParameters())($input, fn (BagInput $input) => $input); | ||
$input = (new ProcessArguments())($input, fn (BagInput $input) => $input); | ||
$input = (new MapInput())($input, fn (BagInput $input) => $input); | ||
$input = (new IsVariadic())($input, fn (BagInput $input) => $input); | ||
$input = (new ExtraParameters())($input, fn (BagInput $input) => $input); | ||
|
||
$pipe = new StripExtraParameters(); | ||
$input = $pipe($input); | ||
|
||
expect($input) | ||
->toBeInstanceOf(BagInput::class) | ||
->and($input->values) | ||
->toBeInstanceOf(Collection::class) | ||
->and($input->values->toArray()) | ||
->toBe([ | ||
'name' => 'Davey Shafik', | ||
'age' => 40, | ||
'email' => '[email protected]', | ||
]); | ||
}); | ||
|
||
test('it does not strip extra parameters with variadic', function () { | ||
$input = new BagInput(VariadicBag::class, collect([ | ||
'name' => 'Davey Shafik', | ||
'age' => 40, | ||
'email' => '[email protected]', | ||
'test' => true, | ||
])); | ||
$input = (new ProcessParameters())($input, fn (BagInput $input) => $input); | ||
$input = (new MapInput())($input, fn (BagInput $input) => $input); | ||
$input = (new IsVariadic())($input, fn (BagInput $input) => $input); | ||
$input = (new ExtraParameters())($input, fn (BagInput $input) => $input); | ||
|
||
$pipe = new StripExtraParameters(); | ||
$input = $pipe($input); | ||
|
||
expect($input) | ||
->toBeInstanceOf(BagInput::class) | ||
->and($input->values) | ||
->toBeInstanceOf(Collection::class) | ||
->and($input->values->toArray()) | ||
->toBe([ | ||
'name' => 'Davey Shafik', | ||
'age' => 40, | ||
'email' => '[email protected]', | ||
'test' => true, | ||
]); | ||
}); |