Browse Source

Merge pull request #7407 from cakephp/uuid-marshal-null

Making UuidType marshal empty strings as null
Mark Story 10 years ago
parent
commit
6bc08930d4
2 changed files with 24 additions and 0 deletions
  1. 14 0
      src/Database/Type/UuidType.php
  2. 10 0
      tests/TestCase/Database/Type/UuidTypeTest.php

+ 14 - 0
src/Database/Type/UuidType.php

@@ -79,4 +79,18 @@ class UuidType extends Type
     {
         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;
+    }
 }

+ 10 - 0
tests/TestCase/Database/Type/UuidTypeTest.php

@@ -91,4 +91,14 @@ class UuidTypeTest extends TestCase
         $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(''));
+    }
 }