refac: Auction DTO류를 새 DTO에 맞게 변경
This commit is contained in:
@@ -41,6 +41,6 @@ class Auction
|
|||||||
if ($rawBettingInfo === null) {
|
if ($rawBettingInfo === null) {
|
||||||
throw new \RuntimeException("해당 경매가 없습니다: {$auctionID}");
|
throw new \RuntimeException("해당 경매가 없습니다: {$auctionID}");
|
||||||
}
|
}
|
||||||
$this->info = new AuctionInfo($rawBettingInfo);
|
$this->info = AuctionInfo::fromArray($rawBettingInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,37 +2,25 @@
|
|||||||
|
|
||||||
namespace sammo\DTO;
|
namespace sammo\DTO;
|
||||||
|
|
||||||
use sammo\Json;
|
use sammo\DTO\Attr\JsonString;
|
||||||
use Spatie\DataTransferObject\DataTransferObject;
|
use sammo\DTO\Attr\RawName;
|
||||||
use Spatie\DataTransferObject\Attributes\MapFrom;
|
|
||||||
use Spatie\DataTransferObject\Attributes\MapTo;
|
|
||||||
|
|
||||||
class AuctionBidItem extends DataTransferObject{
|
class AuctionBidItem extends DTO
|
||||||
public int $id;
|
{
|
||||||
#[MapFrom('auction_id')]
|
public function __construct(
|
||||||
#[MapTo('auction_id')]
|
public int $id,
|
||||||
public int $auctionID;
|
#[RawName('auction_id')]
|
||||||
public ?int $owner;
|
public int $auctionID,
|
||||||
|
public ?int $owner,
|
||||||
|
|
||||||
#[MapFrom('general_id')]
|
#[RawName('general_id')]
|
||||||
#[MapTo('general_id')]
|
public int $generalID,
|
||||||
public int $generalID;
|
|
||||||
|
|
||||||
public int $amount;
|
public int $amount,
|
||||||
|
|
||||||
public string $date;
|
public string $date,
|
||||||
public AuctionBidItemData $data;
|
#[JsonString]
|
||||||
|
public AuctionBidItemData $data,
|
||||||
public static function parse(array $data): self
|
) {
|
||||||
{
|
|
||||||
$data['data'] = Json::decode($data['data']);
|
|
||||||
return new self($data);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
public function toArray(): array
|
|
||||||
{
|
|
||||||
$result = parent::toArray();
|
|
||||||
$result['data'] = Json::encode($this->data);
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
namespace sammo\DTO;
|
namespace sammo\DTO;
|
||||||
|
|
||||||
use Spatie\DataTransferObject\DataTransferObject;
|
class AuctionBidItemData extends DTO
|
||||||
|
{
|
||||||
class AuctionBidItemData extends DataTransferObject{
|
public function __construct(
|
||||||
public string $ownerName;
|
public string $ownerName,
|
||||||
public string $generalName;
|
public string $generalName,
|
||||||
}
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,27 +2,19 @@
|
|||||||
|
|
||||||
namespace sammo\DTO;
|
namespace sammo\DTO;
|
||||||
|
|
||||||
use Spatie\DataTransferObject\Attributes\CastWith;
|
|
||||||
use Spatie\DataTransferObject\Attributes\Strict;
|
|
||||||
use Spatie\DataTransferObject\DataTransferObject;
|
|
||||||
|
|
||||||
use sammo\Enums\ResourceType;
|
use sammo\Enums\ResourceType;
|
||||||
use sammo\DTO\Caster\BackedEnumCaster;
|
|
||||||
|
|
||||||
//https://json2dto.atymic.dev/
|
class AuctionInfo extends DTO
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[Strict]
|
|
||||||
class AuctionInfo extends DataTransferObject
|
|
||||||
{
|
{
|
||||||
public int $id;
|
public function __construct(
|
||||||
public int $openerName;
|
public int $id,
|
||||||
public int $openerGeneralID;
|
public int $openerName,
|
||||||
public string $type;
|
public int $openerGeneralID,
|
||||||
public bool $finished;
|
public string $type,
|
||||||
#[CastWith(BackedEnumCaster::class)]
|
public bool $finished,
|
||||||
public ResourceType $reqResource;
|
public ResourceType $reqResource,
|
||||||
public string $openDate;
|
public string $openDate,
|
||||||
public string $closeDate;
|
public string $closeDate,
|
||||||
|
) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace sammo\DTO\Caster;
|
|
||||||
use Spatie\DataTransferObject\Caster;
|
|
||||||
|
|
||||||
class BackedEnumCaster implements Caster
|
|
||||||
{
|
|
||||||
private \ReflectionMethod $backedEnumFrom;
|
|
||||||
public function __construct(
|
|
||||||
array $type,
|
|
||||||
) {
|
|
||||||
$enumType = $type[0] ?? null;
|
|
||||||
if(!is_subclass_of($enumType, \BackedEnum::class)){
|
|
||||||
throw new \Exception('Not BackedEnum: '.$enumType);
|
|
||||||
}
|
|
||||||
$reflector = new \ReflectionEnum($enumType);
|
|
||||||
$this->backedEnumFrom = $reflector->getMethod('from');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function cast(mixed $value): \BackedEnum
|
|
||||||
{
|
|
||||||
if(!is_string($value) && !is_int($value)){
|
|
||||||
throw new \Exception('BackedEnumCaster only accepts string|int');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->backedEnumFrom->invoke(null, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user