Skip to content

Instantly share code, notes, and snippets.

@alcaeus
Last active July 22, 2020 08:47
Show Gist options
  • Save alcaeus/0b66968ee505ab24bdb71fe7c88325a7 to your computer and use it in GitHub Desktop.
Save alcaeus/0b66968ee505ab24bdb71fe7c88325a7 to your computer and use it in GitHub Desktop.
DTO example
final class Category
{
private string $name;
public static function fromName(string $name): self
{
return new self($name);
}
private function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function rename(string $name): void
{
$this->name = $name;
}
}
final class EditCategoryProxy
{
private $category;
public static function fromCategory(Category $category): self
{
return new self($category);
}
private function __construct(Category $category)
{
$this->category = $category;
}
public function getName(): ?string
{
return $this->category->getName();
}
public function setName(string $name): void
{
$this->category->rename($name);
}
}
final class EditCategoryTwoStep
{
public $name;
private $category;
private $applied = false;
public static function fromCategory(Category $category): self
{
return new self($category);
}
private function __construct(Category $category)
{
$this->name = $category->getName();
$this->category = $category;
}
public function apply(): void
{
if ($this->applied) {
throw new \LogicException();
}
$this->applied = true;
$this->category->rename($this->name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment