feat: DTO Convert 인자 버그 수정, DateTimeConverter 확장

- DateTimeConverter에 목표 Timezone 설정
This commit is contained in:
2022-05-28 02:50:21 +09:00
parent c46128ed3e
commit 08eae297fd
3 changed files with 97 additions and 4 deletions
+36
View File
@@ -8,6 +8,7 @@ use sammo\DTO\Attr\RawName;
use sammo\DTO\Converter\ArrayConverter;
use sammo\DTO\Converter\Converter;
use sammo\DTO\Converter\MapConverter;
use sammo\DTO\Converter\DateTimeConverter;
use sammo\Json;
class TypeA extends DTO
@@ -155,6 +156,21 @@ class TypeRawName extends DTO
}
}
class TypeDateTime extends DTO{
public function __construct(
#[Convert(DateTimeConverter::class)]
public \DateTimeImmutable $a,
#[Convert(DateTimeConverter::class, false, 9)]
public \DateTime $b,
#[Convert(DateTimeConverter::class, true, 8)]
public \DateTimeImmutable|\DateTime $c,
#[Convert(DateTimeConverter::class)]
public \DateTimeInterface $d,
) {
}
}
class DTOTest extends PHPUnit\Framework\TestCase
{
public function testBasic()
@@ -351,4 +367,24 @@ class DTOTest extends PHPUnit\Framework\TestCase
$this->assertEquals($rawType, $testType);
}
public function testDateTime(){
$rawType = [
'a' => '2022-01-01 10:11:22',
'b' => '2022-02-01T12:34:56.1234+09:00',
'c' => '2022-03-01T00:00:00.1234+09:00',
'd' => '2022-04-01',
];
$testValue = [
'a' => '2022-01-01 10:11:22',
'b' => '2022-02-01 12:34:56',
'c' => '2022-02-28 23:00:00.123400',
'd' => '2022-04-01 00:00:00',
];
$obj = TypeDateTime::fromArray($rawType);
$testType = $obj->toArray();
$this->assertEquals($testValue, $testType);
}
}