Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Types / Type.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Types;
21
22 use Doctrine\DBAL\Platforms\AbstractPlatform,
23     Doctrine\DBAL\DBALException;
24
25 /**
26  * The base class for so-called Doctrine mapping types.
27  *
28  * A Type object is obtained by calling the static {@link getType()} method.
29  *
30  * @author Roman Borschel <roman@code-factory.org>
31  * @author Benjamin Eberlei <kontakt@beberlei.de>
32  * @since 2.0
33  */
34 abstract class Type
35 {
36     const TARRAY = 'array';
37     const SIMPLE_ARRAY = 'simple_array';
38     const JSON_ARRAY = 'json_array';
39     const BIGINT = 'bigint';
40     const BOOLEAN = 'boolean';
41     const DATETIME = 'datetime';
42     const DATETIMETZ = 'datetimetz';
43     const DATE = 'date';
44     const TIME = 'time';
45     const DECIMAL = 'decimal';
46     const INTEGER = 'integer';
47     const OBJECT = 'object';
48     const SMALLINT = 'smallint';
49     const STRING = 'string';
50     const TEXT = 'text';
51     const BLOB = 'blob';
52     const FLOAT = 'float';
53     const GUID = 'guid';
54
55     /** Map of already instantiated type objects. One instance per type (flyweight). */
56     private static $_typeObjects = array();
57
58     /** The map of supported doctrine mapping types. */
59     private static $_typesMap = array(
60         self::TARRAY => 'Doctrine\DBAL\Types\ArrayType',
61         self::SIMPLE_ARRAY => 'Doctrine\DBAL\Types\SimpleArrayType',
62         self::JSON_ARRAY => 'Doctrine\DBAL\Types\JsonArrayType',
63         self::OBJECT => 'Doctrine\DBAL\Types\ObjectType',
64         self::BOOLEAN => 'Doctrine\DBAL\Types\BooleanType',
65         self::INTEGER => 'Doctrine\DBAL\Types\IntegerType',
66         self::SMALLINT => 'Doctrine\DBAL\Types\SmallIntType',
67         self::BIGINT => 'Doctrine\DBAL\Types\BigIntType',
68         self::STRING => 'Doctrine\DBAL\Types\StringType',
69         self::TEXT => 'Doctrine\DBAL\Types\TextType',
70         self::DATETIME => 'Doctrine\DBAL\Types\DateTimeType',
71         self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
72         self::DATE => 'Doctrine\DBAL\Types\DateType',
73         self::TIME => 'Doctrine\DBAL\Types\TimeType',
74         self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType',
75         self::FLOAT => 'Doctrine\DBAL\Types\FloatType',
76         self::BLOB => 'Doctrine\DBAL\Types\BlobType',
77         self::GUID => 'Doctrine\DBAL\Types\GuidType',
78     );
79
80     /* Prevent instantiation and force use of the factory method. */
81     final private function __construct() {}
82
83     /**
84      * Converts a value from its PHP representation to its database representation
85      * of this type.
86      *
87      * @param mixed $value The value to convert.
88      * @param AbstractPlatform $platform The currently used database platform.
89      * @return mixed The database representation of the value.
90      */
91     public function convertToDatabaseValue($value, AbstractPlatform $platform)
92     {
93         return $value;
94     }
95
96     /**
97      * Converts a value from its database representation to its PHP representation
98      * of this type.
99      *
100      * @param mixed $value The value to convert.
101      * @param AbstractPlatform $platform The currently used database platform.
102      * @return mixed The PHP representation of the value.
103      */
104     public function convertToPHPValue($value, AbstractPlatform $platform)
105     {
106         return $value;
107     }
108
109     /**
110      * Gets the default length of this type.
111      *
112      * @todo Needed?
113      */
114     public function getDefaultLength(AbstractPlatform $platform)
115     {
116         return null;
117     }
118
119     /**
120      * Gets the SQL declaration snippet for a field of this type.
121      *
122      * @param array $fieldDeclaration The field declaration.
123      * @param AbstractPlatform $platform The currently used database platform.
124      */
125     abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform);
126
127     /**
128      * Gets the name of this type.
129      *
130      * @return string
131      * @todo Needed?
132      */
133     abstract public function getName();
134
135     /**
136      * Factory method to create type instances.
137      * Type instances are implemented as flyweights.
138      *
139      * @static
140      * @throws DBALException
141      * @param string $name The name of the type (as returned by getName()).
142      * @return \Doctrine\DBAL\Types\Type
143      */
144     public static function getType($name)
145     {
146         if ( ! isset(self::$_typeObjects[$name])) {
147             if ( ! isset(self::$_typesMap[$name])) {
148                 throw DBALException::unknownColumnType($name);
149             }
150             self::$_typeObjects[$name] = new self::$_typesMap[$name]();
151         }
152
153         return self::$_typeObjects[$name];
154     }
155
156     /**
157      * Adds a custom type to the type map.
158      *
159      * @static
160      * @param string $name Name of the type. This should correspond to what getName() returns.
161      * @param string $className The class name of the custom type.
162      * @throws DBALException
163      */
164     public static function addType($name, $className)
165     {
166         if (isset(self::$_typesMap[$name])) {
167             throw DBALException::typeExists($name);
168         }
169
170         self::$_typesMap[$name] = $className;
171     }
172
173     /**
174      * Checks if exists support for a type.
175      *
176      * @static
177      * @param string $name Name of the type
178      * @return boolean TRUE if type is supported; FALSE otherwise
179      */
180     public static function hasType($name)
181     {
182         return isset(self::$_typesMap[$name]);
183     }
184
185     /**
186      * Overrides an already defined type to use a different implementation.
187      *
188      * @static
189      * @param string $name
190      * @param string $className
191      * @throws DBALException
192      */
193     public static function overrideType($name, $className)
194     {
195         if ( ! isset(self::$_typesMap[$name])) {
196             throw DBALException::typeNotFound($name);
197         }
198
199         if (isset(self::$_typeObjects[$name])) {
200             unset(self::$_typeObjects[$name]);
201         }
202
203         self::$_typesMap[$name] = $className;
204     }
205
206     /**
207      * Gets the (preferred) binding type for values of this type that
208      * can be used when binding parameters to prepared statements.
209      *
210      * This method should return one of the PDO::PARAM_* constants, that is, one of:
211      *
212      * PDO::PARAM_BOOL
213      * PDO::PARAM_NULL
214      * PDO::PARAM_INT
215      * PDO::PARAM_STR
216      * PDO::PARAM_LOB
217      *
218      * @return integer
219      */
220     public function getBindingType()
221     {
222         return \PDO::PARAM_STR;
223     }
224
225     /**
226      * Get the types array map which holds all registered types and the corresponding
227      * type class
228      *
229      * @return array $typesMap
230      */
231     public static function getTypesMap()
232     {
233         return self::$_typesMap;
234     }
235
236     public function __toString()
237     {
238         $e = explode('\\', get_class($this));
239         return str_replace('Type', '', end($e));
240     }
241
242     /**
243      * Does working with this column require SQL conversion functions?
244      *
245      * This is a metadata function that is required for example in the ORM.
246      * Usage of {@link convertToDatabaseValueSQL} and
247      * {@link convertToPHPValueSQL} works for any type and mostly
248      * does nothing. This method can additionally be used for optimization purposes.
249      *
250      * @return bool
251      */
252     public function canRequireSQLConversion()
253     {
254         return false;
255     }
256
257     /**
258      * Modifies the SQL expression (identifier, parameter) to convert to a database value.
259      *
260      * @param string $sqlExpr
261      * @param AbstractPlatform $platform
262      * @return string
263      */
264     public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
265     {
266         return $sqlExpr;
267     }
268
269     /**
270      * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
271      *
272      * @param string $sqlExpr
273      * @param AbstractPlatform $platform
274      * @return string
275      */
276     public function convertToPHPValueSQL($sqlExpr, $platform)
277     {
278         return $sqlExpr;
279     }
280
281     /**
282      * Get an array of database types that map to this Doctrine type.
283      *
284      * @param AbstractPlatform $platform
285      * @return array
286      */
287     public function getMappedDatabaseTypes(AbstractPlatform $platform)
288     {
289         return array();
290     }
291
292     /**
293      * If this Doctrine Type maps to an already mapped database type,
294      * reverse schema engineering can't take them apart. You need to mark
295      * one of those types as commented, which will have Doctrine use an SQL
296      * comment to typehint the actual Doctrine Type.
297      *
298      * @param AbstractPlatform $platform
299      * @return bool
300      */
301     public function requiresSQLCommentHint(AbstractPlatform $platform)
302     {
303         return false;
304     }
305 }
306