Skip to content

Commit

Permalink
Fixed wrong datetime assignment (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: Dimannn <[email protected]>
  • Loading branch information
DimanKuskov and Dimannn authored Mar 12, 2020
1 parent e5752f6 commit 363320a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Mapper/DtoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use ArrayAccess;
use ArrayObject;
use BadMethodCallException;
use DateTime;
use OnMoon\OpenApiServerBundle\CodeGenerator\Factory\OperationDefinitionFactory;
use OnMoon\OpenApiServerBundle\CodeGenerator\Naming\NamingStrategy;
use OnMoon\OpenApiServerBundle\Exception\CannotMapToDto;
Expand Down Expand Up @@ -55,6 +56,7 @@ public function __construct(NamingStrategy $namingStrategy)
* @throws UnexpectedScalarValue
* @throws ReflectionException
* @throws StringsException
* @throws NotADateTimeValue
*/
public function map($from, string $toDTO, ?callable $propertyMapper = null)
{
Expand Down Expand Up @@ -146,6 +148,17 @@ public function map($from, string $toDTO, ?callable $propertyMapper = null)
/** @psalm-suppress MixedAssignment */
$value[] = $this->map($item, $fullClass, $nextMapper);
}
} elseif ($shortClassName === 'DateTime') {
/** @var mixed $item */
foreach ($rawValue as $item) {
if ($item instanceof DateTime) {
$value[] = $item;
} elseif (is_string($item)) {
$value[] = new \Safe\DateTime($item);
} else {
throw new NotADateTimeValue($property->getName(), $toDTO, $item);
}
}
} else {
/** @var mixed $item */
foreach ($rawValue as $item) {
Expand All @@ -156,6 +169,14 @@ public function map($from, string $toDTO, ?callable $propertyMapper = null)
}
}
}
} elseif ($typeName === 'DateTime') {
if ($rawValue instanceof DateTime) {
$value = $rawValue;
} elseif (is_string($rawValue)) {
$value = new \Safe\DateTime($rawValue);
} else {
throw new NotADateTimeValue($property->getName(), $toDTO, $rawValue);
}
} elseif ($type->isBuiltin()) {
/** phpcs:disable Generic.PHP.ForbiddenFunctions.Found */
settype($rawValue, $typeName);
Expand Down
39 changes: 39 additions & 0 deletions src/Mapper/NotADateTimeValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace OnMoon\OpenApiServerBundle\Mapper;

use OnMoon\OpenApiServerBundle\Exception\OpenApiError;
use Safe\Exceptions\StringsException;
use function Safe\sprintf;

class NotADateTimeValue extends OpenApiError
{
/** @var mixed $value */
private $value;

/**
* @param mixed $value
*
* @throws StringsException
*/
public function __construct(string $name, string $class, $value)
{
$message = sprintf(
'Value is not DateTime for "%s" in "%s"',
$name,
$class
);
$this->value = $value;
parent::__construct($message);
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}

0 comments on commit 363320a

Please sign in to comment.