vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/ConversionException.php line 36

Open in your IDE?
  1. <?php
  2. namespace Doctrine\DBAL\Types;
  3. use Doctrine\DBAL\Exception;
  4. use Throwable;
  5. use function get_class;
  6. use function gettype;
  7. use function implode;
  8. use function is_object;
  9. use function is_scalar;
  10. use function sprintf;
  11. use function strlen;
  12. use function substr;
  13. /**
  14.  * Conversion Exception is thrown when the database to PHP conversion fails.
  15.  *
  16.  * @psalm-immutable
  17.  */
  18. class ConversionException extends Exception
  19. {
  20.     /**
  21.      * Thrown when a Database to Doctrine Type Conversion fails.
  22.      *
  23.      * @param string $value
  24.      * @param string $toType
  25.      *
  26.      * @return ConversionException
  27.      */
  28.     public static function conversionFailed($value$toType, ?Throwable $previous null)
  29.     {
  30.         $value strlen($value) > 32 substr($value020) . '...' $value;
  31.         return new self('Could not convert database value "' $value '" to Doctrine Type ' $toType0$previous);
  32.     }
  33.     /**
  34.      * Thrown when a Database to Doctrine Type Conversion fails and we can make a statement
  35.      * about the expected format.
  36.      *
  37.      * @param string $value
  38.      * @param string $toType
  39.      * @param string $expectedFormat
  40.      *
  41.      * @return ConversionException
  42.      */
  43.     public static function conversionFailedFormat($value$toType$expectedFormat, ?Throwable $previous null)
  44.     {
  45.         $value strlen($value) > 32 substr($value020) . '...' $value;
  46.         return new self(
  47.             'Could not convert database value "' $value '" to Doctrine Type ' .
  48.             $toType '. Expected format: ' $expectedFormat,
  49.             0,
  50.             $previous
  51.         );
  52.     }
  53.     /**
  54.      * Thrown when the PHP value passed to the converter was not of the expected type.
  55.      *
  56.      * @param mixed    $value
  57.      * @param string   $toType
  58.      * @param string[] $possibleTypes
  59.      *
  60.      * @return ConversionException
  61.      */
  62.     public static function conversionFailedInvalidType(
  63.         $value,
  64.         $toType,
  65.         array $possibleTypes,
  66.         ?Throwable $previous null
  67.     ) {
  68.         $actualType is_object($value) ? get_class($value) : gettype($value);
  69.         if (is_scalar($value)) {
  70.             return new self(sprintf(
  71.                 "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
  72.                 $value,
  73.                 $actualType,
  74.                 $toType,
  75.                 implode(', '$possibleTypes)
  76.             ), 0$previous);
  77.         }
  78.         return new self(sprintf(
  79.             "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
  80.             $actualType,
  81.             $toType,
  82.             implode(', '$possibleTypes)
  83.         ), 0$previous);
  84.     }
  85.     /**
  86.      * @param mixed  $value
  87.      * @param string $format
  88.      * @param string $error
  89.      *
  90.      * @return ConversionException
  91.      */
  92.     public static function conversionFailedSerialization($value$format$error)
  93.     {
  94.         $actualType is_object($value) ? get_class($value) : gettype($value);
  95.         return new self(sprintf(
  96.             "Could not convert PHP type '%s' to '%s', as an '%s' error was triggered by the serialization",
  97.             $actualType,
  98.             $format,
  99.             $error
  100.         ));
  101.     }
  102.     public static function conversionFailedUnserialization(string $formatstring $error): self
  103.     {
  104.         return new self(sprintf(
  105.             "Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
  106.             $format,
  107.             $error
  108.         ));
  109.     }
  110. }