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

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