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: /**
27: * Casts given value from a PHP type to one acceptable by database
28: *
29: * @param mixed $value value to be converted to database equivalent
30: * @param \Cake\Database\Driver $driver object from which database preferences and configuration will be extracted
31: * @return string|null
32: */
33: public function toDatabase($value, Driver $driver)
34: {
35: if ($value === null || $value === '') {
36: return null;
37: }
38:
39: return parent::toDatabase($value, $driver);
40: }
41:
42: /**
43: * Generate a new UUID
44: *
45: * @return string A new primary key value.
46: */
47: public function newId()
48: {
49: return Text::uuid();
50: }
51:
52: /**
53: * Marshals request data into a PHP string
54: *
55: * @param mixed $value The value to convert.
56: * @return string|null Converted value.
57: */
58: public function marshal($value)
59: {
60: if ($value === null || $value === '' || is_array($value)) {
61: return null;
62: }
63:
64: return (string)$value;
65: }
66: }
67: