Browse Source

Implement the TypeInterface into all type subclasses and remove from the Type class.

David Yell 9 years ago
parent
commit
d63423eebd

+ 1 - 1
src/Database/Type.php

@@ -21,7 +21,7 @@ use PDO;
  * Encapsulates all conversion functions for values coming from database into PHP and
  * going from PHP into database.
  */
-class Type implements TypeInterface
+class Type
 {
 
     /**

+ 47 - 2
src/Database/Type/BinaryType.php

@@ -17,7 +17,7 @@ namespace Cake\Database\Type;
 use Cake\Core\Exception\Exception;
 use Cake\Database\Driver;
 use Cake\Database\Driver\Sqlserver;
-use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use PDO;
 
 /**
@@ -25,10 +25,40 @@ use PDO;
  *
  * Use to convert binary data between PHP and the database types.
  */
-class BinaryType extends Type
+class BinaryType implements TypeInterface
 {
 
     /**
+     * Identifier name for this type
+     *
+     * @var string
+     */
+    protected $_name = null;
+
+    /**
+     * Constructor
+     *
+     * @param string|null $name The name identifying this type
+     */
+    public function __construct($name = null)
+    {
+        $this->_name = $name;
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
      * Convert binary data into the database format.
      *
      * Binary data is not altered before being inserted into the database.
@@ -79,4 +109,19 @@ class BinaryType extends Type
     {
         return PDO::PARAM_LOB;
     }
+
+    /**
+     * Marshalls flat data into PHP objects.
+     *
+     * Most useful for converting request data into PHP objects
+     * that make sense for the rest of the ORM/Database layers.
+     *
+     * @param mixed $value The value to convert.
+     *
+     * @return mixed Converted value.
+     */
+    public function marshal($value)
+    {
+        return $value;
+    }
 }

+ 32 - 2
src/Database/Type/BoolType.php

@@ -15,7 +15,7 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
-use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use InvalidArgumentException;
 use PDO;
 
@@ -24,10 +24,40 @@ use PDO;
  *
  * Use to convert bool data between PHP and the database types.
  */
-class BoolType extends Type
+class BoolType implements TypeInterface
 {
 
     /**
+     * Identifier name for this type
+     *
+     * @var string
+     */
+    protected $_name = null;
+
+    /**
+     * Constructor
+     *
+     * @param string|null $name The name identifying this type
+     */
+    public function __construct($name = null)
+    {
+        $this->_name = $name;
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
      * Convert bool data into the database format.
      *
      * @param mixed $value The value to convert.

+ 25 - 4
src/Database/Type/DateTimeType.php

@@ -15,9 +15,10 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
-use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use DateTimeInterface;
 use Exception;
+use PDO;
 use RuntimeException;
 
 /**
@@ -25,7 +26,7 @@ use RuntimeException;
  *
  * Use to convert datetime instances to strings & back.
  */
-class DateTimeType extends Type
+class DateTimeType implements TypeInterface
 {
 
     /**
@@ -77,11 +78,18 @@ class DateTimeType extends Type
     protected $_className;
 
     /**
+     * Identifier name for this type
+     *
+     * @var string
+     */
+    protected $_name = null;
+
+    /**
      * {@inheritDoc}
      */
     public function __construct($name = null)
     {
-        parent::__construct($name);
+        $this->_name = $name;
         $this->_setClassName(static::$dateTimeClass, 'DateTime');
     }
 
@@ -273,7 +281,7 @@ class DateTimeType extends Type
     }
 
     /**
-     * Converts a string into a DateTime object after parseing it using the locale
+     * Converts a string into a DateTime object after parsing it using the locale
      * aware parser with the specified format.
      *
      * @param string $value The value to parse and convert to an object.
@@ -285,4 +293,17 @@ class DateTimeType extends Type
 
         return $class::parseDateTime($value, $this->_localeFormat);
     }
+
+    /**
+     * Casts given value to Statement equivalent
+     *
+     * @param mixed $value value to be converted to PDO statement
+     * @param \Cake\Database\Driver $driver object from which database preferences and configuration will be extracted
+     *
+     * @return mixed
+     */
+    public function toStatement($value, Driver $driver)
+    {
+        return PDO::PARAM_STR;
+    }
 }

+ 2 - 1
src/Database/Type/DateType.php

@@ -15,9 +15,10 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
+use Cake\Database\TypeInterface;
 use DateTime;
 
-class DateType extends DateTimeType
+class DateType extends DateTimeType implements TypeInterface
 {
 
     /**

+ 32 - 2
src/Database/Type/FloatType.php

@@ -15,7 +15,7 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
-use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use PDO;
 use RuntimeException;
 
@@ -24,10 +24,40 @@ use RuntimeException;
  *
  * Use to convert float/decimal data between PHP and the database types.
  */
-class FloatType extends Type
+class FloatType implements TypeInterface
 {
 
     /**
+     * Identifier name for this type
+     *
+     * @var string
+     */
+    protected $_name = null;
+
+    /**
+     * Constructor
+     *
+     * @param string|null $name The name identifying this type
+     */
+    public function __construct($name = null)
+    {
+        $this->_name = $name;
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
      * The class to use for representing number objects
      *
      * @var string

+ 46 - 2
src/Database/Type/IntegerType.php

@@ -15,7 +15,7 @@
 namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
-use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use InvalidArgumentException;
 use PDO;
 
@@ -24,10 +24,54 @@ use PDO;
  *
  * Use to convert integer data between PHP and the database types.
  */
-class IntegerType extends Type
+class IntegerType implements TypeInterface
 {
 
     /**
+     * Identifier name for this type
+     *
+     * @var string
+     */
+    protected $_name = null;
+
+    /**
+     * Constructor
+     *
+     * @param string|null $name The name identifying this type
+     */
+    public function __construct($name = null)
+    {
+        $this->_name = $name;
+    }
+
+    /**
+     * 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;
+    }
+
+    /**
+     * Generate a new primary key value for a given type.
+     *
+     * This method can be used by types to create new primary key values
+     * when entities are inserted.
+     *
+     * @return mixed A new primary key value.
+     * @see \Cake\Database\Type\UuidType
+     */
+    public function newId()
+    {
+        return null;
+    }
+
+    /**
      * Convert integer data into the database format.
      *
      * @param mixed $value The value to convert.

+ 2 - 1
src/Database/Type/StringType.php

@@ -16,6 +16,7 @@ namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
 use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use InvalidArgumentException;
 use PDO;
 
@@ -24,7 +25,7 @@ use PDO;
  *
  * Use to convert string data between PHP and the database types.
  */
-class StringType extends Type implements OptionalConvertInterface
+class StringType extends Type implements OptionalConvertInterface, TypeInterface
 {
 
     /**

+ 3 - 1
src/Database/Type/TimeType.php

@@ -14,12 +14,14 @@
  */
 namespace Cake\Database\Type;
 
+use Cake\Database\TypeInterface;
+
 /**
  * Time type converter.
  *
  * Use to convert time instances to strings & back.
  */
-class TimeType extends DateTimeType
+class TimeType extends DateTimeType implements TypeInterface
 {
 
     /**

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

@@ -16,12 +16,13 @@ namespace Cake\Database\Type;
 
 use Cake\Database\Driver;
 use Cake\Database\Type;
+use Cake\Database\TypeInterface;
 use Cake\Utility\Text;
 
 /**
  * Provides behavior for the UUID type
  */
-class UuidType extends StringType
+class UuidType extends StringType implements TypeInterface
 {
 
     /**

+ 1 - 1
src/Database/TypeConverterTrait.php

@@ -33,7 +33,7 @@ trait TypeConverterTrait
         if (is_string($type)) {
             $type = Type::build($type);
         }
-        if ($type instanceof Type) {
+        if ($type instanceof TypeInterface) {
             $value = $type->toDatabase($value, $this->_driver);
             $type = $type->toStatement($value, $this->_driver);
         }