forked from loafle/openapi-generator-original
[PHP] Include microseconds in serialized date-time (#4542)
* [PHP] Include milliseconds in serialized date-time * Add setDateTimeFormat()
This commit is contained in:
parent
96bbab98a4
commit
eebad5c9aa
@ -31,6 +31,19 @@ use {{modelPackage}}\ModelInterface;
|
|||||||
*/
|
*/
|
||||||
class ObjectSerializer
|
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
|
* Serialize data
|
||||||
*
|
*
|
||||||
@ -45,7 +58,7 @@ class ObjectSerializer
|
|||||||
if (is_scalar($data) || null === $data) {
|
if (is_scalar($data) || null === $data) {
|
||||||
return $data;
|
return $data;
|
||||||
} elseif ($data instanceof \DateTime) {
|
} 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)) {
|
} elseif (is_array($data)) {
|
||||||
foreach ($data as $property => $value) {
|
foreach ($data as $property => $value) {
|
||||||
$data[$property] = self::sanitizeForSerialization($value);
|
$data[$property] = self::sanitizeForSerialization($value);
|
||||||
@ -178,7 +191,7 @@ class ObjectSerializer
|
|||||||
public static function toString($value)
|
public static function toString($value)
|
||||||
{
|
{
|
||||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||||
return $value->format(\DateTime::ATOM);
|
return $value->format(self::$dateTimeFormat);
|
||||||
} else if (is_bool($value)) {
|
} else if (is_bool($value)) {
|
||||||
return $value ? 'true' : 'false';
|
return $value ? 'true' : 'false';
|
||||||
} else {
|
} else {
|
||||||
|
@ -41,6 +41,19 @@ use OpenAPI\Client\Model\ModelInterface;
|
|||||||
*/
|
*/
|
||||||
class ObjectSerializer
|
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
|
* Serialize data
|
||||||
*
|
*
|
||||||
@ -55,7 +68,7 @@ class ObjectSerializer
|
|||||||
if (is_scalar($data) || null === $data) {
|
if (is_scalar($data) || null === $data) {
|
||||||
return $data;
|
return $data;
|
||||||
} elseif ($data instanceof \DateTime) {
|
} 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)) {
|
} elseif (is_array($data)) {
|
||||||
foreach ($data as $property => $value) {
|
foreach ($data as $property => $value) {
|
||||||
$data[$property] = self::sanitizeForSerialization($value);
|
$data[$property] = self::sanitizeForSerialization($value);
|
||||||
@ -188,7 +201,7 @@ class ObjectSerializer
|
|||||||
public static function toString($value)
|
public static function toString($value)
|
||||||
{
|
{
|
||||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||||
return $value->format(\DateTime::ATOM);
|
return $value->format(self::$dateTimeFormat);
|
||||||
} else if (is_bool($value)) {
|
} else if (is_bool($value)) {
|
||||||
return $value ? 'true' : 'false';
|
return $value ? 'true' : 'false';
|
||||||
} else {
|
} else {
|
||||||
|
@ -18,6 +18,11 @@ class DateTimeSerializerTest extends TestCase
|
|||||||
$data = ObjectSerializer::sanitizeForSerialization($input);
|
$data = ObjectSerializer::sanitizeForSerialization($input);
|
||||||
|
|
||||||
$this->assertEquals($data->dateTime, '1973-04-30T17:05:00+02:00');
|
$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()
|
public function testDateSanitazion()
|
||||||
|
@ -41,6 +41,19 @@ use OpenAPI\Client\Model\ModelInterface;
|
|||||||
*/
|
*/
|
||||||
class ObjectSerializer
|
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
|
* Serialize data
|
||||||
*
|
*
|
||||||
@ -55,7 +68,7 @@ class ObjectSerializer
|
|||||||
if (is_scalar($data) || null === $data) {
|
if (is_scalar($data) || null === $data) {
|
||||||
return $data;
|
return $data;
|
||||||
} elseif ($data instanceof \DateTime) {
|
} 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)) {
|
} elseif (is_array($data)) {
|
||||||
foreach ($data as $property => $value) {
|
foreach ($data as $property => $value) {
|
||||||
$data[$property] = self::sanitizeForSerialization($value);
|
$data[$property] = self::sanitizeForSerialization($value);
|
||||||
@ -188,7 +201,7 @@ class ObjectSerializer
|
|||||||
public static function toString($value)
|
public static function toString($value)
|
||||||
{
|
{
|
||||||
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
if ($value instanceof \DateTime) { // datetime in ISO8601 format
|
||||||
return $value->format(\DateTime::ATOM);
|
return $value->format(self::$dateTimeFormat);
|
||||||
} else if (is_bool($value)) {
|
} else if (is_bool($value)) {
|
||||||
return $value ? 'true' : 'false';
|
return $value ? 'true' : 'false';
|
||||||
} else {
|
} else {
|
||||||
|
@ -18,6 +18,11 @@ class DateTimeSerializerTest extends TestCase
|
|||||||
$data = ObjectSerializer::sanitizeForSerialization($input);
|
$data = ObjectSerializer::sanitizeForSerialization($input);
|
||||||
|
|
||||||
$this->assertEquals($data->dateTime, '1973-04-30T17:05:00+02:00');
|
$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()
|
public function testDateSanitazion()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user