BinaryUuidType.php 4.4 KB

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