-
-
Save joekenpat/0b008fa7eddc60ba32ee0854a3c46f80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Order | |
{ | |
/** @var int */ | |
private $id; | |
/** @var OrderItem[] */ | |
private $orderedItems = []; | |
/** @var int */ | |
private $itemCount = 0; | |
/** @var float */ | |
private $totalAmount = 0; | |
public function __construct($orderItems) | |
{ | |
$this->id = $this->generateId(); | |
$this->addOrderItem($orderItems); | |
$this->calculateTotals(); | |
} | |
/** | |
* @return OrderItem[] | |
*/ | |
public function getOrderedItems(): array | |
{ | |
return $this->orderedItems; | |
} | |
/** | |
* Supply an OrderItem or Array of OrderItems | |
* to be added to the orderItems list | |
* @param OrderItem|OrderItem[] $orderItems | |
*/ | |
public function addOrderItem($orderItems): void | |
{ | |
$orderItems = is_array($orderItems) ? $orderItems : [$orderItems]; | |
$this->orderedItems = array_merge($this->orderedItems, array_map(function ($orderedItem) { | |
if ($orderedItem instanceof OrderItem) { | |
return $orderedItem; | |
} | |
}, $orderItems)); | |
$this->calculateTotals(); | |
} | |
/** | |
* Supply an OrderItem [id] or Array of OrderItem [id]s | |
* to be removed from the orderItems list | |
* @param Int|Int[] $removable | |
*/ | |
public function removeOrderItem($removable): void | |
{ | |
$removables = is_array($removable) ? $removable : [$removable]; | |
$this->orderedItems = array_filter($this->orderedItems, function ($orderedItem) use ($removables) { | |
return in_array($orderedItem->id, $removables); | |
}); | |
$this->calculateTotals(); | |
} | |
/** | |
* @return int | |
*/ | |
public function getItemCount(): int | |
{ | |
return $this->itemCount; | |
} | |
/** | |
* @return float | |
*/ | |
public function getTotalAmount(): int | |
{ | |
return $this->totalAmount; | |
} | |
private function calculateTotals(): void | |
{ | |
$this->itemCount = count($this->orderedItems); | |
$this->totalAmount = 0; | |
foreach ($this->orderedItems as $orderedItem) { | |
$this->totalAmount += $orderedItem->amount; | |
} | |
} | |
private function generateId(): int | |
{ | |
return random_int(1, 99999); | |
} | |
} | |
class OrderItem | |
{ | |
/** @var int */ | |
private $id; | |
/** @var Item */ | |
private $orderItem; | |
/** @var float */ | |
private $quantity; | |
/** @var int */ | |
public $amount; | |
public function __construct(Item $orderItem, Float $quantity) | |
{ | |
$this->id = $this->generateId(); | |
$this->orderItem = $orderItem; | |
$this->quantity = $quantity; | |
$this->calculateAmount(); | |
} | |
public function setOrderItem(OrderItem $orderItem) | |
{ | |
$this->orderItem = $orderItem; | |
$this->calculateAmount(); | |
} | |
public function setQuantity(Float $quantity) | |
{ | |
$this->quantity = $quantity; | |
$this->calculateAmount(); | |
} | |
private function calculateAmount(): void | |
{ | |
$this->amount = $this->quantity * $this->orderItem->price; | |
} | |
private function generateId(): int | |
{ | |
return random_int(1, 9999); | |
} | |
} | |
/** | |
* Creates a new Order Item | |
* @param String $name | |
* @param Float $price | |
*/ | |
class Item | |
{ | |
/** @var int */ | |
private $id; | |
/** @var int */ | |
private $price; | |
public function __construct(String $name, Float $price) | |
{ | |
$this->id = $this->generateId(); | |
$this->name = $name; | |
$this->price = $price; | |
} | |
private function generateId(): int | |
{ | |
return random_int(1, 999); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment