Handle empty date-time gracefully

Some API's return an invalid, empty string as a date-time property.

DateTime::__construct() will return the current time for empty input
which is probably not what is meant.

The invalid empty string is probably to be interpreted as a missing
field/value.

Let's handle this graceful.
This commit is contained in:
Arne Jørgensen 2016-03-15 06:46:51 +01:00
parent 3147061345
commit 65b78f7ffe

View File

@ -245,7 +245,17 @@ class ObjectSerializer
settype($data, 'array');
$deserialized = $data;
} elseif ($class === '\DateTime') {
$deserialized = new \DateTime($data);
// Some API's return an invalid, empty string as a
// date-time property. DateTime::__construct() will return
// the current time for empty input which is probably not
// what is meant. The invalid empty string is probably to
// be interpreted as a missing field/value. Let's handle
// this graceful.
if (!empty($data)) {
$deserialized = new \DateTime($data);
} else {
$deserialized = null;
}
} elseif (in_array($class, array({{&primitives}}))) {
settype($data, $class);
$deserialized = $data;