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.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Type;
16:
17: use Cake\Database\Driver;
18: use Cake\Utility\Text;
19:
20: /**
21: * Provides behavior for the UUID type
22: */
23: class UuidType extends StringType
24: {
25: /**
26: * Casts given value from a PHP type to one acceptable by database
27: *
28: * @param mixed $value value to be converted to database equivalent
29: * @param \Cake\Database\Driver $driver object from which database preferences and configuration will be extracted
30: * @return string|null
31: */
32: public function toDatabase($value, Driver $driver)
33: {
34: if ($value === null || $value === '') {
35: return null;
36: }
37:
38: return parent::toDatabase($value, $driver);
39: }
40:
41: /**
42: * Generate a new UUID
43: *
44: * @return string A new primary key value.
45: */
46: public function newId()
47: {
48: return Text::uuid();
49: }
50:
51: /**
52: * Marshals request data into a PHP string
53: *
54: * @param mixed $value The value to convert.
55: * @return string|null Converted value.
56: */
57: public function marshal($value)
58: {
59: if ($value === null || $value === '' || is_array($value)) {
60: return null;
61: }
62:
63: return (string)$value;
64: }
65: }
66: