Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
*/
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
*
Expand All @@ -55,7 +68,7 @@ public static function sanitizeForSerialization($data, $type = null, $format = n
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);
Expand Down Expand Up @@ -188,7 +201,7 @@ public static function toFormValue($value)
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function testDateTimeSanitazion()
$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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
*/
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
*
Expand All @@ -55,7 +68,7 @@ public static function sanitizeForSerialization($data, $type = null, $format = n
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);
Expand Down Expand Up @@ -188,7 +201,7 @@ public static function toFormValue($value)
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function testDateTimeSanitazion()
$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()
Expand Down