diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index f5f0e5119ef..99b992ed5dd 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -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 { diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index cdae80a4e82..3569ce8ce4c 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -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 { diff --git a/samples/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php b/samples/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php index 3a889cba6da..ca0fc505d6d 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php +++ b/samples/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php @@ -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() diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index cdae80a4e82..3569ce8ce4c 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -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 { diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php index 3a889cba6da..ca0fc505d6d 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/DateTimeSerializerTest.php @@ -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()