Last active
July 22, 2020 08:47
-
-
Save alcaeus/0b66968ee505ab24bdb71fe7c88325a7 to your computer and use it in GitHub Desktop.
DTO example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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