Skip to content

Commit

Permalink
Added settings example in readme file. Changed type in dateTime seria…
Browse files Browse the repository at this point in the history
…lization and deserialization methods
  • Loading branch information
msalakhov committed Dec 20, 2023
1 parent f0affbc commit 6cbd745
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ open_api_server:
type: yaml # Specification format, either yaml or json. If omitted, the specification file extension will be used.
name_space: PetStore # Namespace for generated DTOs and Interfaces
media_type: 'application/json' # media type from the specification files to use for generating request and response DTOs
#date_time_class: '\Carbon\CarbonImmutable' # FQCN which implements \DateTimeInterface.
## If set up, then generated DTOs will return instances of this class in DateTime parameters
```

Add your OpenApi specifications to the application routes configuration file using standard `resource` keyword
Expand Down
10 changes: 5 additions & 5 deletions src/Types/TypeSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace OnMoon\OpenApiServerBundle\Types;

use DateTime;
use DateTimeInterface;
use Exception;
use Safe\DateTime;
use Safe\Exceptions\DatetimeException;

use function base64_encode;
Expand All @@ -28,7 +28,7 @@ class TypeSerializer
public static function deserializeDate(string $date, ?string $dateTimeClass = null): DateTimeInterface
{
if ($dateTimeClass === null) {
return \Safe\DateTime::createFromFormat(self::DATE_FORMAT, $date);
return DateTime::createFromFormat(self::DATE_FORMAT, $date);
}

if (method_exists($dateTimeClass, 'createFromFormat') === false) {
Expand All @@ -50,7 +50,7 @@ public static function deserializeDate(string $date, ?string $dateTimeClass = nu
return $deserializedDate;
}

public static function serializeDate(DateTime $date): string
public static function serializeDate(DateTimeInterface $date): string
{
return $date->format(self::DATE_FORMAT);
}
Expand All @@ -63,14 +63,14 @@ public static function serializeDate(DateTime $date): string
public static function deserializeDateTime(string $date, ?string $dateTimeClass = null): DateTimeInterface
{
if ($dateTimeClass === null) {
return new \Safe\DateTime($date);
return new DateTime($date);
}

/** @psalm-suppress InvalidStringClass */
return new $dateTimeClass($date);
}

public static function serializeDateTime(DateTime $date): string
public static function serializeDateTime(DateTimeInterface $date): string
{
return $date->format(self::DATETIME_FORMAT);
}
Expand Down

0 comments on commit 6cbd745

Please sign in to comment.