OrderStatus.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Module\Mex\Enums;
  3. /**
  4. * 农贸市场订单状态枚举
  5. */
  6. enum OrderStatus: string
  7. {
  8. case PENDING = 'PENDING'; // 等待中
  9. case COMPLETED = 'COMPLETED'; // 已完成
  10. case CANCELLED = 'CANCELLED'; // 已取消
  11. case FAILED = 'FAILED'; // 失败
  12. /**
  13. * 获取订单状态描述
  14. */
  15. public function getDescription(): string
  16. {
  17. return match ($this) {
  18. self::PENDING => '等待中',
  19. self::COMPLETED => '已完成',
  20. self::CANCELLED => '已取消',
  21. self::FAILED => '失败',
  22. };
  23. }
  24. /**
  25. * 获取所有订单状态
  26. */
  27. public static function getAll(): array
  28. {
  29. return [
  30. self::PENDING->value => self::PENDING->getDescription(),
  31. self::COMPLETED->value => self::COMPLETED->getDescription(),
  32. self::CANCELLED->value => self::CANCELLED->getDescription(),
  33. self::FAILED->value => self::FAILED->getDescription(),
  34. ];
  35. }
  36. /**
  37. * 判断是否为等待状态
  38. */
  39. public function isPending(): bool
  40. {
  41. return $this === self::PENDING;
  42. }
  43. /**
  44. * 判断是否为完成状态
  45. */
  46. public function isCompleted(): bool
  47. {
  48. return $this === self::COMPLETED;
  49. }
  50. /**
  51. * 判断是否为取消状态
  52. */
  53. public function isCancelled(): bool
  54. {
  55. return $this === self::CANCELLED;
  56. }
  57. /**
  58. * 判断是否为失败状态
  59. */
  60. public function isFailed(): bool
  61. {
  62. return $this === self::FAILED;
  63. }
  64. /**
  65. * 判断是否为最终状态(不可再变更)
  66. */
  67. public function isFinal(): bool
  68. {
  69. return in_array($this, [self::COMPLETED, self::CANCELLED, self::FAILED]);
  70. }
  71. }