Skip to content

Commit

Permalink
Merge pull request #157 from Divyesh000/enum
Browse files Browse the repository at this point in the history
use OrderStatus enum & update related methods and valid status values
  • Loading branch information
creme332 authored May 10, 2024
2 parents 31eee3c + 3382c09 commit 21551bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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),
);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions src/models/OrderStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Steamy\Model;

enum OrderStatus: string
{
case PENDING = 'pending';
case CANCELLED = 'cancelled';
case COMPLETED = 'completed';
}

0 comments on commit 21551bf

Please sign in to comment.