Browse Source

Making all type objects throw exceptions on bad data sent to the database #7494

Jose Lorenzo Rodriguez 10 years ago
parent
commit
77e74d5d5d

+ 5 - 5
src/Database/Type/IntegerType.php

@@ -16,6 +16,7 @@ namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
 use Cake\Database\Type;
+use InvalidArgumentException;
 use PDO;
 
 /**
@@ -38,9 +39,11 @@ class IntegerType extends Type
         if ($value === null || $value === '') {
             return null;
         }
-        if (is_array($value)) {
-            return 1;
+
+        if (!is_scalar($value)) {
+            throw new InvalidArgumentException('Cannot convert value to integer');
         }
+
         return (int)$value;
     }
 
@@ -56,9 +59,6 @@ class IntegerType extends Type
         if ($value === null) {
             return null;
         }
-        if (is_array($value)) {
-            return 1;
-        }
         return (int)$value;
     }
 

+ 1 - 53
src/Database/Type/UuidType.php

@@ -22,54 +22,8 @@ use PDO;
 /**
  * Provides behavior for the uuid type
  */
-class UuidType extends Type
+class UuidType extends StringType
 {
-
-    /**
-     * Casts give value to Statement equivalent
-     *
-     * @param mixed $value value to be converted to PHP equivalent
-     * @param Driver $driver object from which database preferences and configuration will be extracted
-     * @return mixed
-     */
-    public function toStatement($value, Driver $driver)
-    {
-        if ($value === null) {
-            return PDO::PARAM_NULL;
-        }
-        return PDO::PARAM_STR;
-    }
-
-    /**
-     * Casts given value from a PHP type to one acceptable by database
-     *
-     * @param mixed $value value to be converted to database equivalent
-     * @param Driver $driver object from which database preferences and configuration will be extracted
-     * @return mixed
-     */
-    public function toDatabase($value, Driver $driver)
-    {
-        if ($value === null || $value === '') {
-            return null;
-        }
-        return strval($value);
-    }
-
-    /**
-     * Casts given value from a database type to PHP equivalent
-     *
-     * @param mixed $value value to be converted to PHP equivalent
-     * @param Driver $driver object from which database preferences and configuration will be extracted
-     * @return mixed
-     */
-    public function toPHP($value, Driver $driver)
-    {
-        if ($value === null) {
-            return null;
-        }
-        return strval($value);
-    }
-
     /**
      * Generate a new UUID
      *
@@ -80,12 +34,6 @@ 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 === '') {

+ 10 - 2
tests/TestCase/Database/Type/IntegerTypeTest.php

@@ -74,9 +74,17 @@ class IntegerTypeTest extends TestCase
 
         $result = $this->type->toDatabase('2', $this->driver);
         $this->assertSame(2, $result);
+    }
 
-        $result = $this->type->toDatabase(['3', '4'], $this->driver);
-        $this->assertSame(1, $result);
+    /**
+     * Tests that passing an invalid value will throw an exception
+     *
+     * @expectedException InvalidArgumentException
+     * @return void
+     */
+    public function testToDatabseInvalid()
+    {
+        $this->type->toDatabase(['3', '4'], $this->driver);
     }
 
     /**