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