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\ParameterType; |
23
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
24
|
|
|
use Doctrine\DBAL\DBALException; |
25
|
|
|
use function end; |
26
|
|
|
use function explode; |
27
|
|
|
use function get_class; |
28
|
|
|
use function str_replace; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The base class for so-called Doctrine mapping types. |
32
|
|
|
* |
33
|
|
|
* A Type object is obtained by calling the static {@link getType()} method. |
34
|
|
|
* |
35
|
|
|
* @author Roman Borschel <[email protected]> |
36
|
|
|
* @author Benjamin Eberlei <[email protected]> |
37
|
|
|
* @since 2.0 |
38
|
|
|
*/ |
39
|
|
|
abstract class Type |
40
|
|
|
{ |
41
|
|
|
const TARRAY = 'array'; |
42
|
|
|
const SIMPLE_ARRAY = 'simple_array'; |
43
|
|
|
const JSON_ARRAY = 'json_array'; |
44
|
|
|
const JSON = 'json'; |
45
|
|
|
const BIGINT = 'bigint'; |
46
|
|
|
const BOOLEAN = 'boolean'; |
47
|
|
|
const DATETIME = 'datetime'; |
48
|
|
|
const DATETIME_IMMUTABLE = 'datetime_immutable'; |
49
|
|
|
const DATETIMETZ = 'datetimetz'; |
50
|
|
|
const DATETIMETZ_IMMUTABLE = 'datetimetz_immutable'; |
51
|
|
|
const DATE = 'date'; |
52
|
|
|
const DATE_IMMUTABLE = 'date_immutable'; |
53
|
|
|
const TIME = 'time'; |
54
|
|
|
const TIME_IMMUTABLE = 'time_immutable'; |
55
|
|
|
const DECIMAL = 'decimal'; |
56
|
|
|
const INTEGER = 'integer'; |
57
|
|
|
const OBJECT = 'object'; |
58
|
|
|
const SMALLINT = 'smallint'; |
59
|
|
|
const STRING = 'string'; |
60
|
|
|
const TEXT = 'text'; |
61
|
|
|
const BINARY = 'binary'; |
62
|
|
|
const BLOB = 'blob'; |
63
|
|
|
const FLOAT = 'float'; |
64
|
|
|
const GUID = 'guid'; |
65
|
|
|
const DATEINTERVAL = 'dateinterval'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Map of already instantiated type objects. One instance per type (flyweight). |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private static $_typeObjects = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The map of supported doctrine mapping types. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
private static $_typesMap = [ |
80
|
|
|
self::TARRAY => ArrayType::class, |
81
|
|
|
self::SIMPLE_ARRAY => SimpleArrayType::class, |
82
|
|
|
self::JSON_ARRAY => JsonArrayType::class, |
83
|
|
|
self::JSON => JsonType::class, |
84
|
|
|
self::OBJECT => ObjectType::class, |
85
|
|
|
self::BOOLEAN => BooleanType::class, |
86
|
|
|
self::INTEGER => IntegerType::class, |
87
|
|
|
self::SMALLINT => SmallIntType::class, |
88
|
|
|
self::BIGINT => BigIntType::class, |
89
|
|
|
self::STRING => StringType::class, |
90
|
|
|
self::TEXT => TextType::class, |
91
|
|
|
self::DATETIME => DateTimeType::class, |
92
|
|
|
self::DATETIME_IMMUTABLE => DateTimeImmutableType::class, |
93
|
|
|
self::DATETIMETZ => DateTimeTzType::class, |
94
|
|
|
self::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class, |
95
|
|
|
self::DATE => DateType::class, |
96
|
|
|
self::DATE_IMMUTABLE => DateImmutableType::class, |
97
|
|
|
self::TIME => TimeType::class, |
98
|
|
|
self::TIME_IMMUTABLE => TimeImmutableType::class, |
99
|
|
|
self::DECIMAL => DecimalType::class, |
100
|
|
|
self::FLOAT => FloatType::class, |
101
|
|
|
self::BINARY => BinaryType::class, |
102
|
|
|
self::BLOB => BlobType::class, |
103
|
|
|
self::GUID => GuidType::class, |
104
|
|
|
self::DATEINTERVAL => DateIntervalType::class, |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Prevents instantiation and forces use of the factory method. |
109
|
|
|
*/ |
110
|
71 |
|
final private function __construct() |
111
|
|
|
{ |
112
|
71 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Converts a value from its PHP representation to its database representation |
116
|
|
|
* of this type. |
117
|
|
|
* |
118
|
|
|
* @param mixed $value The value to convert. |
119
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform. |
120
|
|
|
* |
121
|
|
|
* @return mixed The database representation of the value. |
122
|
|
|
*/ |
123
|
304 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
124
|
|
|
{ |
125
|
304 |
|
return $value; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Converts a value from its database representation to its PHP representation |
130
|
|
|
* of this type. |
131
|
|
|
* |
132
|
|
|
* @param mixed $value The value to convert. |
133
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform. |
134
|
|
|
* |
135
|
|
|
* @return mixed The PHP representation of the value. |
136
|
|
|
*/ |
137
|
95 |
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
138
|
|
|
{ |
139
|
95 |
|
return $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Converts a given value from a database reprentation or flexibly from an existing |
144
|
|
|
* value to a value which will be used to hydrate an object. |
145
|
|
|
* |
146
|
|
|
* If $param is of type DateTime and convertToPHPValue returns a DateTime this function |
147
|
|
|
* should return the original DateTime. If the return value should be an integer and |
148
|
|
|
* a string is passed then the value should be typecast to an integer. This function |
149
|
|
|
* may be implemented differently for each field type. |
150
|
|
|
* |
151
|
|
|
* This also allows custom field types to render uniquely. |
152
|
|
|
* |
153
|
|
|
* @param mixed $value The value to convert. |
154
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform. |
155
|
|
|
* |
156
|
|
|
* @return mixed The type specific representation of the value. |
157
|
|
|
*/ |
158
|
|
|
public function handleTypeConversion($value, AbstractPlatform $platform) |
159
|
|
|
{ |
160
|
|
|
return $this->convertToPHPValue($value, $platform); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets the default length of this type. |
165
|
|
|
* |
166
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
167
|
|
|
* |
168
|
|
|
* @return int|null |
169
|
|
|
* |
170
|
|
|
* @deprecated Rely on information provided by the platform instead. |
171
|
|
|
*/ |
172
|
|
|
public function getDefaultLength(AbstractPlatform $platform) |
173
|
|
|
{ |
174
|
|
|
return null; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets the SQL declaration snippet for a field of this type. |
179
|
|
|
* |
180
|
|
|
* @param array $fieldDeclaration The field declaration. |
181
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform The currently used database platform. |
182
|
|
|
* |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
33157 |
|
abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform); |
186
|
|
|
|
187
|
33157 |
|
/** |
188
|
71 |
|
* Gets the name of this type. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
71 |
|
* |
192
|
|
|
* @todo Needed? |
193
|
|
|
*/ |
194
|
33157 |
|
abstract public function getName(); |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Factory method to create type instances. |
198
|
|
|
* Type instances are implemented as flyweights. |
199
|
|
|
* |
200
|
|
|
* @param string $name The name of the type (as returned by getName()). |
201
|
|
|
* |
202
|
|
|
* @return \Doctrine\DBAL\Types\Type |
203
|
|
|
* |
204
|
|
|
* @throws \Doctrine\DBAL\DBALException |
205
|
|
|
*/ |
206
|
|
|
public static function getType($name) |
207
|
71 |
|
{ |
208
|
|
|
if ( ! isset(self::$_typeObjects[$name])) { |
209
|
71 |
|
if ( ! isset(self::$_typesMap[$name])) { |
210
|
|
|
throw DBALException::unknownColumnType($name); |
211
|
|
|
} |
212
|
|
|
self::$_typeObjects[$name] = new self::$_typesMap[$name](); |
213
|
71 |
|
} |
214
|
71 |
|
|
215
|
|
|
return self::$_typeObjects[$name]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Adds a custom type to the type map. |
220
|
|
|
* |
221
|
|
|
* @param string $name The name of the type. This should correspond to what getName() returns. |
222
|
|
|
* @param string $className The class name of the custom type. |
223
|
1873 |
|
* |
224
|
|
|
* @return void |
225
|
1873 |
|
* |
226
|
|
|
* @throws \Doctrine\DBAL\DBALException |
227
|
|
|
*/ |
228
|
|
|
public static function addType($name, $className) |
229
|
|
|
{ |
230
|
|
|
if (isset(self::$_typesMap[$name])) { |
231
|
|
|
throw DBALException::typeExists($name); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
self::$_typesMap[$name] = $className; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Checks if exists support for a type. |
239
|
|
|
* |
240
|
|
|
* @param string $name The name of the type. |
241
|
|
|
* |
242
|
|
|
* @return bool TRUE if type is supported; FALSE otherwise. |
243
|
|
|
*/ |
244
|
|
|
public static function hasType($name) |
245
|
|
|
{ |
246
|
|
|
return isset(self::$_typesMap[$name]); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Overrides an already defined type to use a different implementation. |
251
|
|
|
* |
252
|
|
|
* @param string $name |
253
|
|
|
* @param string $className |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
* |
257
|
|
|
* @throws \Doctrine\DBAL\DBALException |
258
|
|
|
*/ |
259
|
418 |
|
public static function overrideType($name, $className) |
260
|
|
|
{ |
261
|
418 |
|
if ( ! isset(self::$_typesMap[$name])) { |
262
|
|
|
throw DBALException::typeNotFound($name); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if (isset(self::$_typeObjects[$name])) { |
266
|
|
|
unset(self::$_typeObjects[$name]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
self::$_typesMap[$name] = $className; |
270
|
18188 |
|
} |
271
|
|
|
|
272
|
18188 |
|
/** |
273
|
|
|
* Gets the (preferred) binding type for values of this type that |
274
|
|
|
* can be used when binding parameters to prepared statements. |
275
|
|
|
* |
276
|
|
|
* This method should return one of the {@link \Doctrine\DBAL\ParameterType} constants. |
277
|
|
|
* |
278
|
|
|
* @return int |
279
|
|
|
*/ |
280
|
|
|
public function getBindingType() |
281
|
|
|
{ |
282
|
|
|
return ParameterType::STRING; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Gets the types array map which holds all registered types and the corresponding |
287
|
|
|
* type class |
288
|
|
|
* |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public static function getTypesMap() |
292
|
|
|
{ |
293
|
|
|
return self::$_typesMap; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
19 |
|
* @return string |
298
|
|
|
* |
299
|
19 |
|
* @deprecated Relying on string representation is discouraged and will be removed in DBAL 3.0. |
300
|
|
|
*/ |
301
|
|
|
public function __toString() |
302
|
|
|
{ |
303
|
|
|
$e = explode('\\', get_class($this)); |
304
|
|
|
|
305
|
|
|
return str_replace('Type', '', end($e)); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Does working with this column require SQL conversion functions? |
310
|
19 |
|
* |
311
|
|
|
* This is a metadata function that is required for example in the ORM. |
312
|
19 |
|
* Usage of {@link convertToDatabaseValueSQL} and |
313
|
|
|
* {@link convertToPHPValueSQL} works for any type and mostly |
314
|
|
|
* does nothing. This method can additionally be used for optimization purposes. |
315
|
|
|
* |
316
|
|
|
* @return bool |
317
|
|
|
*/ |
318
|
|
|
public function canRequireSQLConversion() |
319
|
|
|
{ |
320
|
|
|
return false; |
321
|
|
|
} |
322
|
|
|
|
323
|
19 |
|
/** |
324
|
|
|
* Modifies the SQL expression (identifier, parameter) to convert to a database value. |
325
|
19 |
|
* |
326
|
|
|
* @param string $sqlExpr |
327
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
328
|
|
|
* |
329
|
|
|
* @return string |
330
|
|
|
*/ |
331
|
|
|
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) |
|
|
|
|
332
|
|
|
{ |
333
|
|
|
return $sqlExpr; |
334
|
|
|
} |
335
|
1881 |
|
|
336
|
|
|
/** |
337
|
1881 |
|
* Modifies the SQL expression (identifier, parameter) to convert to a PHP value. |
338
|
|
|
* |
339
|
|
|
* @param string $sqlExpr |
340
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
341
|
|
|
* |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public function convertToPHPValueSQL($sqlExpr, $platform) |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
return $sqlExpr; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
17054 |
|
* Gets an array of database types that map to this Doctrine type. |
351
|
|
|
* |
352
|
17054 |
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
353
|
|
|
* |
354
|
|
|
* @return array |
355
|
|
|
*/ |
356
|
|
|
public function getMappedDatabaseTypes(AbstractPlatform $platform) |
357
|
|
|
{ |
358
|
|
|
return []; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* If this Doctrine Type maps to an already mapped database type, |
363
|
|
|
* reverse schema engineering can't tell them apart. You need to mark |
364
|
|
|
* one of those types as commented, which will have Doctrine use an SQL |
365
|
|
|
* comment to typehint the actual Doctrine Type. |
366
|
|
|
* |
367
|
|
|
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform |
368
|
|
|
* |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
372
|
|
|
{ |
373
|
|
|
return false; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.