diff --git a/src/cart.php b/src/cart.php index 233ca5c..07badef 100644 --- a/src/cart.php +++ b/src/cart.php @@ -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 = []; @@ -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 diff --git a/src/cartData.php b/src/cartData.php index e26a593..11abc9f 100644 --- a/src/cartData.php +++ b/src/cartData.php @@ -1,74 +1,36 @@ 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; diff --git a/src/cartStorageSession.php b/src/cartStorageSession.php index df3e3dd..928a6e4 100644 --- a/src/cartStorageSession.php +++ b/src/cartStorageSession.php @@ -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; @@ -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 ); }