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