BinaryUtils.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 RFC 4122 variant field to the 16-bit clock sequence
  20. *
  21. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant
  22. *
  23. * @param int $clockSeq The 16-bit clock sequence value before the RFC 4122
  24. * variant is applied
  25. *
  26. * @return int The 16-bit clock sequence multiplexed with the UUID variant
  27. *
  28. * @psalm-pure
  29. */
  30. public static function applyVariant(int $clockSeq): int
  31. {
  32. $clockSeq = $clockSeq & 0x3fff;
  33. $clockSeq |= 0x8000;
  34. return $clockSeq;
  35. }
  36. /**
  37. * Applies the RFC 4122 version number to the 16-bit `time_hi_and_version` field
  38. *
  39. * @link http://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version
  40. *
  41. * @param int $timeHi The value of the 16-bit `time_hi_and_version` field
  42. * before the RFC 4122 version is applied
  43. * @param int $version The RFC 4122 version to apply to the `time_hi` field
  44. *
  45. * @return int The 16-bit time_hi field of the timestamp multiplexed with
  46. * the UUID version number
  47. *
  48. * @psalm-pure
  49. */
  50. public static function applyVersion(int $timeHi, int $version): int
  51. {
  52. $timeHi = $timeHi & 0x0fff;
  53. $timeHi |= $version << 12;
  54. return $timeHi;
  55. }
  56. }