Skip to content

Commit

Permalink
update Cart controller to use match statements for enum conversions a…
Browse files Browse the repository at this point in the history
…nd update Orders view to display milk type and cup size values from enum objects
  • Loading branch information
Divyeshhhh committed Jun 8, 2024
1 parent a33383f commit 2f796ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/controllers/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Steamy\Model\OrderProduct;
use Steamy\Model\Product;
use Steamy\Model\Store;
use Steamy\Model\OrderMilkType;
use Steamy\Model\OrderCupSize;

class Cart
{
Expand Down Expand Up @@ -101,8 +103,19 @@ private function handleCheckout(): void
foreach ($form_data['items'] as $item) {
$line_item = new OrderProduct(
product_id: filter_var($item['productID'], FILTER_VALIDATE_INT),
cup_size: strtolower($item['cupSize']),
milk_type: strtolower($item['milkType']),
cup_size: match (strtolower($item['cupSize'])) { // Using match for enum conversion
'small' => OrderCupSize::SMALL,
'medium' => OrderCupSize::MEDIUM,
'large' => OrderCupSize::LARGE,
default => throw new Exception('Invalid cup size: ' . $item['cupSize'])
},
milk_type: match (strtolower($item['milkType'])) { // Using match for enum conversion
'almond' => OrderMilkType::ALMOND,
'coconut' => OrderMilkType::COCONUT,
'oat' => OrderMilkType::OAT,
'soy' => OrderMilkType::SOY,
default => throw new Exception('Invalid milk type: ' . $item['milkType'])
},
quantity: filter_var($item['quantity'], FILTER_VALIDATE_INT)
);
$new_order->addLineItem($line_item);
Expand Down
4 changes: 2 additions & 2 deletions src/views/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<tr>
<td><?= htmlspecialchars($item->getProductName()) ?></td>
<td><?= filter_var($item->getQuantity(), FILTER_SANITIZE_NUMBER_INT) ?></td>
<td><?= htmlspecialchars($item->getMilkType()) ?></td>
<td><?= htmlspecialchars($item->getCupSize()) ?></td>
<td><?= htmlspecialchars(ucfirst($item->getMilkType()->value)) ?></td>
<td><?= htmlspecialchars(ucfirst($item->getCupSize()->value)) ?></td>
<td>$<?= (number_format($item->getUnitPrice(), 2)) ?></td>
</tr>
<?php
Expand Down

0 comments on commit 2f796ce

Please sign in to comment.