From 53e48e06dbd0dc4c8a1ae3c2d2e7d4885b44d72f Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Tue, 7 May 2024 20:53:08 +0400 Subject: [PATCH 1/4] use OrderStatus enum & update related methods and valid status values --- src/models/Order.php | 10 +++++----- src/models/OrderStatus.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 src/models/OrderStatus.php diff --git a/src/models/Order.php b/src/models/Order.php index cf9ea45..c018748 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -17,7 +17,7 @@ class Order private int $store_id; private int $order_id; - private string $status; + private OrderStatus $status; private DateTime $created_date; private ?DateTime $pickup_date; // ?DateTime type allows $pickup_date to be null private int $client_id; @@ -27,7 +27,7 @@ public function __construct( int $client_id, ?int $order_id = null, ?DateTime $pickup_date = null, - string $status = "pending", + OrderStatus $status = OrderStatus::PENDING, // Default to 'pending', DateTime $created_date = new DateTime(), ) { $this->store_id = $store_id; @@ -148,12 +148,12 @@ public function getOrderID(): int return $this->order_id; } - public function getStatus(): string + public function getStatus(): OrderStatus { return $this->status; } - public function setStatus(string $status): void + public function setStatus(OrderStatus $status): void { $this->status = $status; } @@ -182,7 +182,7 @@ public function validate(): array { $errors = []; - $validStatus = ['pending', 'cancelled', 'completed']; + $validStatus = [OrderStatus::PENDING, OrderStatus::CANCELLED, OrderStatus::COMPLETED]; if (!in_array($this->status, $validStatus)) { $errors['status'] = "Status must be one of: " . implode(', ', $validStatus); } diff --git a/src/models/OrderStatus.php b/src/models/OrderStatus.php new file mode 100644 index 0000000..2ef1c80 --- /dev/null +++ b/src/models/OrderStatus.php @@ -0,0 +1,11 @@ + Date: Wed, 8 May 2024 20:45:53 +0400 Subject: [PATCH 2/4] change abstract class OrderStatus to enum --- src/models/OrderStatus.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models/OrderStatus.php b/src/models/OrderStatus.php index 2ef1c80..9050be3 100644 --- a/src/models/OrderStatus.php +++ b/src/models/OrderStatus.php @@ -4,8 +4,8 @@ namespace Steamy\Model; -abstract class OrderStatus { - const PENDING = 'pending'; - const CANCELLED = 'cancelled'; - const COMPLETED = 'completed'; +enum OrderStatus { + case PENDING; + case CANCELLED; + case COMPLETED; } \ No newline at end of file From dc4b8f20acd2bbd1ca891c51c222646470fd3c49 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 10 May 2024 14:10:23 +0400 Subject: [PATCH 3/4] use backed enum ref: https://benjamincrozat.com/php-enums --- src/models/OrderStatus.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/models/OrderStatus.php b/src/models/OrderStatus.php index 9050be3..4b30041 100644 --- a/src/models/OrderStatus.php +++ b/src/models/OrderStatus.php @@ -4,8 +4,9 @@ namespace Steamy\Model; -enum OrderStatus { - case PENDING; - case CANCELLED; - case COMPLETED; +enum OrderStatus: string +{ + case PENDING = 'pending'; + case CANCELLED = 'cancelled'; + case COMPLETED = 'completed'; } \ No newline at end of file From 3382c09694991b642a0a27b8a44ee9f21c79fc04 Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Fri, 10 May 2024 14:11:19 +0400 Subject: [PATCH 4/4] update toArray and getByID to ensure that OrderStatus is of the right type --- src/models/Order.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/Order.php b/src/models/Order.php index c018748..8af5b10 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -42,7 +42,7 @@ public function toArray(): array { return [ 'order_id' => $this->order_id, - 'status' => $this->status, + 'status' => $this->status->value, 'created_date' => $this->created_date->format('Y-m-d H:i:s'), 'pickup_date' => $this->pickup_date?->format('Y-m-d H:i:s'), 'client_id' => $this->client_id, @@ -108,7 +108,7 @@ public static function getByID(int $order_id): ?Order client_id: $orderData->client_id, order_id: $orderData->order_id, pickup_date: $orderData->pickup_date ? Utility::stringToDate($orderData->pickup_date) : null, - status: $orderData->status, + status: OrderStatus::from($orderData->status), created_date: Utility::stringToDate($orderData->created_date), ); }