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

/src/Dat0r/Core/Field/UuidField.php

https://github.com/berlinonline/Dat0r
PHP | 50 lines | 24 code | 4 blank | 22 comment | 0 complexity | 2fe827de6f1d03f3445ace952355b7e4 MD5 | raw file
  1. <?php
  2. namespace Dat0r\Core\Field;
  3. use Dat0r\Core\ValueHolder\TextValueHolder;
  4. /**
  5. * Concrete implementation of the Field base class.
  6. * Stuff in here is dedicated to handling text.
  7. *
  8. * @copyright BerlinOnline Stadtportal GmbH & Co. KG
  9. * @author Thorsten Schmitt-Rink <tschmittrink@gmail.com>
  10. */
  11. class UuidField extends TextField
  12. {
  13. /**
  14. * Returns the default value of the field.
  15. *
  16. * @return IValueHolder
  17. */
  18. public function getDefaultValue()
  19. {
  20. return $this->generateUuidV4();
  21. }
  22. // from http://www.php.net/manual/en/function.uniqid.php#94959
  23. // more info http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
  24. protected function generateUuidV4()
  25. {
  26. return sprintf(
  27. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  28. // 32 bits for "time_low"
  29. mt_rand(0, 0xffff),
  30. mt_rand(0, 0xffff),
  31. // 16 bits for "time_mid"
  32. mt_rand(0, 0xffff),
  33. // 16 bits for "time_hi_and_version",
  34. // four most significant bits holds version number 4
  35. mt_rand(0, 0x0fff) | 0x4000,
  36. // 16 bits, 8 bits for "clk_seq_hi_res",
  37. // 8 bits for "clk_seq_low",
  38. // two most significant bits holds zero and one for variant DCE1.1
  39. mt_rand(0, 0x3fff) | 0x8000,
  40. // 48 bits for "node"
  41. mt_rand(0, 0xffff),
  42. mt_rand(0, 0xffff),
  43. mt_rand(0, 0xffff)
  44. );
  45. }
  46. }