DTO code generator for PHP
About
GUI for generation DTO PHP classes in window 11 and linux xubuntu 20.04. Namespace set automatically calculate it from path of catalog with saved file. Full filepath must include catalog src for generate namespace. You enter class name, member type and member name, and programm generic PHP class with members, getters and setters and save it in choosed catalog.
What is DTO
If you begin use DTO, you will not use associative array or PHP standart class for trabsfer data from one subsystem your programm to other subsystem your program.
Instead creating associative array like:
$fileInfo = [
'name' => 'about_my_dto_generator.txt',
'fileCreatedTime' => '2022-02-26 00:00:00',
'fileModifyTime' => '2022-02-26 01:00:00',
'owners' => [
[
'name' => 'john',
'role' => 'user'
],
[
'name' => 'james',
'role' => 'admin'
]
]
];
we write two PHP classes with getters and setters:
declare(strict_types=1);
namespace App\Unit\Socnet\Dto;
use DateTime;
use OwnerDto;
use JsonSerializable;
class FileInfoDto implements JsonSerializable
{
private ?string $name = null;
private ?DateTimeInterface $fileCreatedTime = null;
private ?DateTimeInterface $fileModifyTime = null;
/**
* @var OwnerDto[] $owners
*/
private array $owners;
/**
* @param OwnerDto[] $owners
*/
public function __construct(?string $name, array $owners)
{
$this->name = $name;
$this->owners = $owners;
}
public function getName() : ?string
{
return $this->name;
}
public function setName(?string $name) : self
{
$this->name = $name;
return $this;
}
public function getFileCreatedTime() : ?DateTimeInterface
{
return $this->fileCreatedTime;
}
public function setFileCreatedTime(?DateTimeInterface $fileCreatedTime) : self
{
$this->fileCreatedTime = $fileCreatedTime;
return $this;
}
public function getFileModifyTime() : ?DateTimeInterface
{
return $this->fileModifyTime;
}
public function setFileModifyTime(?DateTimeInterface $fileModifyTime) : self
{
$this->fileModifyTime = $fileModifyTime;
return $this;
}
/**
* @return OwnerDto[]
*/
public function getOwners() : array
{
return $this->owners;
}
/**
* @param OwnerDto[] $owners
*/
public function setOwners(array $owners) : self
{
$this->owners = $owners;
return $this;
}
}
class OwnerDto implements JsonSerializable
{
private ?string $name = null;
private ?string $role = null;
public function __construct(?string $name, ?string $role)
{
$this->name = $name;
$this->role = $role;
}
public function getName() : ?string
{
return $this->name;
}
public function setName(?string $name) : self
{
$this->name = $name;
return $this;
}
public function getRole() : ?string
{
return $this->role;
}
public function setRole(?string $role) : self
{
$this->role = $role;
return $this;
}
}
and use our new classes instead associative array:
$owners = [
new OwnerDto('john', 'user'),
new OwnerDto('james', 'admin')
];
$fileInfo = new FileInfoDto('about_my_dto_generator.txt', $owners);
$fileInfo->setCreatedTime( new DateTime('2022-02-26 00:00:00') );
$fileInfo->setUpdatedTime( new DateTime('2022-02-26 01:00:00') );
Good side this method. If you see in programm code variable type of FileInfoDto, in modern IDE you immediately can mean, what kind of members containts in the this variable. If you see in programm code variable type of array or StdClass, you do not known, what kind fields containts in the this this variable. You must use debugger, or call var_dump function, or research code, and find place, where array or object initalize.
Bad side this method. We must write more code, example you can see early in this text.
DTO PHP class code generator let automatize creating DTO classes.
Next version
In next version I to plane add generation PHP DTO classes from JSON.
Download and install DTO PHP classes code generator.
Windows 11, 7, XP files
Download DTO PHP classes for windowsPre-installation of Qt5 WebView is required, you can see links to both installers below. Unpack the archives and run the Setup files from each of them.