From f64724aced54731a9868af0957ccd6c93ec2e316 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Wed, 16 Sep 2015 09:48:58 +0200 Subject: [PATCH] Making UuidType marshal empty strings as null --- src/Database/Type/UuidType.php | 14 ++++++++++++++ tests/TestCase/Database/Type/UuidTypeTest.php | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Database/Type/UuidType.php b/src/Database/Type/UuidType.php index eb62a423af3..767d80a3e0c 100644 --- a/src/Database/Type/UuidType.php +++ b/src/Database/Type/UuidType.php @@ -79,4 +79,18 @@ public function newId() { return Text::uuid(); } + + /** + * Marshalls request data into a PHP string + * + * @param mixed $value The value to convert. + * @return string|null Converted value. + */ + public function marshal($value) + { + if ($value === null || $value === '') { + return null; + } + return (string)$value; + } } diff --git a/tests/TestCase/Database/Type/UuidTypeTest.php b/tests/TestCase/Database/Type/UuidTypeTest.php index 45b5b3c71fa..3c52146bb3f 100644 --- a/tests/TestCase/Database/Type/UuidTypeTest.php +++ b/tests/TestCase/Database/Type/UuidTypeTest.php @@ -91,4 +91,14 @@ public function testNewId() $this->assertRegExp('/^[a-f0-9-]+$/', $one, 'Should quack like a uuid'); $this->assertRegExp('/^[a-f0-9-]+$/', $two, 'Should quack like a uuid'); } + + /** + * Tests that marshalling an empty string results in null + * + * @return void + */ + public function testMarshalEmptyString() + { + $this->assertNull($this->type->marshal('')); + } }