Skip to content

Commit

Permalink
Speed improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Jan 19, 2024
1 parent 8bb91eb commit 5b34518
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
9 changes: 9 additions & 0 deletions src/DataPipes/DataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
use Illuminate\Support\Collection;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataClass;
use Spatie\LaravelData\Support\DataProperty;

interface DataPipe
{
/**
* @param mixed $payload
* @param DataClass $class
* @param Collection<string, mixed> $properties
* @param CreationContext $creationContext
*
* @return Collection
*/
public function handle(
mixed $payload,
DataClass $class,
Expand Down
35 changes: 18 additions & 17 deletions src/DataPipes/DefaultValuesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ public function handle(
Collection $properties,
CreationContext $creationContext
): Collection {
$class
->properties
->filter(fn (DataProperty $property) => ! $properties->has($property->name))
->each(function (DataProperty $property) use (&$properties) {
if ($property->hasDefaultValue) {
$properties[$property->name] = $property->defaultValue;
foreach ($class->properties as $name => $property) {
if($properties->has($name)) {
continue;
}

return;
}
if ($property->hasDefaultValue) {
$properties[$name] = $property->defaultValue;

if ($property->type->isOptional) {
$properties[$property->name] = Optional::create();
continue;
}

return;
}
if ($property->type->isOptional) {
$properties[$name] = Optional::create();

if ($property->type->isNullable) {
$properties[$property->name] = null;
continue;
}

return;
}
});
if ($property->type->isNullable) {
$properties[$name] = null;

continue;
}
}

return $properties;
}
Expand Down
1 change: 0 additions & 1 deletion src/DataPipes/FillRouteParameterPropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ protected function resolveValue(
}

return data_get($parameter, $attribute->property ?? $dataProperty->name);
;
}
}
4 changes: 3 additions & 1 deletion src/Exceptions/CannotCreateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public static function noNormalizerFound(string $dataClass, mixed $value): self

public static function constructorMissingParameters(
DataClass $dataClass,
Collection $parameters,
array $parameters,
Throwable $previous,
): self {
$parameters = collect($parameters);

$message = "Could not create `{$dataClass->name}`: the constructor requires {$dataClass->constructorMethod->parameters->count()} parameters, {$parameters->count()} given.";


Expand Down
38 changes: 22 additions & 16 deletions src/Resolvers/DataFromArrayResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,7 @@ public function execute(string $class, Collection $properties): BaseData
{
$dataClass = $this->dataConfig->getDataClass($class);

$constructorParameters = $dataClass->constructorMethod?->parameters ?? collect();

$data = $constructorParameters
->mapWithKeys(function (DataParameter|DataProperty $parameter) use ($properties) {
if ($properties->has($parameter->name)) {
return [$parameter->name => $properties->get($parameter->name)];
}

if (! $parameter->isPromoted && $parameter->hasDefaultValue) {
return [$parameter->name => $parameter->defaultValue];
}

return [];
})
->pipe(fn (Collection $parameters) => $this->createData($dataClass, $parameters));
$data = $this->createData($dataClass, $properties);

$dataClass
->properties
Expand Down Expand Up @@ -81,8 +67,28 @@ public function execute(string $class, Collection $properties): BaseData

protected function createData(
DataClass $dataClass,
Collection $parameters,
Collection $properties,
) {
$constructorParameters = $dataClass->constructorMethod?->parameters;

if ($constructorParameters === null) {
return new $dataClass->name();
}

$parameters = [];

foreach ($constructorParameters as $parameter) {
if ($properties->has($parameter->name)) {
$parameters[$parameter->name] = $properties->get($parameter->name);

continue;
}

if (! $parameter->isPromoted && $parameter->hasDefaultValue) {
$parameters[$parameter->name] = $parameter->defaultValue;
}
}

try {
return new $dataClass->name(...$parameters);
} catch (ArgumentCountError $error) {
Expand Down

0 comments on commit 5b34518

Please sign in to comment.