BinaryUtils.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Ramsey\Uuid;
  3. /**
  4. * Provides binary math utilities
  5. */
  6. class BinaryUtils
  7. {
  8. /**
  9. * Applies the RFC 4122 variant field to the `clock_seq_hi_and_reserved` field
  10. *
  11. * @param $clockSeqHi
  12. * @return int The high field of the clock sequence multiplexed with the variant
  13. * @link http://tools.ietf.org/html/rfc4122#section-4.1.1
  14. */
  15. public static function applyVariant($clockSeqHi)
  16. {
  17. // Set the variant to RFC 4122
  18. $clockSeqHi = $clockSeqHi & 0x3f;
  19. $clockSeqHi &= ~(0xc0);
  20. $clockSeqHi |= 0x80;
  21. return $clockSeqHi;
  22. }
  23. /**
  24. * Applies the RFC 4122 version number to the `time_hi_and_version` field
  25. *
  26. * @param string $timeHi
  27. * @param integer $version
  28. * @return int The high field of the timestamp multiplexed with the version number
  29. * @link http://tools.ietf.org/html/rfc4122#section-4.1.3
  30. */
  31. public static function applyVersion($timeHi, $version)
  32. {
  33. $timeHi = hexdec($timeHi) & 0x0fff;
  34. $timeHi &= ~(0xf000);
  35. $timeHi |= $version << 12;
  36. return $timeHi;
  37. }
  38. }