Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
33f55969
Commit
33f55969
authored
Jan 25, 2016
by
Baptiste Clavié
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Throws an ConversionException if the json encoding fails
parent
dee01418
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
1 deletion
+59
-1
ConversionException.php
lib/Doctrine/DBAL/Types/ConversionException.php
+12
-0
JsonType.php
lib/Doctrine/DBAL/Types/JsonType.php
+47
-1
No files found.
lib/Doctrine/DBAL/Types/ConversionException.php
View file @
33f55969
...
...
@@ -99,4 +99,16 @@ class ConversionException extends \Doctrine\DBAL\DBALException
implode
(
', '
,
$possibleTypes
)
));
}
static
public
function
conversionFailedSerialization
(
$value
,
$format
,
$error
,
\Exception
$previous
=
null
)
{
$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
));
}
}
lib/Doctrine/DBAL/Types/JsonType.php
View file @
33f55969
...
...
@@ -46,7 +46,13 @@ class JsonType extends Type
return
null
;
}
return
json_encode
(
$value
);
$encoded
=
json_encode
(
$value
);
if
(
JSON_ERROR_NONE
!==
json_last_error
())
{
throw
ConversionException
::
conversionFailedSerialization
(
$value
,
'json'
,
$this
->
getLastErrorMessage
());
}
return
$encoded
;
}
/**
...
...
@@ -91,4 +97,44 @@ class JsonType extends Type
//return ! $platform->hasNativeJsonType();
return
true
;
}
/**
* Get the latest json error message
*
* This method declaration has been extracted from symfony's php 5.5 polyfill
*
* @link https://github.com/symfony/polyfill-php55/blob/master/Php55.php
* @link http://nl1.php.net/manual/en/function.json-last-error-msg.php
*
* @return string
*/
private
function
getLastErrorMessage
()
{
if
(
function_exists
(
'json_last_error_msg'
))
{
return
json_last_error_msg
();
}
switch
(
json_last_error
())
{
case
JSON_ERROR_NONE
:
return
'No error'
;
case
JSON_ERROR_DEPTH
:
return
'Maximum stack depth exceeded'
;
case
JSON_ERROR_STATE_MISMATCH
:
return
'State mismatch (invalid or malformed JSON)'
;
case
JSON_ERROR_CTRL_CHAR
:
return
'Control character error, possibly incorrectly encoded'
;
case
JSON_ERROR_SYNTAX
:
return
'Syntax error'
;
case
JSON_ERROR_UTF8
:
return
'Malformed UTF-8 characters, possibly incorrectly encoded'
;
default
:
return
'Unknown error'
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment