Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce FormRequestNormalizer #380

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});