PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/PHPCR/Util/UUIDHelper.php

https://github.com/lyrixx/phpcr-utils
PHP | 75 lines | 22 code | 9 blank | 44 comment | 1 complexity | f165539bb7b1ad021ba5e395257b7263 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the PHPCR API and was originally ported from the Java
  4. * JCR API to PHP by Karsten Dambekalns for the FLOW3 project.
  5. *
  6. * Copyright 2008-2011 Karsten Dambekalns <karsten@typo3.org>
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
  21. * @link http://phpcr.github.com/
  22. */
  23. namespace PHPCR\Util;
  24. /**
  25. * static helper functions to deal with UUID's
  26. */
  27. class UUIDHelper
  28. {
  29. /**
  30. * Checks if the string could be a uuid.
  31. *
  32. * @param string $id Possible uuid
  33. * @return boolean True if the test was passed, else false.
  34. */
  35. public static function isUUID($id)
  36. {
  37. // UUID is HEX_CHAR{8}-HEX_CHAR{4}-HEX_CHAR{4}-HEX_CHAR{4}-HEX_CHAR{12}
  38. if (1 === preg_match('/^[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}$/', $id)) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. /**
  44. * Generates a UUID.
  45. *
  46. * @return string
  47. */
  48. public static function generateUUID()
  49. {
  50. return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  51. // 32 bits for "time_low"
  52. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
  53. // 16 bits for "time_mid"
  54. mt_rand( 0, 0xffff ),
  55. // 16 bits for "time_hi_and_version",
  56. // four most significant bits holds version number 4
  57. mt_rand( 0, 0x0fff ) | 0x4000,
  58. // 16 bits, 8 bits for "clk_seq_hi_res",
  59. // 8 bits for "clk_seq_low",
  60. // two most significant bits holds zero and one for variant DCE1.1
  61. mt_rand( 0, 0x3fff ) | 0x8000,
  62. // 48 bits for "node"
  63. mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
  64. );
  65. }
  66. }