Skip to content

Commit

Permalink
bug fixes and simplification in cartData
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim committed Jun 30, 2020
1 parent ba92cf3 commit 0207b3e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 82 deletions.
47 changes: 17 additions & 30 deletions src/cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ public function addProduct( product $product, $qty ) : self
return $this;
}
//------------------------------------------------------------------------
public function addData( cartData $data ) : self
{
$this->data[ $data->getType() ] = $data;

return $this;
}
//------------------------------------------------------------------------
public function emptyCart() : self
{
$this->data = [];
Expand Down Expand Up @@ -228,37 +221,31 @@ public function save()
$this->storageHandler->finalize();
}
//------------------------------------------------------------------------
public function addData( cartData $data ) : self
{
$this->data[$data->getType()] = $data;

return $this;
}
//------------------------------------------------------------------------
public function getCartData() : array
{
return $this->data;
}
//------------------------------------------------------------------------
public function getDataByCartDataType( string $type )
public function hasCartDataType( string $type ) : bool
{
if( count($this->data) > 0 )
{
foreach( $this->data as $cartData )
{
if( $cartData->isValidType( $type ) == false )
{
throw new \Exception("Invalid card data type : ".$type, 1);
}

if( $cartData->getType() == $type )
{
return $cartData->getDataArray();
}
else
{
continue;
}
}
return [];
}
else
return array_key_exists( $type, $this->data );
}
//------------------------------------------------------------------------
public function getDataByType( string $type ) : array
{
if( ! $this->hasCartDataType( $type ) )
{
return (array) $this->data;
throw new \Exception( 'Invalid cart data type: ' . $type );
}

return $this->data[$type];
}
//------------------------------------------------------------------------
public function getCartItems() : array
Expand Down
60 changes: 11 additions & 49 deletions src/cartData.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,36 @@
<?php namespace treehousetim\shopCart;
use \stdClass, \Exception, \ReflectionClass;
class cartData
{
const tOBJ = 'object';
const tARR = 'array';
const tMetalAllocation = 'metalAllocation';
const tCharges = 'Charges';
const tPaymentMethod = 'paymentMethod';


abstract class cartData implements jsonSerializable
{
protected $data;
protected $type;

//------------------------------------------------------------------------
public function setType( string $type )
{
switch( $type )
{
case self::tOBJ:
case self::tARR:
case self::tMetalAllocation:
case self::tCharges:
case self::tPaymentMethod:
$this->type = $type;
break;

default:
throw new Exception( 'Unknown type: ' . $type );
}

return $this;
}
abstract public function jsonSerialize();
//------------------------------------------------------------------------
static function getConstants() {
$oClass = new ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
//------------------------------------------------------------------------
public function isValidType( string $type ): bool
public function getForStorage()
{
return in_array($type, self::getConstants());
return $this->jsonSerialize();
}
//------------------------------------------------------------------------
//for now we are not planning to use this and use setDataArray for now.
public function setDataObject( object $data ) : self
public function setData( $data ) : self
{
throw new Exception("Use setDataArray instead.", 1);

$this->setType( self::tOBJ );
$this->data = $data;
return $this;
}
//------------------------------------------------------------------------
public function setData( array $data ) : self
public function getData()
{
//$this->setType( self::tARR );
$this->data = $data;
return $this;
return $this->data;
}
//------------------------------------------------------------------------
public function getDataObject() : object
public function setType( string $type ) : self
{
return (object)$this->data;
$this->type = $type;
return $this;
}
//------------------------------------------------------------------------
public function getDataArray() : array
{
return (array)$this->data;
}

public function getType()
{
return $this->type;
Expand Down
7 changes: 4 additions & 3 deletions src/cartStorageSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function saveData( array $data )

$_SESSION['cart_data'] = [];

foreach( $this->data as $data )
foreach( $data as $type => $_data )
{
$_SESSION['cart_data'][] = $data;
$_SESSION['cart_data'][$type] = $data->getForStorage();
}

return $this;
Expand Down Expand Up @@ -72,8 +72,9 @@ public function loadCart( cart $cart ) : cartStorageInterface

$data = $_SESSION['cart_data'] ?? array();

foreach( $data as $cartData )
foreach( $data as $type => $cartData )
{
$cartData->setType( $type );
$cart->addData( $cartData );
}

Expand Down

0 comments on commit 0207b3e

Please sign in to comment.