vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/Type.php line 279

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. use function str_replace;
  10. use function strrpos;
  11. use function substr;
  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.     /** @deprecated Use {@see Types::BIGINT} instead. */
  20.     public const BIGINT Types::BIGINT;
  21.     /** @deprecated Use {@see Types::BINARY} instead. */
  22.     public const BINARY Types::BINARY;
  23.     /** @deprecated Use {@see Types::BLOB} instead. */
  24.     public const BLOB Types::BLOB;
  25.     /** @deprecated Use {@see Types::BOOLEAN} instead. */
  26.     public const BOOLEAN Types::BOOLEAN;
  27.     /** @deprecated Use {@see Types::DATE_MUTABLE} instead. */
  28.     public const DATE Types::DATE_MUTABLE;
  29.     /** @deprecated Use {@see Types::DATE_IMMUTABLE} instead. */
  30.     public const DATE_IMMUTABLE Types::DATE_IMMUTABLE;
  31.     /** @deprecated Use {@see Types::DATEINTERVAL} instead. */
  32.     public const DATEINTERVAL Types::DATEINTERVAL;
  33.     /** @deprecated Use {@see Types::DATETIME_MUTABLE} instead. */
  34.     public const DATETIME Types::DATETIME_MUTABLE;
  35.     /** @deprecated Use {@see Types::DATETIME_IMMUTABLE} instead. */
  36.     public const DATETIME_IMMUTABLE Types::DATETIME_IMMUTABLE;
  37.     /** @deprecated Use {@see Types::DATETIMETZ_MUTABLE} instead. */
  38.     public const DATETIMETZ Types::DATETIMETZ_MUTABLE;
  39.     /** @deprecated Use {@see Types::DATETIMETZ_IMMUTABLE} instead. */
  40.     public const DATETIMETZ_IMMUTABLE Types::DATETIMETZ_IMMUTABLE;
  41.     /** @deprecated Use {@see Types::DECIMAL} instead. */
  42.     public const DECIMAL Types::DECIMAL;
  43.     /** @deprecated Use {@see Types::FLOAT} instead. */
  44.     public const FLOAT Types::FLOAT;
  45.     /** @deprecated Use {@see Types::GUID} instead. */
  46.     public const GUID Types::GUID;
  47.     /** @deprecated Use {@see Types::INTEGER} instead. */
  48.     public const INTEGER Types::INTEGER;
  49.     /** @deprecated Use {@see Types::JSON} instead. */
  50.     public const JSON Types::JSON;
  51.     /** @deprecated Use {@see Types::JSON_ARRAY} instead. */
  52.     public const JSON_ARRAY Types::JSON_ARRAY;
  53.     /** @deprecated Use {@see Types::OBJECT} instead. */
  54.     public const OBJECT Types::OBJECT;
  55.     /** @deprecated Use {@see Types::SIMPLE_ARRAY} instead. */
  56.     public const SIMPLE_ARRAY Types::SIMPLE_ARRAY;
  57.     /** @deprecated Use {@see Types::SMALLINT} instead. */
  58.     public const SMALLINT Types::SMALLINT;
  59.     /** @deprecated Use {@see Types::STRING} instead. */
  60.     public const STRING Types::STRING;
  61.     /** @deprecated Use {@see Types::ARRAY} instead. */
  62.     public const TARRAY Types::ARRAY;
  63.     /** @deprecated Use {@see Types::TEXT} instead. */
  64.     public const TEXT Types::TEXT;
  65.     /** @deprecated Use {@see Types::TIME_MUTABLE} instead. */
  66.     public const TIME Types::TIME_MUTABLE;
  67.     /** @deprecated Use {@see Types::TIME_IMMUTABLE} instead. */
  68.     public const TIME_IMMUTABLE Types::TIME_IMMUTABLE;
  69.     /**
  70.      * The map of supported doctrine mapping types.
  71.      */
  72.     private const BUILTIN_TYPES_MAP = [
  73.         Types::ARRAY                => ArrayType::class,
  74.         Types::ASCII_STRING         => AsciiStringType::class,
  75.         Types::BIGINT               => BigIntType::class,
  76.         Types::BINARY               => BinaryType::class,
  77.         Types::BLOB                 => BlobType::class,
  78.         Types::BOOLEAN              => BooleanType::class,
  79.         Types::DATE_MUTABLE         => DateType::class,
  80.         Types::DATE_IMMUTABLE       => DateImmutableType::class,
  81.         Types::DATEINTERVAL         => DateIntervalType::class,
  82.         Types::DATETIME_MUTABLE     => DateTimeType::class,
  83.         Types::DATETIME_IMMUTABLE   => DateTimeImmutableType::class,
  84.         Types::DATETIMETZ_MUTABLE   => DateTimeTzType::class,
  85.         Types::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
  86.         Types::DECIMAL              => DecimalType::class,
  87.         Types::FLOAT                => FloatType::class,
  88.         Types::GUID                 => GuidType::class,
  89.         Types::INTEGER              => IntegerType::class,
  90.         Types::JSON                 => JsonType::class,
  91.         Types::JSON_ARRAY           => JsonArrayType::class,
  92.         Types::OBJECT               => ObjectType::class,
  93.         Types::SIMPLE_ARRAY         => SimpleArrayType::class,
  94.         Types::SMALLINT             => SmallIntType::class,
  95.         Types::STRING               => StringType::class,
  96.         Types::TEXT                 => TextType::class,
  97.         Types::TIME_MUTABLE         => TimeType::class,
  98.         Types::TIME_IMMUTABLE       => TimeImmutableType::class,
  99.     ];
  100.     /** @var TypeRegistry|null */
  101.     private static $typeRegistry;
  102.     /**
  103.      * @internal Do not instantiate directly - use {@see Type::addType()} method instead.
  104.      */
  105.     final public function __construct()
  106.     {
  107.     }
  108.     /**
  109.      * Converts a value from its PHP representation to its database representation
  110.      * of this type.
  111.      *
  112.      * @param mixed            $value    The value to convert.
  113.      * @param AbstractPlatform $platform The currently used database platform.
  114.      *
  115.      * @return mixed The database representation of the value.
  116.      */
  117.     public function convertToDatabaseValue($valueAbstractPlatform $platform)
  118.     {
  119.         return $value;
  120.     }
  121.     /**
  122.      * Converts a value from its database representation to its PHP representation
  123.      * of this type.
  124.      *
  125.      * @param mixed            $value    The value to convert.
  126.      * @param AbstractPlatform $platform The currently used database platform.
  127.      *
  128.      * @return mixed The PHP representation of the value.
  129.      */
  130.     public function convertToPHPValue($valueAbstractPlatform $platform)
  131.     {
  132.         return $value;
  133.     }
  134.     /**
  135.      * Gets the default length of this type.
  136.      *
  137.      * @deprecated Rely on information provided by the platform instead.
  138.      *
  139.      * @return int|null
  140.      */
  141.     public function getDefaultLength(AbstractPlatform $platform)
  142.     {
  143.         Deprecation::trigger(
  144.             'doctrine/dbal',
  145.             'https://github.com/doctrine/dbal/pull/3255',
  146.             'Type::getDefaultLength() is deprecated, use AbstractPlatform directly.'
  147.         );
  148.         return null;
  149.     }
  150.     /**
  151.      * Gets the SQL declaration snippet for a column of this type.
  152.      *
  153.      * @param mixed[]          $column   The column definition
  154.      * @param AbstractPlatform $platform The currently used database platform.
  155.      *
  156.      * @return string
  157.      */
  158.     abstract public function getSQLDeclaration(array $columnAbstractPlatform $platform);
  159.     /**
  160.      * Gets the name of this type.
  161.      *
  162.      * @return string
  163.      *
  164.      * @todo Needed?
  165.      */
  166.     abstract public function getName();
  167.     final public static function getTypeRegistry(): TypeRegistry
  168.     {
  169.         if (self::$typeRegistry === null) {
  170.             self::$typeRegistry self::createTypeRegistry();
  171.         }
  172.         return self::$typeRegistry;
  173.     }
  174.     private static function createTypeRegistry(): TypeRegistry
  175.     {
  176.         $instances = [];
  177.         foreach (self::BUILTIN_TYPES_MAP as $name => $class) {
  178.             $instances[$name] = new $class();
  179.         }
  180.         return new TypeRegistry($instances);
  181.     }
  182.     /**
  183.      * Factory method to create type instances.
  184.      * Type instances are implemented as flyweights.
  185.      *
  186.      * @param string $name The name of the type (as returned by getName()).
  187.      *
  188.      * @return Type
  189.      *
  190.      * @throws Exception
  191.      */
  192.     public static function getType($name)
  193.     {
  194.         return self::getTypeRegistry()->get($name);
  195.     }
  196.     /**
  197.      * Adds a custom type to the type map.
  198.      *
  199.      * @param string             $name      The name of the type. This should correspond to what getName() returns.
  200.      * @param class-string<Type> $className The class name of the custom type.
  201.      *
  202.      * @return void
  203.      *
  204.      * @throws Exception
  205.      */
  206.     public static function addType($name$className)
  207.     {
  208.         self::getTypeRegistry()->register($name, new $className());
  209.     }
  210.     /**
  211.      * Checks if exists support for a type.
  212.      *
  213.      * @param string $name The name of the type.
  214.      *
  215.      * @return bool TRUE if type is supported; FALSE otherwise.
  216.      */
  217.     public static function hasType($name)
  218.     {
  219.         return self::getTypeRegistry()->has($name);
  220.     }
  221.     /**
  222.      * Overrides an already defined type to use a different implementation.
  223.      *
  224.      * @param string             $name
  225.      * @param class-string<Type> $className
  226.      *
  227.      * @return void
  228.      *
  229.      * @throws Exception
  230.      */
  231.     public static function overrideType($name$className)
  232.     {
  233.         self::getTypeRegistry()->override($name, new $className());
  234.     }
  235.     /**
  236.      * Gets the (preferred) binding type for values of this type that
  237.      * can be used when binding parameters to prepared statements.
  238.      *
  239.      * This method should return one of the {@link ParameterType} constants.
  240.      *
  241.      * @return int
  242.      */
  243.     public function getBindingType()
  244.     {
  245.         return ParameterType::STRING;
  246.     }
  247.     /**
  248.      * Gets the types array map which holds all registered types and the corresponding
  249.      * type class
  250.      *
  251.      * @return string[]
  252.      */
  253.     public static function getTypesMap()
  254.     {
  255.         return array_map(
  256.             static function (Type $type): string {
  257.                 return get_class($type);
  258.             },
  259.             self::getTypeRegistry()->getMap()
  260.         );
  261.     }
  262.     /**
  263.      * @deprecated Relying on string representation is discouraged and will be removed in DBAL 3.0.
  264.      *
  265.      * @return string
  266.      */
  267.     public function __toString()
  268.     {
  269.         Deprecation::trigger(
  270.             'doctrine/dbal',
  271.             'https://github.com/doctrine/dbal/pull/3258',
  272.             'Type::__toString() is deprecated, use Type::getName() or get_class($type) instead.'
  273.         );
  274.         $type     = static::class;
  275.         $position strrpos($type'\\');
  276.         if ($position !== false) {
  277.             $type substr($type$position);
  278.         }
  279.         return str_replace('Type'''$type);
  280.     }
  281.     /**
  282.      * Does working with this column require SQL conversion functions?
  283.      *
  284.      * This is a metadata function that is required for example in the ORM.
  285.      * Usage of {@link convertToDatabaseValueSQL} and
  286.      * {@link convertToPHPValueSQL} works for any type and mostly
  287.      * does nothing. This method can additionally be used for optimization purposes.
  288.      *
  289.      * @return bool
  290.      */
  291.     public function canRequireSQLConversion()
  292.     {
  293.         return false;
  294.     }
  295.     /**
  296.      * Modifies the SQL expression (identifier, parameter) to convert to a database value.
  297.      *
  298.      * @param string $sqlExpr
  299.      *
  300.      * @return string
  301.      */
  302.     public function convertToDatabaseValueSQL($sqlExprAbstractPlatform $platform)
  303.     {
  304.         return $sqlExpr;
  305.     }
  306.     /**
  307.      * Modifies the SQL expression (identifier, parameter) to convert to a PHP value.
  308.      *
  309.      * @param string           $sqlExpr
  310.      * @param AbstractPlatform $platform
  311.      *
  312.      * @return string
  313.      */
  314.     public function convertToPHPValueSQL($sqlExpr$platform)
  315.     {
  316.         return $sqlExpr;
  317.     }
  318.     /**
  319.      * Gets an array of database types that map to this Doctrine type.
  320.      *
  321.      * @return string[]
  322.      */
  323.     public function getMappedDatabaseTypes(AbstractPlatform $platform)
  324.     {
  325.         return [];
  326.     }
  327.     /**
  328.      * If this Doctrine Type maps to an already mapped database type,
  329.      * reverse schema engineering can't tell them apart. You need to mark
  330.      * one of those types as commented, which will have Doctrine use an SQL
  331.      * comment to typehint the actual Doctrine Type.
  332.      *
  333.      * @return bool
  334.      */
  335.     public function requiresSQLCommentHint(AbstractPlatform $platform)
  336.     {
  337.         return false;
  338.     }
  339. }