ソースを参照

Adding a Type::getBaseType() to hint other part of the framework about extensions

Jose Lorenzo Rodriguez 10 年 前
コミット
8488eed87c
2 ファイル変更19 行追加0 行削除
  1. 13 0
      src/Database/Type.php
  2. 6 0
      tests/TestCase/Database/TypeTest.php

+ 13 - 0
src/Database/Type.php

@@ -164,6 +164,19 @@ class Type
     }
 
     /**
+     * Returns the base type name that this class is inheriting.
+     * This is useful when extending base type for adding extra functionality
+     * but still want the rest of the framework to use the same assumptions it would
+     * do about the base type it inherits from.
+     *
+     * @return string
+     */
+    public function getBaseType()
+    {
+        return $this->_name;
+    }
+
+    /**
      * Casts given value from a PHP type to one acceptable by database
      *
      * @param mixed $value value to be converted to database equivalent

+ 6 - 0
tests/TestCase/Database/TypeTest.php

@@ -25,6 +25,10 @@ use PDO;
 class FooType extends \Cake\Database\Type
 {
 
+    public function getBaseType()
+    {
+        return 'text';
+    }
 }
 
 /**
@@ -74,6 +78,7 @@ class TypeTest extends TestCase
         $type = Type::build($name);
         $this->assertInstanceOf('Cake\Database\Type', $type);
         $this->assertEquals($name, $type->getName());
+        $this->assertEquals($name, $type->getBaseType());
     }
 
     /**
@@ -133,6 +138,7 @@ class TypeTest extends TestCase
         $type = Type::build('foo');
         $this->assertInstanceOf($fooType, $type);
         $this->assertEquals('foo', $type->getName());
+        $this->assertEquals('text', $type->getBaseType());
     }
 
     /**