Skip to content

Commit

Permalink
getDto() function now firstly tries to retrieve the Transformer DTO i…
Browse files Browse the repository at this point in the history
…f it is attached.
  • Loading branch information
eelco2k committed Oct 30, 2024
1 parent 2e9de2b commit e153d10
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Http/Handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Routing\Router;
use Illuminate\Support\Str;
use Rupadana\ApiService\ApiService;
use Rupadana\ApiService\Attributes\UsesDTO;
use Rupadana\ApiService\Exceptions\TransformerNotFoundException;
use Rupadana\ApiService\Traits\HasHandlerTenantScope;
use Rupadana\ApiService\Contracts\HasAllowedFields;
Expand Down Expand Up @@ -104,9 +105,15 @@ public static function getModel()

public static function getDto(): ?string
{
$modelReflection = new ReflectionClass(static::getModel());
if (property_exists(static::getModel(), 'dataClass')) {
return $modelReflection->getProperty('dataClass')->getDefaultValue();
$transformerReflection = new ReflectionClass(static::getApiTransformer());
$transformerAttributes = $transformerReflection->getAttributes(UsesDTO::class);
if (!empty($transformerAttributes)) {
return $transformerAttributes[0]->newInstance()->dtoClass;
} else {
$modelReflection = new ReflectionClass(static::getModel());
if (property_exists(static::getModel(), 'dataClass')) {
return $modelReflection->getProperty('dataClass')->getDefaultValue();
}
}
return null;
}
Expand Down
1 change: 0 additions & 1 deletion stubs/ApiTransformer.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace {{ namespace }};

{{ dtoNamespace }}

use Rupadana\ApiService\Attributes\UsesDTO;
use Illuminate\Http\Resources\Json\JsonResource;

Expand Down

0 comments on commit e153d10

Please sign in to comment.