BinaryUuidType.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.6.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Database\Type;
  17. use Cake\Core\Exception\Exception;
  18. use Cake\Database\DriverInterface;
  19. use Cake\Utility\Text;
  20. use PDO;
  21. /**
  22. * Binary UUID type converter.
  23. *
  24. * Use to convert binary uuid data between PHP and the database types.
  25. */
  26. class BinaryUuidType extends BaseType
  27. {
  28. /**
  29. * Convert binary uuid data into the database format.
  30. *
  31. * Binary data is not altered before being inserted into the database.
  32. * As PDO will handle reading file handles.
  33. *
  34. * @param string|resource $value The value to convert.
  35. * @param \Cake\Database\DriverInterface $driver The driver instance to convert with.
  36. * @return string|resource
  37. */
  38. public function toDatabase($value, DriverInterface $driver)
  39. {
  40. if (is_string($value)) {
  41. return $this->convertStringToBinaryUuid($value);
  42. }
  43. return $value;
  44. }
  45. /**
  46. * Generate a new binary UUID
  47. *
  48. * @return string A new primary key value.
  49. */
  50. public function newId(): string
  51. {
  52. return Text::uuid();
  53. }
  54. /**
  55. * Convert binary uuid into resource handles
  56. *
  57. * @param null|string|resource $value The value to convert.
  58. * @param \Cake\Database\DriverInterface $driver The driver instance to convert with.
  59. * @return resource|string|null
  60. * @throws \Cake\Core\Exception\Exception
  61. */
  62. public function toPHP($value, DriverInterface $driver)
  63. {
  64. if ($value === null) {
  65. return null;
  66. }
  67. if (is_string($value)) {
  68. return $this->convertBinaryUuidToString($value);
  69. }
  70. if (is_resource($value)) {
  71. return $value;
  72. }
  73. throw new Exception(sprintf('Unable to convert %s into binary uuid.', gettype($value)));
  74. }
  75. /**
  76. * Get the correct PDO binding type for Binary data.
  77. *
  78. * @param mixed $value The value being bound.
  79. * @param \Cake\Database\DriverInterface $driver The driver.
  80. * @return int
  81. */
  82. public function toStatement($value, DriverInterface $driver): int
  83. {
  84. return PDO::PARAM_LOB;
  85. }
  86. /**
  87. * Marshals flat data into PHP objects.
  88. *
  89. * Most useful for converting request data into PHP objects
  90. * that make sense for the rest of the ORM/Database layers.
  91. *
  92. * @param mixed $value The value to convert.
  93. *
  94. * @return mixed Converted value.
  95. */
  96. public function marshal($value)
  97. {
  98. return $value;
  99. }
  100. /**
  101. * Converts a binary uuid to a string representation
  102. *
  103. *
  104. * @param mixed $binary The value to convert.
  105. *
  106. * @return string Converted value.
  107. */
  108. protected function convertBinaryUuidToString($binary): string
  109. {
  110. $string = unpack("H*", $binary);
  111. $string = preg_replace(
  112. "/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/",
  113. "$1-$2-$3-$4-$5",
  114. $string
  115. );
  116. return $string[1];
  117. }
  118. /**
  119. * Converts a string uuid to a binary representation
  120. *
  121. *
  122. * @param string $string The value to convert.
  123. *
  124. * @return string Converted value.
  125. */
  126. protected function convertStringToBinaryUuid($string): string
  127. {
  128. $string = str_replace('-', '', $string);
  129. return pack("H*", $string);
  130. }
  131. }