[PHP] Include microseconds in serialized date-time (#4542)

* [PHP] Include milliseconds in serialized date-time

* Add setDateTimeFormat()
This commit is contained in:
Hinrik Örn Sigurðsson 2019-11-21 03:21:27 +01:00 committed by William Cheng
parent 96bbab98a4
commit eebad5c9aa
5 changed files with 55 additions and 6 deletions

View File

@ -31,6 +31,19 @@ use {{modelPackage}}\ModelInterface;
*/
class ObjectSerializer
{
/** @var string */
private static $dateTimeFormat = \DateTime::ATOM;
/**
* Change the date format
*
* @param string $format the new date format to use
*/
public static function setDateTimeFormat($format)
{
self::$dateTimeFormat = $format;
}
/**
* Serialize data
*
@ -45,7 +58,7 @@ class ObjectSerializer
if (is_scalar($data) || null === $data) {
return $data;
} elseif ($data instanceof \DateTime) {
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat);
} elseif (is_array($data)) {
foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value);
@ -178,7 +191,7 @@ class ObjectSerializer
public static function toString($value)
{
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ATOM);
return $value->format(self::$dateTimeFormat);
} else if (is_bool($value)) {
return $value ? 'true' : 'false';
} else {

View File

@ -41,6 +41,19 @@ use OpenAPI\Client\Model\ModelInterface;
*/
class ObjectSerializer
{
/** @var string */
private static $dateTimeFormat = \DateTime::ATOM;
/**
* Change the date format
*
* @param string $format the new date format to use
*/
public static function setDateTimeFormat($format)
{
self::$dateTimeFormat = $format;
}
/**
* Serialize data
*
@ -55,7 +68,7 @@ class ObjectSerializer
if (is_scalar($data) || null === $data) {
return $data;
} elseif ($data instanceof \DateTime) {
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat);
} elseif (is_array($data)) {
foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value);
@ -188,7 +201,7 @@ class ObjectSerializer
public static function toString($value)
{
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ATOM);
return $value->format(self::$dateTimeFormat);
} else if (is_bool($value)) {
return $value ? 'true' : 'false';
} else {

View File

@ -18,6 +18,11 @@ class DateTimeSerializerTest extends TestCase
$data = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($data->dateTime, '1973-04-30T17:05:00+02:00');
ObjectSerializer::setDateTimeFormat(\DateTime::RFC3339_EXTENDED);
$dataFraction = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($dataFraction->dateTime, '1973-04-30T17:05:00.000+02:00');
ObjectSerializer::setDateTimeFormat(\DateTime::ATOM);
}
public function testDateSanitazion()

View File

@ -41,6 +41,19 @@ use OpenAPI\Client\Model\ModelInterface;
*/
class ObjectSerializer
{
/** @var string */
private static $dateTimeFormat = \DateTime::ATOM;
/**
* Change the date format
*
* @param string $format the new date format to use
*/
public static function setDateTimeFormat($format)
{
self::$dateTimeFormat = $format;
}
/**
* Serialize data
*
@ -55,7 +68,7 @@ class ObjectSerializer
if (is_scalar($data) || null === $data) {
return $data;
} elseif ($data instanceof \DateTime) {
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(\DateTime::ATOM);
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat);
} elseif (is_array($data)) {
foreach ($data as $property => $value) {
$data[$property] = self::sanitizeForSerialization($value);
@ -188,7 +201,7 @@ class ObjectSerializer
public static function toString($value)
{
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ATOM);
return $value->format(self::$dateTimeFormat);
} else if (is_bool($value)) {
return $value ? 'true' : 'false';
} else {

View File

@ -18,6 +18,11 @@ class DateTimeSerializerTest extends TestCase
$data = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($data->dateTime, '1973-04-30T17:05:00+02:00');
ObjectSerializer::setDateTimeFormat(\DateTime::RFC3339_EXTENDED);
$dataFraction = ObjectSerializer::sanitizeForSerialization($input);
$this->assertEquals($dataFraction->dateTime, '1973-04-30T17:05:00.000+02:00');
ObjectSerializer::setDateTimeFormat(\DateTime::ATOM);
}
public function testDateSanitazion()