Browse Source

Merge pull request #12478 from cakephp/3.next-type-marshalling

3.next type marshalling
Mark Story 7 years ago
parent
commit
63c65c2604

+ 3 - 0
src/Database/Type/BoolType.php

@@ -161,6 +161,9 @@ class BoolType extends Type implements TypeInterface, BatchCastingInterface
         if ($value === 'false') {
             return false;
         }
+        if (!is_scalar($value)) {
+            return null;
+        }
 
         return !empty($value);
     }

+ 1 - 4
src/Database/Type/DecimalType.php

@@ -157,11 +157,8 @@ class DecimalType extends Type implements TypeInterface, BatchCastingInterface
         if (is_numeric($value)) {
             return (float)$value;
         }
-        if (is_array($value)) {
-            return 1;
-        }
 
-        return $value;
+        return null;
     }
 
     /**

+ 3 - 6
src/Database/Type/FloatType.php

@@ -140,17 +140,14 @@ class FloatType extends Type implements TypeInterface, BatchCastingInterface
         if ($value === null || $value === '') {
             return null;
         }
-        if (is_numeric($value)) {
-            return (float)$value;
-        }
         if (is_string($value) && $this->_useLocaleParser) {
             return $this->_parseValue($value);
         }
-        if (is_array($value)) {
-            return 1.0;
+        if (is_numeric($value)) {
+            return (float)$value;
         }
 
-        return $value;
+        return null;
     }
 
     /**

+ 1 - 4
src/Database/Type/IntegerType.php

@@ -130,12 +130,9 @@ class IntegerType extends Type implements TypeInterface, BatchCastingInterface
         if ($value === null || $value === '') {
             return null;
         }
-        if (is_numeric($value) || ctype_digit($value)) {
+        if (is_numeric($value)) {
             return (int)$value;
         }
-        if (is_array($value)) {
-            return 1;
-        }
 
         return null;
     }

+ 1 - 1
tests/TestCase/Database/Type/BoolTypeTest.php

@@ -157,7 +157,7 @@ class BoolTypeTest extends TestCase
         $this->assertFalse($this->type->marshal(0));
         $this->assertFalse($this->type->marshal(''));
         $this->assertTrue($this->type->marshal('not empty'));
-        $this->assertTrue($this->type->marshal(['2', '3']));
+        $this->assertNull($this->type->marshal(['2', '3']));
     }
 
     /**

+ 3 - 3
tests/TestCase/Database/Type/DecimalTypeTest.php

@@ -159,7 +159,7 @@ class DecimalTypeTest extends TestCase
     public function testMarshal()
     {
         $result = $this->type->marshal('some data');
-        $this->assertSame('some data', $result);
+        $this->assertNull($result);
 
         $result = $this->type->marshal('');
         $this->assertNull($result);
@@ -168,10 +168,10 @@ class DecimalTypeTest extends TestCase
         $this->assertSame(2.51, $result);
 
         $result = $this->type->marshal('3.5 bears');
-        $this->assertSame('3.5 bears', $result);
+        $this->assertNull($result);
 
         $result = $this->type->marshal(['3', '4']);
-        $this->assertSame(1, $result);
+        $this->assertNull($result);
     }
 
     /**

+ 3 - 3
tests/TestCase/Database/Type/FloatTypeTest.php

@@ -148,7 +148,7 @@ class FloatTypeTest extends TestCase
     public function testMarshal()
     {
         $result = $this->type->marshal('some data');
-        $this->assertSame('some data', $result);
+        $this->assertNull($result);
 
         $result = $this->type->marshal('');
         $this->assertNull($result);
@@ -157,10 +157,10 @@ class FloatTypeTest extends TestCase
         $this->assertSame(2.51, $result);
 
         $result = $this->type->marshal('3.5 bears');
-        $this->assertSame('3.5 bears', $result);
+        $this->assertNull($result);
 
         $result = $this->type->marshal(['3', '4']);
-        $this->assertSame(1.0, $result);
+        $this->assertNull($result);
     }
 
     /**

+ 8 - 1
tests/TestCase/Database/Type/IntegerTypeTest.php

@@ -159,7 +159,14 @@ class IntegerTypeTest extends TestCase
         $this->assertNull($result);
 
         $result = $this->type->marshal(['3', '4']);
-        $this->assertSame(1, $result);
+        $this->assertNull($result);
+
+        $result = $this->type->marshal('+0123.45e2');
+        if (version_compare(PHP_VERSION, '7.1', '<')) {
+            $this->assertSame(123, $result);
+        } else {
+            $this->assertSame(12345, $result);
+        }
     }
 
     /**