Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'autoload.php';
- class Cart implements CartInterface
- {
- private $Warenkorb = array();
- public function addItem($sku, $itemPrice, $quantity)
- {
- $exists = false;
- foreach($this->Warenkorb as $item){
- if($item['sku'] === $sku){
- $exists = true;
- }
- }
- if($exists){
- //Anzahl der Quanität erhöhen.
- }else{
- $newItem = array("sku" => $sku, "itemPrice" => $itemPrice, "quantity" => $quantity);
- array_push($this->Warenkorb, $newItem);
- }
- }
- public function removeItem($sku)
- {
- $exists = false;
- for($d = 0; $d < count($this->Warenkorb); $d++){
- if($this->Warenkorb[$d]['sku'] === $sku){
- unset($this->Warenkorb[$d]);
- $exists = true;
- }
- }
- if($exists === false){
- throw new Exception('Dieser Artikel ist nicht in Ihrem Warenkorb');
- }
- }
- public function getItemCount()
- {
- $total = 0;
- foreach($this->Warenkorb as $item){
- $total += $item['quantity'];
- }
- return $total;
- }
- public function getTotalPrice()
- {
- $total = 0;
- foreach($this->Warenkorb as $item){
- $total += $item['itemPrice'];
- }
- return $total;
- }
- public function getQuantity($sku)
- {
- $quantity = 0;
- $exist = false;
- foreach($this->Warenkorb as $item){
- if($item['sku'] === $sku){
- $quantity += $item['quantity'];
- $exist = true;
- }
- }
- if(!$exist){
- throw new Exception('Dieser Artikel ist nicht in Ihrem Warenkorb');
- }
- return $quantity;
- }
- public function getItems()
- {
- $items = array();
- foreach($this->Warenkorb as $item){
- $items[$item['sku']] = $item['quantity'];
- }
- return $items;
- }
- public function compareTo(CartInterface $cart)
- {
- $same = true;
- $WarenkorbB = $cart->getItems();
- $WarenkorbA = $this->getItems();
- if(count(array_diff_key($WarenkorbA, $WarenkorbB)) !== 0 OR array_diff($WarenkorbA, $WarenkorbB) !== 0){
- $same = false;
- }
- return $same;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement