From 5b34518c4f10de7c271b3dacaa757bcf34cd1c18 Mon Sep 17 00:00:00 2001 From: Ruben Van Assche Date: Fri, 19 Jan 2024 13:45:09 +0100 Subject: [PATCH] Speed improvements --- src/DataPipes/DataPipe.php | 9 +++++ src/DataPipes/DefaultValuesDataPipe.php | 35 ++++++++--------- .../FillRouteParameterPropertiesDataPipe.php | 1 - src/Exceptions/CannotCreateData.php | 4 +- src/Resolvers/DataFromArrayResolver.php | 38 +++++++++++-------- 5 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/DataPipes/DataPipe.php b/src/DataPipes/DataPipe.php index c5ee8df17..91cdb69b1 100644 --- a/src/DataPipes/DataPipe.php +++ b/src/DataPipes/DataPipe.php @@ -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 $properties + * @param CreationContext $creationContext + * + * @return Collection + */ public function handle( mixed $payload, DataClass $class, diff --git a/src/DataPipes/DefaultValuesDataPipe.php b/src/DataPipes/DefaultValuesDataPipe.php index b865068bf..da42cb3b9 100644 --- a/src/DataPipes/DefaultValuesDataPipe.php +++ b/src/DataPipes/DefaultValuesDataPipe.php @@ -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; } diff --git a/src/DataPipes/FillRouteParameterPropertiesDataPipe.php b/src/DataPipes/FillRouteParameterPropertiesDataPipe.php index 394836ca4..ca667e8f5 100644 --- a/src/DataPipes/FillRouteParameterPropertiesDataPipe.php +++ b/src/DataPipes/FillRouteParameterPropertiesDataPipe.php @@ -65,6 +65,5 @@ protected function resolveValue( } return data_get($parameter, $attribute->property ?? $dataProperty->name); - ; } } diff --git a/src/Exceptions/CannotCreateData.php b/src/Exceptions/CannotCreateData.php index b7c48e8c6..2a9f5a600 100644 --- a/src/Exceptions/CannotCreateData.php +++ b/src/Exceptions/CannotCreateData.php @@ -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."; diff --git a/src/Resolvers/DataFromArrayResolver.php b/src/Resolvers/DataFromArrayResolver.php index 33eb7e7de..469ec1920 100644 --- a/src/Resolvers/DataFromArrayResolver.php +++ b/src/Resolvers/DataFromArrayResolver.php @@ -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 @@ -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) {