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

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