Laravel:获取资源类上枚举字段的正确值

nue99wik  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(156)

我在一个项目中使用PHP8.1和Laravel 9,在这个项目中我得到了以下枚举:

enum OrderStatuses : string
{
    case New           = 'new';
    case Pending       = 'pending';
    case Canceled      = 'canceled';
    case Paid          = 'paid';
    case PaymentFailed = 'payment-failed';

    public function createOrderStatus(Order $order) : OrderStatus
    {
        return match($this) {
            OrderStatuses::Pending       => new PendingOrderStatus($order),
            OrderStatuses::Canceled      => new CanceledOrderStatus($order),
            OrderStatuses::Paid          => new PaidOrderStatus($order),
            OrderStatuses::PaymentFailed => new PaymentFailedOrderStatus($order),
            default                      => new NewOrderStatus($order)
        };
    }

枚举中列出的一个类如下所示:
第一次
所有其他的基本上是相同的,它们只是在canBeChanged方法的实现上有所不同。
现在,我有了以下资源:

class OrdersResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => (string)$this->id,
            'type' => 'orders',
            'attributes' => [
                'status' => $this->status,
                'payment_type' => $this->payment_type,
                'payment_transaction_no' => $this->payment_transaction_no,
                'subtotal' => $this->subtotal,
                'taxes'  => $this->taxes,
                'total' => $this->total,
                'items' => OrderItemsResource::collection($this->whenLoaded('orderItems')),
                'created_at' => $this->created_at,
                'updated_at' => $this->updated_at,
            ]
        ];
    }
}

从我的控制器调用它,如下所示:

return (new OrdersResource($order))
            ->response()->setStatusCode(ResponseAlias::HTTP_OK);

在实现枚举之前,我的资源正常工作,它返回了预期的数据。但是在实现枚举之后,它返回了status字段的[]。一个示例返回如下所示:

"id" => "86b4e2da-76d4-4e66-8016-88a251513050"
  "type" => "orders"
  "attributes" => array:8 [
    "status" => []
    "payment_type" => "card"
    "payment_transaction_no" => "3kaL92f5UwOG"
    "subtotal" => 3005.76
    "taxes" => 0
    "total" => 3005.76
    "created_at" => "2022-08-31T12:47:55.000000Z"
    "updated_at" => "2022-08-31T12:47:55.000000Z"
  ]
]

再次注意status的值。
我在Order的模型中有一个角色转换和一个属性:
第一个
此外,如果I ddOrdersResourcetoArray方法中$this->status的类型,则表示它是Domain\Order\Enums\PaidOrderStatus类型,这是正确的。
我试着将__toString()添加到PaidOrderStatus类中,但没有成功。我错过了什么?

更新

我在PaidOrderStatus中添加了一个test()方法:

class PaidOrderStatus extends OrderStatus
{
    public function canBeChanged(): bool
    {
        return false;
    }

    public function test() : string
    {
        return OrderStatuses::Paid->value;
    }
}

然后做了:

public function toArray($request): array
    {
        return [
            'id' => (string)$this->id,
            'type' => 'orders',
            'attributes' => [
                'status' => ($this->status)->test(),
                'payment_type' => $this->payment_type,
                'payment_transaction_no' => $this->payment_transaction_no,
                'subtotal' => $this->subtotal,
                'taxes'  => $this->taxes,
                'total' => $this->total,
                'items' => OrderItemsResource::collection($this->whenLoaded('orderItems')),
                'created_at' => $this->created_at,
                'updated_at' => $this->updated_at,
            ]
        ];
    }

这给了我:

[
  "id" => "8a2d6024-a63f-44ba-a145-cede2ecf3aaa"
  "type" => "orders"
  "attributes" => array:8 [
    "status" => "paid"
    "payment_type" => "card"
    "payment_transaction_no" => "kC9upaoGb2Nd"
    "subtotal" => 657.26
    "taxes" => 0
    "total" => 657.26
    "created_at" => "2022-08-31T13:17:25.000000Z"
    "updated_at" => "2022-08-31T13:17:25.000000Z"
  ]

但这是一个非常笨拙的解决方案,我想做得更好。

nqwrtyyt

nqwrtyyt1#

我相信你已经解决了这个问题,因为它是在几个月前,但首先你不需要Attribute mutator,因为你已经定义了转换,它将正确地Map字符串值到正确的枚举情况。
然后在资源中,您只需从枚举中获取值,如下所示。

'status' => $this->status->value,

相关问题