refac: Auction DTO류를 새 DTO에 맞게 변경
This commit is contained in:
@@ -41,6 +41,6 @@ class Auction
|
||||
if ($rawBettingInfo === null) {
|
||||
throw new \RuntimeException("해당 경매가 없습니다: {$auctionID}");
|
||||
}
|
||||
$this->info = new AuctionInfo($rawBettingInfo);
|
||||
$this->info = AuctionInfo::fromArray($rawBettingInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,37 +2,25 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use sammo\Json;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
use Spatie\DataTransferObject\Attributes\MapFrom;
|
||||
use Spatie\DataTransferObject\Attributes\MapTo;
|
||||
use sammo\DTO\Attr\JsonString;
|
||||
use sammo\DTO\Attr\RawName;
|
||||
|
||||
class AuctionBidItem extends DataTransferObject{
|
||||
public int $id;
|
||||
#[MapFrom('auction_id')]
|
||||
#[MapTo('auction_id')]
|
||||
public int $auctionID;
|
||||
public ?int $owner;
|
||||
class AuctionBidItem extends DTO
|
||||
{
|
||||
public function __construct(
|
||||
public int $id,
|
||||
#[RawName('auction_id')]
|
||||
public int $auctionID,
|
||||
public ?int $owner,
|
||||
|
||||
#[MapFrom('general_id')]
|
||||
#[MapTo('general_id')]
|
||||
public int $generalID;
|
||||
#[RawName('general_id')]
|
||||
public int $generalID,
|
||||
|
||||
public int $amount;
|
||||
public int $amount,
|
||||
|
||||
public string $date;
|
||||
public AuctionBidItemData $data;
|
||||
|
||||
public static function parse(array $data): self
|
||||
{
|
||||
$data['data'] = Json::decode($data['data']);
|
||||
return new self($data);
|
||||
public string $date,
|
||||
#[JsonString]
|
||||
public AuctionBidItemData $data,
|
||||
) {
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$result = parent::toArray();
|
||||
$result['data'] = Json::encode($this->data);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
class AuctionBidItemData extends DataTransferObject{
|
||||
public string $ownerName;
|
||||
public string $generalName;
|
||||
}
|
||||
class AuctionBidItemData extends DTO
|
||||
{
|
||||
public function __construct(
|
||||
public string $ownerName,
|
||||
public string $generalName,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,27 +2,19 @@
|
||||
|
||||
namespace sammo\DTO;
|
||||
|
||||
use Spatie\DataTransferObject\Attributes\CastWith;
|
||||
use Spatie\DataTransferObject\Attributes\Strict;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
use sammo\Enums\ResourceType;
|
||||
use sammo\DTO\Caster\BackedEnumCaster;
|
||||
|
||||
//https://json2dto.atymic.dev/
|
||||
|
||||
|
||||
|
||||
#[Strict]
|
||||
class AuctionInfo extends DataTransferObject
|
||||
class AuctionInfo extends DTO
|
||||
{
|
||||
public int $id;
|
||||
public int $openerName;
|
||||
public int $openerGeneralID;
|
||||
public string $type;
|
||||
public bool $finished;
|
||||
#[CastWith(BackedEnumCaster::class)]
|
||||
public ResourceType $reqResource;
|
||||
public string $openDate;
|
||||
public string $closeDate;
|
||||
public function __construct(
|
||||
public int $id,
|
||||
public int $openerName,
|
||||
public int $openerGeneralID,
|
||||
public string $type,
|
||||
public bool $finished,
|
||||
public ResourceType $reqResource,
|
||||
public string $openDate,
|
||||
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