Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace Entity\Id;
  6.  
  7. use Ramsey\Uuid\Exception\InvalidUuidStringException;
  8. use Ramsey\Uuid\Uuid;
  9. use Entity\Id\Exception\InvalidIdException;
  10.  
  11. final class BinaryUuid
  12. {
  13. /**
  14. * @var string
  15. */
  16. protected $id;
  17.  
  18. public function __construct(string $id = null)
  19. {
  20. parent::__construct($id ?? Uuid::uuid4()->toString());
  21. }
  22.  
  23. public function getId(): string
  24. {
  25. return $this->id;
  26. }
  27.  
  28. public function __toString()
  29. {
  30. return $this->id;
  31. }
  32.  
  33. public function jsonSerialize()
  34. {
  35. return $this->id;
  36. }
  37.  
  38. public function getBytes(): string
  39. {
  40. return Uuid::fromString($this->id)->getBytes();
  41. }
  42.  
  43. /**
  44. * @throws InvalidIdException
  45. */
  46. public function validate($id): void
  47. {
  48. try {
  49. Uuid::fromString($id);
  50. } catch (InvalidUuidStringException $e) {
  51. throw new InvalidIdException($id);
  52. }
  53. }
  54.  
  55. public function toString(): string
  56. {
  57. return $this->__toString();
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement