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

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