Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace Entity\Id;
- use Ramsey\Uuid\Exception\InvalidUuidStringException;
- use Ramsey\Uuid\Uuid;
- use Entity\Id\Exception\InvalidIdException;
- final class BinaryUuid
- {
- /**
- * @var string
- */
- protected $id;
- public function __construct(string $id = null)
- {
- parent::__construct($id ?? Uuid::uuid4()->toString());
- }
- public function getId(): string
- {
- return $this->id;
- }
- public function __toString()
- {
- return $this->id;
- }
- public function jsonSerialize()
- {
- return $this->id;
- }
- public function getBytes(): string
- {
- return Uuid::fromString($this->id)->getBytes();
- }
- /**
- * @throws InvalidIdException
- */
- public function validate($id): void
- {
- try {
- Uuid::fromString($id);
- } catch (InvalidUuidStringException $e) {
- throw new InvalidIdException($id);
- }
- }
- public function toString(): string
- {
- return $this->__toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement