Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isNew feature #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 37 additions & 25 deletions src/Moltin/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
class Cart
{
protected $id;

protected $identifier;
protected $store;

protected $currency;

protected $new = true;

protected $requiredParams = array(
'id',
'name',
Expand All @@ -41,7 +43,7 @@ class Cart

/**
* Cart constructor
*
*
* @param StorageInterface $store The interface for storing the cart data
* @param IdentifierInterface $identifier The interface for storing the identifier
*/
Expand All @@ -54,15 +56,25 @@ public function __construct(StorageInterface $store, IdentifierInterface $identi
$this->id = $this->identifier->get();

// Restore the cart from a saved version
if (method_exists($this->store, 'restore')) $this->store->restore($this->id);
if (method_exists($this->store, 'restore')) {
$this->new = !$this->store->restore($this->id);
}

// Let our storage class know which cart we're talking about
$this->store->setIdentifier($this->id);
}

public function isNew($value = null)
{
if (!is_null($value)) {
$this->new = (bool)$value;
}
return (bool)$this->new;
}

/**
* Retrieve the cart contents
*
*
* @return array An array of Item objects
*/
public function &contents($asArray = false)
Expand All @@ -72,7 +84,7 @@ public function &contents($asArray = false)

/**
* Retrieve the cart contents as an array
*
*
* @return array An array of items
*/
public function &contentsArray()
Expand All @@ -82,7 +94,7 @@ public function &contentsArray()

/**
* Insert an item into the cart
*
*
* @param array $item An array of item data
* @return string A unique item identifier
*/
Expand Down Expand Up @@ -110,7 +122,7 @@ public function insert(array $item)

/**
* Update an item
*
*
* @param string $itemIdentifier The unique item identifier
* @param string|int|array $key The key to update, or an array of key-value pairs
* @param mixed $value The value to set $key to
Expand All @@ -130,7 +142,7 @@ public function update($itemIdentifier, $key, $value = null)

/**
* Remove an item from the cart
*
*
* @param string $identifier Unique item identifier
* @return void
*/
Expand All @@ -141,7 +153,7 @@ public function remove($identifier)

/**
* Destroy/empty the cart
*
*
* @return void
*/
public function destroy()
Expand All @@ -151,7 +163,7 @@ public function destroy()

/**
* Check if the cart has a specific item
*
*
* @param string $itemIdentifier The unique item identifier
* @return boolean Yes or no?
*/
Expand All @@ -162,7 +174,7 @@ public function has($itemIdentifier)

/**
* Return a specific item object by identifier
*
*
* @param string $itemIdentifier The unique item identifier
* @return Item Item object
*/
Expand All @@ -173,7 +185,7 @@ public function item($itemIdentifier)

/**
* Returns the first occurance of an item with a given id
*
*
* @param string $id The item id
* @return Item Item object
*/
Expand All @@ -184,7 +196,7 @@ public function find($id)

/**
* The total tax value for the cart
*
*
* @return float The total tax value
*/
public function tax()
Expand All @@ -198,7 +210,7 @@ public function tax()

/**
* The total value of the cart
*
*
* @param boolean $includeTax Include tax on the total?
* @return float The total cart value
*/
Expand All @@ -213,7 +225,7 @@ public function total($includeTax = true)

/**
* The total value of the cart with tax
*
*
* @return float The total cart value
*/
public function totalWithTax()
Expand All @@ -223,7 +235,7 @@ public function totalWithTax()

/**
* The total value of the cart without tax
*
*
* @return float The total cart value
*/
public function totalWithoutTax()
Expand All @@ -233,7 +245,7 @@ public function totalWithoutTax()

/**
* The total number of items in the cart
*
*
* @param boolean $unique Just return unique items?
* @return int Total number of items
*/
Expand All @@ -250,7 +262,7 @@ public function totalItems($unique = false)

/**
* The total number of unique items in the cart
*
*
* @return int Total number of items
*/
public function totalUniqueItems()
Expand All @@ -260,7 +272,7 @@ public function totalUniqueItems()

/**
* Set the currency object
*
*
* @param \Moltin\Currency\Currency $currency The currency object
*/
public function setCurrency(Currency $currency)
Expand All @@ -272,17 +284,17 @@ public function setCurrency(Currency $currency)

/**
* Get the currency object
*
*
* @return Currency The currency object for this cart
*/
public function currency()
{
return $this->currency;
}

/**
* Set the cart identifier, useful if restoring a saved cart
*
*
* @param mixed The identifier
* @return void
*/
Expand All @@ -293,7 +305,7 @@ public function setIdentifier($identifier)

/**
* Create a unique item identifier
*
*
* @param array $item An array of item data
* @return string An md5 hash of item
*/
Expand All @@ -308,7 +320,7 @@ protected function createItemIdentifier(array $item)

/**
* Check if a cart item has the required parameters
*
*
* @param array $item An array of item data
* @return void
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Moltin/Cart/Storage/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public function restore()
{
session_id() or session_start();

if (isset($_SESSION['cart'])) static::$cart = unserialize($_SESSION['cart']);
if (isset($_SESSION['cart'])) {
static::$cart = unserialize($_SESSION['cart']);
return true;
}

return false;
}

/**
Expand Down
26 changes: 19 additions & 7 deletions tests/CartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public function tearDown()
$this->cart->destroy();
}

public function testIsNew()
{
$this->assertTrue($this->cart->isNew());

$this->assertTrue($this->cart->isNew(true));
$this->assertTrue($this->cart->isNew(1));
$this->assertTrue($this->cart->isNew('hi'));

$this->assertFalse($this->cart->isNew(0));
$this->assertFalse($this->cart->isNew(false));
}

public function testInsert()
{
$actualId = $this->cart->insert(array(
Expand Down Expand Up @@ -98,7 +110,7 @@ public function testMagicUpdate()

$this->assertEquals($this->cart->item($actualId)->name, 'baz');
}

public function testOptions()
{
$actualId = $this->cart->insert(array(
Expand All @@ -110,14 +122,14 @@ public function testOptions()
'size' => 'L'
)
));

$item = $this->cart->item($actualId);

$this->assertTrue($item->hasOptions());
$this->assertNotEmpty($item->options);

$item->options = array();

$this->assertFalse($item->hasOptions());
$this->assertEmpty($item->options);
}
Expand Down Expand Up @@ -158,14 +170,14 @@ public function testTotalItems()

for ($i = 1; $i <= $adding; $i++) {
$quantity = rand(1, 20);

$this->cart->insert(array(
'id' => uniqid(),
'name' => 'bar',
'price' => 100,
'quantity' => $quantity
));

$actualTotal += $quantity;
}

Expand Down