generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #380 from bastien-phi/form_request_normalizer
Introduce FormRequestNormalizer
- Loading branch information
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |