|
| 1 | +--- |
| 2 | +title: Laravel |
| 3 | +subtitle: '' |
| 4 | +position: 4 |
| 5 | +--- |
| 6 | +# GetValueFormRequest |
| 7 | + |
| 8 | +Allows you access `GetValue` instance within your `FormRequest` by extending `GetValueFormRequest`. |
| 9 | + |
| 10 | +```php |
| 11 | +class TestFormRequest extends GetValueFormRequest |
| 12 | +{ |
| 13 | + final public const KeyTest = 'test'; |
| 14 | + |
| 15 | + public function rules(): array |
| 16 | + { |
| 17 | + return [ |
| 18 | + self::KeyTest => ['sometimes', 'string'], |
| 19 | + ]; |
| 20 | + } |
| 21 | + |
| 22 | + public function getTest(): ?string |
| 23 | + { |
| 24 | + return $this->data->getString(self::KeyTest); |
| 25 | + } |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Get value instance uses only validated data. |
| 30 | + |
| 31 | + |
| 32 | +# GetValueFactory |
| 33 | + |
| 34 | +We have implemented helper functions that pulls array data from Laravel requests: |
| 35 | + |
| 36 | +- `$this->getValueFactory->request($request);` - Initializes `ArrayData` with FormRequest and uses only **validated** |
| 37 | + array data. |
| 38 | +- `$this->getValueFactory->requestAll($request);` - Initializes `ArrayData` with **all values** from the requests |
| 39 | + |
| 40 | +```php |
| 41 | +class TestAction { |
| 42 | + |
| 43 | + public function __construct(private readonly \Wrkflow\GetValue\GetValueFactory $getValueFactory) {} |
| 44 | + |
| 45 | + public function execute(\Illuminate\Http\Request $request): strin { |
| 46 | + return $this->getValueFactory |
| 47 | + ->request($request) |
| 48 | + ->getRequiredString('test'); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// 1. Dependency injection, 2. |
| 53 | +$test = app(TestAction::class)->execute(); |
| 54 | +``` |
| 55 | + |
| 56 | +### Dependency injection |
| 57 | + |
| 58 | +To change the implementation using dependency injection just bind contracts below in your framework container: |
| 59 | + |
| 60 | +- `Wrkflow\GetValue\Contracts\TransformerStrategy $transformerStrategy` |
| 61 | +- `Wrkflow\GetValue\Contracts\ExceptionBuilderContract $exceptionBuilder` |
| 62 | + |
| 63 | +*Example for Laravel:* |
| 64 | + |
| 65 | +```php |
| 66 | +class MyServiceProvider extends ServiceProvider { |
| 67 | + public function register (): void { |
| 68 | + parent::register(); |
| 69 | + |
| 70 | + $this->app->bind(Wrkflow\GetValue\Contracts\TransformerStrategy::class, MyTransformerStrategy::class); |
| 71 | + $this->app->bind(Wrkflow\GetValue\Contracts\ExceptionBuilderContract::class, MyExceptionBuilder::class); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
0 commit comments