diff --git a/docs/content/en/laravel.md b/docs/content/en/laravel.md index 294011b..72fbe97 100644 --- a/docs/content/en/laravel.md +++ b/docs/content/en/laravel.md @@ -4,6 +4,27 @@ subtitle: '' position: 4 --- +## Command input + +For safe access of command arguments / options you can extend `GetValueFactoryCommand` that provides `$this->inputData` +with wrapped arguments and options in `GetValue` instance. + +```php +class MyCommand extends GetValueFactoryCommand { + protected $signature = 'test {argument} {--option} {--value=}'; + + public function handle(): void + { + // Ensures that you will get a string + $this->inputData->getRequiredString('argument'); + // Get bool (option always return bool) + $this->inputData->getRequiredBool('option'); + // Returns nullable string + $this->inputData->getString('value'); + } +} +``` + ## GetValueFormRequest Allows you access `GetValue` instance within your `FormRequest` by extending `GetValueFormRequest`. @@ -36,6 +57,7 @@ We have implemented helper functions that pulls array data from Laravel requests - `$this->getValueFactory->request($request);` - Initializes `ArrayData` with FormRequest and uses only **validated** array data. - `$this->getValueFactory->requestAll($request);` - Initializes `ArrayData` with **all values** from the requests +- `$this->getValueFactory->command($request);` - Initializes `ArrayData` with command arguments and options. ```php class TestAction { diff --git a/src/GetValueFactory.php b/src/GetValueFactory.php index e5a2f44..393b753 100644 --- a/src/GetValueFactory.php +++ b/src/GetValueFactory.php @@ -4,6 +4,7 @@ namespace Wrkflow\GetValue; +use Illuminate\Console\Command; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Request; use SimpleXMLElement; @@ -23,11 +24,17 @@ public function __construct( ) { } + /** + * Wraps all request data. + */ public function requestAll(Request $request, string $parentKey = ''): GetValue { return $this->array($request->all(), $parentKey); } + /** + * Wraps validated request data. + */ public function request(FormRequest $request, string $parentKey = ''): GetValue { return $this->array($request->validated(), $parentKey); @@ -43,6 +50,14 @@ public function array(array $array, string $parentKey = ''): GetValue return $this->make(new ArrayData($array, $parentKey)); } + /** + * Wraps and merges command arguments and options. + */ + public function command(Command $command): GetValue + { + return $this->array(array_merge($command->arguments(), $command->options())); + } + protected function make(AbstractData $data): GetValue { return new GetValue( diff --git a/src/Laravel/GetValueFactoryCommand.php b/src/Laravel/GetValueFactoryCommand.php new file mode 100644 index 0000000..b7924e6 --- /dev/null +++ b/src/Laravel/GetValueFactoryCommand.php @@ -0,0 +1,29 @@ +inputData = $this->getValueFactory->command($this); + } +} diff --git a/tests/Laravel/GetValueFactoryCommandTest.php b/tests/Laravel/GetValueFactoryCommandTest.php new file mode 100644 index 0000000..0f0ac33 --- /dev/null +++ b/tests/Laravel/GetValueFactoryCommandTest.php @@ -0,0 +1,94 @@ + + */ + public function dataCommand(): array + { + return [ + 'argument only' => [ + fn (self $self) => $self->assertCommand( + input: 'test', + expectedArgument: 'test', + expectedBoolOption: false, + expectedValueOption: null, + ), + ], + 'argument, option' => [ + fn (self $self) => $self->assertCommand( + input: 'test --option', + expectedArgument: 'test', + expectedBoolOption: true, + expectedValueOption: null, + ), + ], + 'argument, option, value' => [ + fn (self $self) => $self->assertCommand( + input: 'test --option --value=test22', + expectedArgument: 'test', + expectedBoolOption: true, + expectedValueOption: 'test22', + ), + ], + ]; + } + + /** + * @param Closure(static):void $assert + * + * @dataProvider dataCommand + */ + public function testCommand(Closure $assert): void + { + $assert($this); + } + + public function assertCommand( + string $input, + ?string $expectedArgument, + bool $expectedBoolOption, + ?string $expectedValueOption, + ): void { + $command = new class( + new GetValueFactory(), + $expectedArgument, + $expectedBoolOption, + $expectedValueOption, + ) extends GetValueFactoryCommand { + protected $signature = 'test {argument} {--option} {--value=}'; + + public function __construct( + private readonly GetValueFactory $getValueFactory, + private readonly ?string $expectedArgument, + private readonly ?bool $expectedBoolOption, + private readonly ?string $expectedValueOption, + ) { + parent::__construct($this->getValueFactory); + } + + public function handle(): void + { + Assert::assertSame($this->expectedArgument, $this->inputData->getString('argument')); + Assert::assertSame($this->expectedBoolOption, $this->inputData->getBool('option')); + Assert::assertSame($this->expectedValueOption, $this->inputData->getString('value')); + } + }; + + $command->setLaravel($this->app()); + + $command->run(new StringInput($input), new NullOutput()); + } +} diff --git a/tests/Laravel/GetValueFactoryLaravelCommandTest.php b/tests/Laravel/GetValueFactoryLaravelCommandTest.php new file mode 100644 index 0000000..a1dbaef --- /dev/null +++ b/tests/Laravel/GetValueFactoryLaravelCommandTest.php @@ -0,0 +1,105 @@ +factory = new GetValueFactory(); + } + + /** + * @return array + */ + public function dataCommand(): array + { + return [ + 'argument only' => [ + fn (self $self) => $self->assertCommand( + input: 'test', + expectedArgument: 'test', + expectedBoolOption: false, + expectedValueOption: null, + ), + ], + 'argument, option' => [ + fn (self $self) => $self->assertCommand( + input: 'test --option', + expectedArgument: 'test', + expectedBoolOption: true, + expectedValueOption: null, + ), + ], + 'argument, option, value' => [ + fn (self $self) => $self->assertCommand( + input: 'test --option --value=test22', + expectedArgument: 'test', + expectedBoolOption: true, + expectedValueOption: 'test22', + ), + ], + ]; + } + + /** + * @param Closure(static):void $assert + * + * @dataProvider dataCommand + */ + public function testCommand(Closure $assert): void + { + $assert($this); + } + + public function assertCommand( + string $input, + ?string $expectedArgument, + bool $expectedBoolOption, + ?string $expectedValueOption, + ): void { + $command = new class( + $this->factory, + $expectedArgument, + $expectedBoolOption, + $expectedValueOption, + ) extends Command { + protected $signature = 'test {argument} {--option} {--value=}'; + + public function __construct( + private readonly GetValueFactory $getValueFactory, + private readonly ?string $expectedArgument, + private readonly ?bool $expectedBoolOption, + private readonly ?string $expectedValueOption, + ) { + parent::__construct(); + } + + public function handle(): void + { + $data = $this->getValueFactory->command($this); + + Assert::assertSame($this->expectedArgument, $data->getString('argument')); + Assert::assertSame($this->expectedBoolOption, $data->getBool('option')); + Assert::assertSame($this->expectedValueOption, $data->getString('value')); + } + }; + + $command->setLaravel($this->app()); + + $command->run(new StringInput($input), new NullOutput()); + } +}