|
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(string $format, string $error) : self |
|
125
|
|
|
{ |
|
126
|
38 |
|
return new self(sprintf( |
|
127
|
38 |
|
"Could not convert database value to '%s' as an error was triggered by the unserialization: '%s'", |
|
128
|
38 |
|
$format, |
|
129
|
38 |
|
$error |
|
130
|
|
|
)); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|