Guest User

Untitled

a guest
Feb 1st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. | | VO | DTO |
  2. |----------|:-------------:|------:|
  3. | _Data_ | yes | yes |
  4. | _Logic_ | yes | no |
  5. | _Identity_ | no | no |
  6. | _Immutability_ | yes | no |
  7.  
  8. ### VO
  9. ```php
  10. final class Address
  11. {
  12. private $street;
  13. private $unit;
  14. private $room;
  15.  
  16. public function __construct(string $street, int $unit, ?int $room = null)
  17. {
  18. $this->street = $street;
  19. $this->unit = $this->handleNumberArgument($unit);
  20. $this->room = $room ? $this->handleNumberArgument($room) : null;
  21. }
  22.  
  23. public function assignRoom(int $room): self
  24. {
  25. if (null !== $this->room) {
  26. throw new LogicException('The room is already assigned to this address');
  27. }
  28.  
  29. return new self($this->street, $this->unit, $room);
  30. }
  31.  
  32. public function __toString(): string
  33. {
  34. return implode(',', [$this->street, $this->unit, $this->room ?? '']);
  35. }
  36.  
  37. private function handleNumberArgument($value): int
  38. {
  39. if ($value <= 0) {
  40. throw new InvalidArgumentException('Number must be more than \'0\'');
  41. }
  42.  
  43. return $value;
  44. }
  45. }
  46. ```
  47.  
  48. ### DTO
  49.  
  50. ```php
  51. final class UserRegisterRequest
  52. {
  53. public const PASSWORD_MIN_LENGTH = 6;
  54.  
  55. public $username;
  56. public $password;
  57.  
  58. private function __construct(string $username, string $password)
  59. {
  60. $this->username = $username;
  61. $this->assertPasswordLength($password);
  62. $this->password = $password;
  63. }
  64.  
  65. public static function create(string $username, string $password): self
  66. {
  67. return new self($username, $password);
  68. }
  69.  
  70. private function assertPasswordLength(string $password)
  71. {
  72. if (self::PASSWORD_MIN_LENGTH >= strlen($password)) {
  73. throw new InvalidArgumentException('Password must be more than 6 characters.');
  74. }
  75. }
  76. }
  77. ```
Add Comment
Please, Sign In to add comment