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

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. * use {@see TypeRegistry::lookupName()} instead.
  95. *
  96. * @return string
  97. */
  98. abstract public function getName();
  99. final public static function getTypeRegistry(): TypeRegistry
  100. {
  101. return self::$typeRegistry ??= self::createTypeRegistry();
  102. }
  103. private static function createTypeRegistry(): TypeRegistry
  104. {
  105. $instances = [];
  106. foreach (self::BUILTIN_TYPES_MAP as $name => $class) {
  107. $instances[$name] = new $class();
  108. }
  109. return new TypeRegistry($instances);
  110. }
  111. /**
  112. * Factory method to create type instances.
  113. * Type instances are implemented as flyweights.
  114. *
  115. * @param string $name The name of the type (as returned by getName()).
  116. *
  117. * @return Type
  118. *
  119. * @throws Exception
  120. */
  121. public static function getType($name)
  122. {
  123. return self::getTypeRegistry()->get($name);
  124. }
  125. /**
  126. * Finds a name for the given type.
  127. *
  128. * @throws Exception
  129. */
  130. public static function lookupName(self $type): string
  131. {
  132. return self::getTypeRegistry()->lookupName($type);
  133. }
  134. /**
  135. * Adds a custom type to the type map.
  136. *
  137. * @param string $name The name of the type. This should correspond to what getName() returns.
  138. * @param class-string<Type> $className The class name of the custom type.
  139. *
  140. * @return void
  141. *
  142. * @throws Exception
  143. */
  144. public static function addType($name, $className)
  145. {
  146. self::getTypeRegistry()->register($name, new $className());
  147. }
  148. /**
  149. * Checks if exists support for a type.
  150. *
  151. * @param string $name The name of the type.
  152. *
  153. * @return bool TRUE if type is supported; FALSE otherwise.
  154. */
  155. public static function hasType($name)
  156. {
  157. return self::getTypeRegistry()->has($name);
  158. }
  159. /**
  160. * Overrides an already defined type to use a different implementation.
  161. *
  162. * @param string $name
  163. * @param class-string<Type> $className
  164. *
  165. * @return void
  166. *
  167. * @throws Exception
  168. */
  169. public static function overrideType($name, $className)
  170. {
  171. self::getTypeRegistry()->override($name, new $className());
  172. }
  173. /**
  174. * Gets the (preferred) binding type for values of this type that
  175. * can be used when binding parameters to prepared statements.
  176. *
  177. * This method should return one of the {@see ParameterType} constants.
  178. *
  179. * @return int
  180. */
  181. public function getBindingType()
  182. {
  183. return ParameterType::STRING;
  184. }
  185. /**
  186. * Gets the types array map which holds all registered types and the corresponding
  187. * type class
  188. *
  189. * @return array<string, string>
  190. */
  191. public static function getTypesMap()
  192. {
  193. return array_map(
  194. static function (Type $type): string {
  195. return get_class($type);
  196. },
  197. self::getTypeRegistry()->getMap(),
  198. );
  199. }
  200. /**
  201. * Does working with this column require SQL conversion functions?
  202. *
  203. * This is a metadata function that is required for example in the ORM.
  204. * Usage of {@see convertToDatabaseValueSQL} and
  205. * {@see convertToPHPValueSQL} works for any type and mostly
  206. * does nothing. This method can additionally be used for optimization purposes.
  207. *
  208. * @deprecated Consumers should call {@see convertToDatabaseValueSQL} and {@see convertToPHPValueSQL}
  209. * regardless of the type.
  210. *
  211. * @return bool
  212. */
  213. public function canRequireSQLConversion()
  214. {
  215. return false;
  216. }
  217. /**
  218. * Modifies the SQL expression (identifier, parameter) to convert to a database value.
  219. *
  220. * @param string $sqlExpr
  221. *
  222. * @return string
  223. */
  224. public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
  225. {
  226. return $sqlExpr;
  227. }
  228. /**
  229. * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
  230. *
  231. * @param string $sqlExpr
  232. * @param AbstractPlatform $platform
  233. *
  234. * @return string
  235. */
  236. public function convertToPHPValueSQL($sqlExpr, $platform)
  237. {
  238. return $sqlExpr;
  239. }
  240. /**
  241. * Gets an array of database types that map to this Doctrine type.
  242. *
  243. * @return string[]
  244. */
  245. public function getMappedDatabaseTypes(AbstractPlatform $platform)
  246. {
  247. return [];
  248. }
  249. /**
  250. * If this Doctrine Type maps to an already mapped database type,
  251. * reverse schema engineering can't tell them apart. You need to mark
  252. * one of those types as commented, which will have Doctrine use an SQL
  253. * comment to typehint the actual Doctrine Type.
  254. *
  255. * @deprecated
  256. *
  257. * @return bool
  258. */
  259. public function requiresSQLCommentHint(AbstractPlatform $platform)
  260. {
  261. Deprecation::triggerIfCalledFromOutside(
  262. 'doctrine/dbal',
  263. 'https://github.com/doctrine/dbal/pull/5509',
  264. '%s is deprecated.',
  265. __METHOD__,
  266. );
  267. return false;
  268. }
  269. }