Skip to content

Commit

Permalink
bug fixes and new interface for cart data
Browse files Browse the repository at this point in the history
make cart data an interface that is used on the base class.  make interface the type that is specified on func calls.

remove unused reflection
bug fixes
  • Loading branch information
Tim committed Jul 7, 2020
1 parent 0207b3e commit bd11126
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ After creating your implementation of this interface, you must supply it to your
```php
interface cartStorageInterface
{
public function init( cart $cart ) : cartStorageInterface;
public function saveCartItem( cartItem $item ) : cartStorageInterface;
public function saveCartData( cartData $data ) : cartStorageInterface;
public function loadCart( cart $cart ) : cartStorageInterface;
public function emptyCart( cart $cart ) : cartStorageInterface;

public function saveItems( array $items ) : cartStorageInterface;
public function saveData( array $data ) : cartStorageInterface;

// used to clean up after all storage is complete
public function finalize( cart $cart ) : cartStorageInterface;
}
```

Expand Down
4 changes: 2 additions & 2 deletions src/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ public function save()
$this->storageHandler->saveItems( $this->items );
$this->storageHandler->saveData( $this->data );

$this->storageHandler->finalize();
$this->storageHandler->finalize( $this );
}
//------------------------------------------------------------------------
public function addData( cartData $data ) : self
public function addData( iCartData $data ) : self
{
$this->data[$data->getType()] = $data;

Expand Down
4 changes: 2 additions & 2 deletions src/cartData.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace treehousetim\shopCart;
use \stdClass, \Exception, \ReflectionClass;
use \stdClass, \Exception;

abstract class cartData implements jsonSerializable
abstract class cartData implements jsonSerializable, iCartData
{
protected $data;
protected $type;
Expand Down
4 changes: 3 additions & 1 deletion src/cartStorageSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function saveItems( array $items )

$_SESSION['cart_items'] = [];

foreach( $this->items as $item )
foreach( $items as $item )
{
$_SESSION['cart_items'][] = ['id' => $item->getProduct()->getId(), 'qty' => $item->getQty()];
}
Expand All @@ -51,6 +51,8 @@ public function saveData( array $data )
public function finalize( cart $cart ) : cartStorageInterface
{
// do nothing

return $this;
}
//------------------------------------------------------------------------
public function loadCart( cart $cart ) : cartStorageInterface
Expand Down
11 changes: 11 additions & 0 deletions src/iCartData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace treehousetim\shopCart;

interface iCartData
{
public function jsonSerialize();
public function getForStorage();
public function setData( $data ) : iCartData;
public function getData();
public function setType( string $type ) : iCartData;
public function getType();
}

0 comments on commit bd11126

Please sign in to comment.