BinaryUtils.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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;
  13. /**
  14. * Provides binary math utilities
  15. */
  16. class BinaryUtils
  17. {
  18. /**
  19. * Applies the variant field to the 16-bit clock sequence
  20. *
  21. * @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field
  22. *
  23. * @param int $clockSeq The 16-bit clock sequence value before the variant is applied
  24. *
  25. * @return int The 16-bit clock sequence multiplexed with the UUID variant
  26. *
  27. * @pure
  28. */
  29. public static function applyVariant(int $clockSeq): int
  30. {
  31. return ($clockSeq & 0x3fff) | 0x8000;
  32. }
  33. /**
  34. * Applies the version field to the 16-bit `time_hi_and_version` field
  35. *
  36. * @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field
  37. *
  38. * @param int $timeHi The value of the 16-bit `time_hi_and_version` field before the version is applied
  39. * @param int $version The version to apply to the `time_hi` field
  40. *
  41. * @return int The 16-bit time_hi field of the timestamp multiplexed with the UUID version number
  42. *
  43. * @pure
  44. */
  45. public static function applyVersion(int $timeHi, int $version): int
  46. {
  47. return ($timeHi & 0x0fff) | ($version << 12);
  48. }
  49. }