fixed issue with empty query params causing errors

This commit is contained in:
pushrbx 2024-01-19 21:33:57 +00:00 committed by GitHub
parent bb71a7a9eb
commit f7f074a532
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,10 +3,8 @@
namespace App\Dto\Concerns;
use Illuminate\Support\Collection;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\App;
use \ReflectionClass;
use Spatie\LaravelData\Support\DataConfig;
use Spatie\LaravelData\Support\DataProperty;
/**
* A trait for preparing the incoming data before passing it through the data pipeline.
@ -29,22 +27,33 @@ trait PreparesData
// collection, and if they are present have such a value, convert them.
$dataClass = app(DataConfig::class)->getDataClass(static::class);
foreach ($dataClass->properties as $property) {
if (!$property->type->acceptsType("bool")) {
continue;
}
/**
* @var DataProperty $property
*/
// the name can be different in the $properties variable, so let's check if there is an input name mapping
// for the property and use that instead if present.
$propertyRawName = $property->inputMappedName ?? $property->name;
if ($properties->has($propertyRawName)) {
$propertyVal = $properties->get($propertyRawName);
if ($propertyVal === "true") {
$propertyVal = true;
if ($property->type->acceptsType("bool")) {
if ($propertyVal === "true") {
$propertyVal = true;
}
if ($propertyVal === "false") {
$propertyVal = false;
}
}
if ($propertyVal === "false") {
$propertyVal = false;
// if the property is optional and the value is an empty string, we want to ignore it.
if ($property->type->isOptional && $propertyVal === "") {
$propertyVal = null;
}
$properties->put($propertyRawName, $propertyVal);
}
if (!is_null($propertyVal)) {
$properties->put($propertyRawName, $propertyVal);
} else {
$properties->forget($propertyRawName);
}
}
}
return $properties;