Passed
Pull Request — master (#3254)
by Farhad
13:52
created

ConversionException   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 81.58%

Importance

Changes 0
Metric Value
wmc 11
eloc 32
dl 0
loc 92
ccs 31
cts 38
cp 0.8158
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A conversionFailedSerialization() 0 9 2
A conversionFailed() 0 5 2
A conversionFailedFormat() 0 9 2
A conversionFailedInvalidType() 0 19 3
A conversionFailedUnserialization() 0 9 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
/**
21
 * Conversion Exception is thrown when the database to PHP conversion fails.
22
 *
23
 * @link   www.doctrine-project.org
24
 * @since  2.0
25
 * @author Benjamin Eberlei <[email protected]>
26
 * @author Guilherme Blanco <[email protected]>
27
 * @author Jonathan Wage <[email protected]>
28
 * @author Roman Borschel <[email protected]>
29
 */
30
namespace Doctrine\DBAL\Types;
31
32
use function get_class;
33
use function gettype;
34
use function implode;
35
use function is_object;
36
use function is_scalar;
37
use function sprintf;
38
use function strlen;
39
use function substr;
40
41
class ConversionException extends \Doctrine\DBAL\DBALException
42
{
43
    /**
44
     * Thrown when a Database to Doctrine Type Conversion fails.
45
     *
46
     * @param string $value
47
     * @param string $toType
48
     *
49
     * @return \Doctrine\DBAL\Types\ConversionException
50
     */
51 228
    public static function conversionFailed($value, $toType)
52
    {
53 228
        $value = (strlen($value) > 32) ? substr($value, 0, 20) . '...' : $value;
54
55 228
        return new self('Could not convert database value "' . $value . '" to Doctrine Type ' . $toType);
56
    }
57
58
    /**
59
     * Thrown when a Database to Doctrine Type Conversion fails and we can make a statement
60
     * about the expected format.
61
     *
62
     * @param string          $value
63
     * @param string          $toType
64
     * @param string          $expectedFormat
65
     * @param \Exception|null $previous
66
     *
67
     * @return \Doctrine\DBAL\Types\ConversionException
68
     */
69 209
    public static function conversionFailedFormat($value, $toType, $expectedFormat, \Exception $previous = null)
70
    {
71 209
        $value = (strlen($value) > 32) ? substr($value, 0, 20) . '...' : $value;
72
73 209
        return new self(
74 209
            'Could not convert database value "' . $value . '" to Doctrine Type ' .
75 209
            $toType . '. Expected format: ' . $expectedFormat,
76 209
            0,
77 209
            $previous
78
        );
79
    }
80
81
    /**
82
     * Thrown when the PHP value passed to the converter was not of the expected type.
83
     *
84
     * @param mixed    $value
85
     * @param string   $toType
86
     * @param string[] $possibleTypes
87
     *
88
     * @return \Doctrine\DBAL\Types\ConversionException
89
     */
90 1596
    public static function conversionFailedInvalidType($value, $toType, array $possibleTypes)
91
    {
92 1596
        $actualType = is_object($value) ? get_class($value) : gettype($value);
93
94 1596
        if (is_scalar($value)) {
95 988
            return new self(sprintf(
96 988
                "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
97 988
                $value,
98 988
                $actualType,
99 988
                $toType,
100 988
                implode(', ', $possibleTypes)
101
            ));
102
        }
103
104 608
        return new self(sprintf(
105 608
            "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
106 608
            $actualType,
107 608
            $toType,
108 608
            implode(', ', $possibleTypes)
109
        ));
110
    }
111
112
    public static function conversionFailedSerialization($value, $format, $error)
113
    {
114
        $actualType = is_object($value) ? get_class($value) : gettype($value);
115
116
        return new self(sprintf(
117
            "Could not convert PHP type '%s' to '%s', as an '%s' error was triggered by the serialization",
118
            $actualType,
119
            $format,
120
            $error
121
        ));
122
    }
123
124 38
    public static function conversionFailedUnserialization($value, $format, $error)
125
    {
126 38
        $actualType = is_object($value) ? get_class($value) : gettype($value);
127
128 38
        return new self(sprintf(
129 38
            "Could not convert database value '%s' to '%s', as an '%s' error was triggered by the unserialization",
130 38
            $actualType,
131 38
            $format,
132 38
            $error
133
        ));
134
    }
135
}
136