VersionTrait.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * This file is part of the ramsey/uuid library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Uuid\Rfc4122;
  13. /**
  14. * Provides common functionality for handling the version, as defined by RFC 4122
  15. *
  16. * @psalm-immutable
  17. */
  18. trait VersionTrait
  19. {
  20. /**
  21. * Returns the version
  22. */
  23. abstract public function getVersion(): ?int;
  24. /**
  25. * Returns true if these fields represent a nil UUID
  26. */
  27. abstract public function isNil(): bool;
  28. /**
  29. * Returns true if the version matches one of those defined by RFC 4122
  30. *
  31. * @return bool True if the UUID version is valid, false otherwise
  32. */
  33. private function isCorrectVersion(): bool
  34. {
  35. if ($this->isNil()) {
  36. return true;
  37. }
  38. switch ($this->getVersion()) {
  39. case 1:
  40. case 2:
  41. case 3:
  42. case 4:
  43. case 5:
  44. case 6:
  45. return true;
  46. }
  47. return false;
  48. }
  49. }