ConversionException.php 3.5 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Types;

5 6
use Doctrine\DBAL\DBALException;
use Throwable;
7

8 9 10 11 12 13 14 15 16
use function get_class;
use function gettype;
use function implode;
use function is_object;
use function is_scalar;
use function sprintf;
use function strlen;
use function substr;

17 18
/**
 * Conversion Exception is thrown when the database to PHP conversion fails.
19 20
 *
 * @psalm-immutable
21
 */
22
class ConversionException extends DBALException
23 24 25
{
    /**
     * Thrown when a Database to Doctrine Type Conversion fails.
26
     *
Benjamin Morel's avatar
Benjamin Morel committed
27 28 29
     * @param string $value
     * @param string $toType
     *
Grégoire Paris's avatar
Grégoire Paris committed
30
     * @return ConversionException
31
     */
32
    public static function conversionFailed($value, $toType, ?Throwable $previous = null)
33
    {
34
        $value = strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value;
Benjamin Morel's avatar
Benjamin Morel committed
35

36
        return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType, 0, $previous);
37
    }
38

39 40 41
    /**
     * Thrown when a Database to Doctrine Type Conversion fails and we can make a statement
     * about the expected format.
42
     *
43 44 45
     * @param string $value
     * @param string $toType
     * @param string $expectedFormat
Benjamin Morel's avatar
Benjamin Morel committed
46
     *
Grégoire Paris's avatar
Grégoire Paris committed
47
     * @return ConversionException
48
     */
49
    public static function conversionFailedFormat($value, $toType, $expectedFormat, ?Throwable $previous = null)
50
    {
51
        $value = strlen($value) > 32 ? substr($value, 0, 20) . '...' : $value;
Benjamin Morel's avatar
Benjamin Morel committed
52

53 54
        return new self(
            'Could not convert database value "' . $value . '" to Doctrine Type ' .
55 56 57
            $toType . '. Expected format: ' . $expectedFormat,
            0,
            $previous
58
        );
59
    }
60 61 62 63

    /**
     * Thrown when the PHP value passed to the converter was not of the expected type.
     *
64 65 66
     * @param mixed    $value
     * @param string   $toType
     * @param string[] $possibleTypes
67
     *
Grégoire Paris's avatar
Grégoire Paris committed
68
     * @return ConversionException
69
     */
70 71 72 73 74 75
    public static function conversionFailedInvalidType(
        $value,
        $toType,
        array $possibleTypes,
        ?Throwable $previous = null
    ) {
76 77 78 79 80 81 82 83 84
        $actualType = is_object($value) ? get_class($value) : gettype($value);

        if (is_scalar($value)) {
            return new self(sprintf(
                "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
                $value,
                $actualType,
                $toType,
                implode(', ', $possibleTypes)
85
            ), 0, $previous);
86 87
        }

88
        return new self(sprintf(
89
            "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
90 91 92
            $actualType,
            $toType,
            implode(', ', $possibleTypes)
93
        ), 0, $previous);
94
    }
95

96
    /**
97 98 99 100
     * @param mixed  $value
     * @param string $format
     * @param string $error
     *
101 102
     * @return ConversionException
     */
103
    public static function conversionFailedSerialization($value, $format, $error)
104 105 106 107 108 109 110 111 112 113
    {
        $actualType = is_object($value) ? get_class($value) : gettype($value);

        return new self(sprintf(
            "Could not convert PHP type '%s' to '%s', as an '%s' error was triggered by the serialization",
            $actualType,
            $format,
            $error
        ));
    }
114

115
    public static function conversionFailedUnserialization(string $format, string $error): self
116 117 118 119 120 121 122
    {
        return new self(sprintf(
            "Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'",
            $format,
            $error
        ));
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
123
}