Skip to content

Commit

Permalink
Merge pull request #380 from bastien-phi/form_request_normalizer
Browse files Browse the repository at this point in the history
Introduce FormRequestNormalizer
  • Loading branch information
rubenvanassche authored Mar 24, 2023
2 parents 335d16a + bae55ce commit 869b876
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/
'normalizers' => [
Spatie\LaravelData\Normalizers\ModelNormalizer::class,
// Spatie\LaravelData\Normalizers\FormRequestNormalizer::class,
Spatie\LaravelData\Normalizers\ArrayableNormalizer::class,
Spatie\LaravelData\Normalizers\ObjectNormalizer::class,
Spatie\LaravelData\Normalizers\ArrayNormalizer::class,
Expand Down
17 changes: 17 additions & 0 deletions src/Normalizers/FormRequestNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\LaravelData\Normalizers;

use Illuminate\Foundation\Http\FormRequest;

class FormRequestNormalizer implements Normalizer
{
public function normalize(mixed $value): ?array
{
if (! $value instanceof FormRequest) {
return null;
}

return $value->validated();
}
}
14 changes: 14 additions & 0 deletions tests/Fakes/DataWithNullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Spatie\LaravelData\Tests\Fakes;

use Spatie\LaravelData\Data;

class DataWithNullable extends Data
{
public function __construct(
public string $string,
public ?string $nullableString,
) {
}
}
56 changes: 56 additions & 0 deletions tests/Normalizers/FormRequestNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Illuminate\Foundation\Http\FormRequest;
use Spatie\LaravelData\Normalizers\FormRequestNormalizer;
use Spatie\LaravelData\Tests\Fakes\DataWithNullable;

beforeEach(function () {
config()->set('data.normalizers', [FormRequestNormalizer::class]);
});

it('can create a data object from FormRequest', function () {
$request = new class () extends FormRequest {
public function rules(): array
{
return [
'string' => 'required|string',
'nullableString' => 'nullable|string',
];
}
};
$request
->replace([
'string' => 'Hello',
'nullableString' => 'World',
])
->setContainer(app())
->validateResolved();

$originalData = new DataWithNullable('Hello', 'World');
$createdData = DataWithNullable::from($request);

expect($createdData)->toEqual($originalData);
});

it("excludes unsafe data", function () {
$request = new class () extends FormRequest {
public function rules(): array
{
return [
'string' => 'required|string',
];
}
};
$request
->replace([
'string' => 'Hello',
'nullableString' => 'World',
])
->setContainer(app())
->validateResolved();

$originalData = new DataWithNullable('Hello', null);
$createdData = DataWithNullable::from($request);

expect($createdData)->toEqual($originalData);
});

0 comments on commit 869b876

Please sign in to comment.