chore: add command to generate dto
- add make:dto command which generates dto in App\Data namespace - varient: --input (default), --output
This commit is contained in:
parent
03f044b8d3
commit
61ecbec994
63
backend/app/Console/Commands/MakeDtoCommand.php
Normal file
63
backend/app/Console/Commands/MakeDtoCommand.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\GeneratorCommand;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
||||
class MakeDtoCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* The console command name and signature.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'make:dto';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Create a new Data Transfer Object class';
|
||||
|
||||
/**
|
||||
* The type of class being generated.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'DTO';
|
||||
|
||||
/**
|
||||
* Get the stub file for the generator.
|
||||
*/
|
||||
protected function getStub(): string
|
||||
{
|
||||
if ($this->option('output')) {
|
||||
return base_path('stubs/dto.output.stub');
|
||||
}
|
||||
|
||||
return base_path('stubs/dto.input.stub');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default namespace for the class.
|
||||
*
|
||||
* @param string $rootNamespace
|
||||
*/
|
||||
protected function getDefaultNamespace($rootNamespace): string
|
||||
{
|
||||
return $rootNamespace.'\Data';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the console command options.
|
||||
*/
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
['input', 'i', InputOption::VALUE_NONE, 'Generate an Input DTO (default)'],
|
||||
['output', 'o', InputOption::VALUE_NONE, 'Generate an Output DTO'],
|
||||
];
|
||||
}
|
||||
}
|
||||
30
backend/stubs/dto.input.stub
Normal file
30
backend/stubs/dto.input.stub
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace {{ namespace }};
|
||||
|
||||
use App\Contracts\InputDataTransferObject;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
final readonly class {{ class }} implements InputDataTransferObject
|
||||
{
|
||||
public function __construct(
|
||||
// TODO: Define your properties here
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
// TODO: Map properties to array
|
||||
];
|
||||
}
|
||||
|
||||
public static function fromRequest(FormRequest $request): InputDataTransferObject
|
||||
{
|
||||
return new self(
|
||||
// TODO: Map request data to properties
|
||||
);
|
||||
}
|
||||
}
|
||||
30
backend/stubs/dto.output.stub
Normal file
30
backend/stubs/dto.output.stub
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace {{ namespace }};
|
||||
|
||||
use App\Contracts\OutputDataTransferObject;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
final readonly class {{ class }} implements OutputDataTransferObject
|
||||
{
|
||||
public function __construct(
|
||||
// TODO: Define your properties here
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
// TODO: Map properties to array
|
||||
];
|
||||
}
|
||||
|
||||
public static function fromModel(Model $model): OutputDataTransferObject
|
||||
{
|
||||
return new self(
|
||||
// TODO: Map model data to properties
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user