vendor/doctrine/dbal/src/Types/Type.php line 150

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Types;
  3. use Doctrine\DBAL\Exception;
  4. use Doctrine\DBAL\ParameterType;
  5. use Doctrine\DBAL\Platforms\AbstractPlatform;
  6. use Doctrine\Deprecations\Deprecation;
  7. use function array_map;
  8. use function get_class;
  9. /**
  10. * The base class for so-called Doctrine mapping types.
  11. *
  12. * A Type object is obtained by calling the static {@see getType()} method.
  13. */
  14. abstract class Type
  15. {
  16. /**
  17. * The map of supported doctrine mapping types.
  18. */
  19. private const BUILTIN_TYPES_MAP = [
  20. Types::ARRAY => ArrayType::class,
  21. Types::ASCII_STRING => AsciiStringType::class,
  22. Types::BIGINT => BigIntType::class,
  23. Types::BINARY => BinaryType::class,
  24. Types::BLOB => BlobType::class,
  25. Types::BOOLEAN => BooleanType::class,
  26. Types::DATE_MUTABLE => DateType::class,
  27. Types::DATE_IMMUTABLE => DateImmutableType::class,
  28. Types::DATEINTERVAL => DateIntervalType::class,
  29. Types::DATETIME_MUTABLE => DateTimeType::class,
  30. Types::DATETIME_IMMUTABLE => DateTimeImmutableType::class,
  31. Types::DATETIMETZ_MUTABLE => DateTimeTzType::class,
  32. Types::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
  33. Types::DECIMAL => DecimalType::class,
  34. Types::FLOAT => FloatType::class,
  35. Types::GUID => GuidType::class,
  36. Types::INTEGER => IntegerType::class,
  37. Types::JSON => JsonType::class,
  38. Types::OBJECT => ObjectType::class,
  39. Types::SIMPLE_ARRAY => SimpleArrayType::class,
  40. Types::SMALLINT => SmallIntType::class,
  41. Types::STRING => StringType::class,
  42. Types::TEXT => TextType::class,
  43. Types::TIME_MUTABLE => TimeType::class,
  44. Types::TIME_IMMUTABLE => TimeImmutableType::class,
  45. ];
  46. private static ?TypeRegistry $typeRegistry = null;
  47. /** @internal Do not instantiate directly - use {@see Type::addType()} method instead. */
  48. final public function __construct()
  49. {
  50. }
  51. /**
  52. * Converts a value from its PHP representation to its database representation
  53. * of this type.
  54. *
  55. * @param mixed $value The value to convert.
  56. * @param AbstractPlatform $platform The currently used database platform.
  57. *
  58. * @return mixed The database representation of the value.
  59. *
  60. * @throws ConversionException
  61. */
  62. public function convertToDatabaseValue($value, AbstractPlatform $platform)
  63. {
  64. return $value;
  65. }
  66. /**
  67. * Converts a value from its database representation to its PHP representation
  68. * of this type.
  69. *
  70. * @param mixed $value The value to convert.
  71. * @param AbstractPlatform $platform The currently used database platform.
  72. *
  73. * @return mixed The PHP representation of the value.
  74. *
  75. * @throws ConversionException
  76. */
  77. public function convertToPHPValue($value, AbstractPlatform $platform)
  78. {
  79. return $value;
  80. }
  81. /**
  82. * Gets the SQL declaration snippet for a column of this type.
  83. *
  84. * @param mixed[] $column The column definition
  85. * @param AbstractPlatform $platform The currently used database platform.
  86. *
  87. * @return string
  88. */
  89. abstract public function getSQLDeclaration(array $column, AbstractPlatform $platform);
  90. /**
  91. * Gets the name of this type.
  92. *
  93. * @deprecated this method will be removed in Doctrine DBAL 4.0.
  94. *
  95. * @return string
  96. */
  97. abstract public function getName();
  98. final public static function getTypeRegistry(): TypeRegistry
  99. {
  100. return self::$typeRegistry ??= self::createTypeRegistry();
  101. }
  102. private static function createTypeRegistry(): TypeRegistry
  103. {
  104. $instances = [];
  105. foreach (self::BUILTIN_TYPES_MAP as $name => $class) {
  106. $instances[$name] = new $class();
  107. }
  108. return new TypeRegistry($instances);
  109. }
  110. /**
  111. * Factory method to create type instances.
  112. * Type instances are implemented as flyweights.
  113. *
  114. * @param string $name The name of the type (as returned by getName()).
  115. *
  116. * @return Type
  117. *
  118. * @throws Exception
  119. */
  120. public static function getType($name)
  121. {
  122. return self::getTypeRegistry()->get($name);
  123. }
  124. /**
  125. * Adds a custom type to the type map.
  126. *
  127. * @param string $name The name of the type. This should correspond to what getName() returns.
  128. * @param class-string<Type> $className The class name of the custom type.
  129. *
  130. * @return void
  131. *
  132. * @throws Exception
  133. */
  134. public static function addType($name, $className)
  135. {
  136. self::getTypeRegistry()->register($name, new $className());
  137. }
  138. /**
  139. * Checks if exists support for a type.
  140. *
  141. * @param string $name The name of the type.
  142. *
  143. * @return bool TRUE if type is supported; FALSE otherwise.
  144. */
  145. public static function hasType($name)
  146. {
  147. return self::getTypeRegistry()->has($name);
  148. }
  149. /**
  150. * Overrides an already defined type to use a different implementation.
  151. *
  152. * @param string $name
  153. * @param class-string<Type> $className
  154. *
  155. * @return void
  156. *
  157. * @throws Exception
  158. */
  159. public static function overrideType($name, $className)
  160. {
  161. self::getTypeRegistry()->override($name, new $className());
  162. }
  163. /**
  164. * Gets the (preferred) binding type for values of this type that
  165. * can be used when binding parameters to prepared statements.
  166. *
  167. * This method should return one of the {@see ParameterType} constants.
  168. *
  169. * @return int
  170. */
  171. public function getBindingType()
  172. {
  173. return ParameterType::STRING;
  174. }
  175. /**
  176. * Gets the types array map which holds all registered types and the corresponding
  177. * type class
  178. *
  179. * @return array<string, string>
  180. */
  181. public static function getTypesMap()
  182. {
  183. return array_map(
  184. static function (Type $type): string {
  185. return get_class($type);
  186. },
  187. self::getTypeRegistry()->getMap(),
  188. );
  189. }
  190. /**
  191. * Does working with this column require SQL conversion functions?
  192. *
  193. * This is a metadata function that is required for example in the ORM.
  194. * Usage of {@see convertToDatabaseValueSQL} and
  195. * {@see convertToPHPValueSQL} works for any type and mostly
  196. * does nothing. This method can additionally be used for optimization purposes.
  197. *
  198. * @deprecated Consumers should call {@see convertToDatabaseValueSQL} and {@see convertToPHPValueSQL}
  199. * regardless of the type.
  200. *
  201. * @return bool
  202. */
  203. public function canRequireSQLConversion()
  204. {
  205. return false;
  206. }
  207. /**
  208. * Modifies the SQL expression (identifier, parameter) to convert to a database value.
  209. *
  210. * @param string $sqlExpr
  211. *
  212. * @return string
  213. */
  214. public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
  215. {
  216. return $sqlExpr;
  217. }
  218. /**
  219. * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
  220. *
  221. * @param string $sqlExpr
  222. * @param AbstractPlatform $platform
  223. *
  224. * @return string
  225. */
  226. public function convertToPHPValueSQL($sqlExpr, $platform)
  227. {
  228. return $sqlExpr;
  229. }
  230. /**
  231. * Gets an array of database types that map to this Doctrine type.
  232. *
  233. * @return string[]
  234. */
  235. public function getMappedDatabaseTypes(AbstractPlatform $platform)
  236. {
  237. return [];
  238. }
  239. /**
  240. * If this Doctrine Type maps to an already mapped database type,
  241. * reverse schema engineering can't tell them apart. You need to mark
  242. * one of those types as commented, which will have Doctrine use an SQL
  243. * comment to typehint the actual Doctrine Type.
  244. *
  245. * @deprecated
  246. *
  247. * @return bool
  248. */
  249. public function requiresSQLCommentHint(AbstractPlatform $platform)
  250. {
  251. Deprecation::triggerIfCalledFromOutside(
  252. 'doctrine/dbal',
  253. 'https://github.com/doctrine/dbal/pull/5509',
  254. '%s is deprecated.',
  255. __METHOD__,
  256. );
  257. return false;
  258. }
  259. }