Skip to content

Commit c9fbf42

Browse files
committed
Add ability to build FormRequest with GetValue instance
1 parent 0a55171 commit c9fbf42

File tree

9 files changed

+172
-50
lines changed

9 files changed

+172
-50
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"ext-simplexml": "*",
1717
"laravel/framework": "^9.25",
1818
"nikic/php-parser": "v4.14.0",
19+
"orchestra/testbench": "^7.7",
1920
"phpstan/phpstan": "1.8.2",
2021
"phpstan/phpstan-deprecation-rules": "1.0.0",
2122
"phpstan/phpstan-phpunit": "1.1.1",

docs/content/en/index.md

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -427,49 +427,7 @@ $getValueArray = $factory->array([]);
427427
$getValueXml = $factory->xml(new SimpleXMLElement('<root />'));
428428
```
429429

430-
### Laravel
431-
432-
We have implemented helper functions that pulls array data from Laravel requests:
433-
434-
- `$this->getValueFactory->request($request);` - Initializes `ArrayData` with FormRequest and uses only **validated**
435-
array data.
436-
- `$this->getValueFactory->requestAll($request);` - Initializes `ArrayData` with **all values** from the requests
437-
438-
```php
439-
class TestAction {
440-
441-
public function __construct(private readonly \Wrkflow\GetValue\GetValueFactory $getValueFactory) {}
442-
443-
public function execute(\Illuminate\Http\Request $request): strin {
444-
return $this->getValueFactory
445-
->request($request)
446-
->getRequiredString('test');
447-
}
448-
}
449-
450-
// 1. Dependency injection, 2.
451-
$test = app(TestAction::class)->execute();
452-
```
453-
454-
### Dependency injection
455-
456-
To change the implementation using dependency injection just bind contracts below in your framework container:
457-
458-
- `Wrkflow\GetValue\Contracts\TransformerStrategy $transformerStrategy`
459-
- `Wrkflow\GetValue\Contracts\ExceptionBuilderContract $exceptionBuilder`
460-
461-
*Example for Laravel:*
462-
463-
```php
464-
class MyServiceProvider extends ServiceProvider {
465-
public function register (): void {
466-
parent::register();
467-
468-
$this->app->bind(Wrkflow\GetValue\Contracts\TransformerStrategy::class, MyTransformerStrategy::class);
469-
$this->app->bind(Wrkflow\GetValue\Contracts\ExceptionBuilderContract::class, MyExceptionBuilder::class);
470-
}
471-
}
472-
```
430+
Example for [Laravel](/laravel)
473431

474432
## Exceptions
475433

docs/content/en/laravel.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
```

ecs.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer;
66
use Symplify\EasyCodingStandard\Config\ECSConfig;
7-
use Symplify\EasyCodingStandard\ValueObject\Option;
87
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
98

109
return static function (ECSConfig $containerConfigurator): void {
@@ -15,12 +14,9 @@
1514

1615
$parameters = $containerConfigurator->parameters();
1716

18-
$parameters->set(Option::PARALLEL, true);
19-
20-
$parameters->set(
21-
Option::PATHS,
17+
$containerConfigurator->parallel();
18+
$containerConfigurator->paths(
2219
[__DIR__ . '/src', __DIR__ . '/tests', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']
2320
);
24-
25-
$parameters->set(Option::SKIP, [YodaStyleFixer::class]);
21+
$containerConfigurator->skip([YodaStyleFixer::class]);
2622
};

phpunit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@
2323
<directory suffix="Test.php">./tests</directory>
2424
</testsuite>
2525
</testsuites>
26+
<php>
27+
<env name="DB_CONNECTION" value="testing"/>
28+
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
29+
</php>
2630
</phpunit>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValue\Laravel;
6+
7+
use Illuminate\Foundation\Http\FormRequest;
8+
use Wrkflow\GetValue\GetValue;
9+
use Wrkflow\GetValue\GetValueFactory;
10+
11+
class GetValueFormRequest extends FormRequest
12+
{
13+
protected GetValue $data;
14+
15+
protected function passedValidation()
16+
{
17+
parent::passedValidation();
18+
19+
/** @var GetValueFactory $getValueFactory */
20+
$getValueFactory = $this->container->make(GetValueFactory::class);
21+
22+
$this->data = $getValueFactory->request($this);
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Laravel;
6+
7+
use Orchestra\Testbench\TestCase as BaseTestCase;
8+
9+
class AbstractLaravelTestCase extends BaseTestCase
10+
{
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Laravel;
6+
7+
use Illuminate\Http\Request;
8+
9+
class GetValueFormRequestTest extends AbstractLaravelTestCase
10+
{
11+
public function testNoValue(): void
12+
{
13+
/** @var TestFormRequest $request */
14+
$request = $this->app->make(TestFormRequest::class);
15+
$this->assertNull($request->getTest());
16+
}
17+
18+
public function testHasValue(): void
19+
{
20+
/** @var Request $appRequest */
21+
$appRequest = $this->app['request'];
22+
$appRequest->initialize(request: [
23+
'test' => 'Works',
24+
]);
25+
$appRequest->setMethod('POST'); // Use request data
26+
27+
$request = $this->app->make(TestFormRequest::class);
28+
$this->assertEquals('Works', $request->getTest());
29+
}
30+
}

tests/Laravel/TestFormRequest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Laravel;
6+
7+
use Wrkflow\GetValue\Laravel\GetValueFormRequest;
8+
9+
class TestFormRequest extends GetValueFormRequest
10+
{
11+
final public const KeyTest = 'test';
12+
13+
public function rules(): array
14+
{
15+
return [
16+
self::KeyTest => ['sometimes', 'string'],
17+
];
18+
}
19+
20+
public function getTest(): ?string
21+
{
22+
return $this->data->getString(self::KeyTest);
23+
}
24+
}

0 commit comments

Comments
 (0)