vendor/doctrine/dbal/src/Types/ConversionException.php line 25

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