Skip to content

Instantly share code, notes, and snippets.

@elmarputz
Created December 16, 2016 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elmarputz/f76207b691e80c68128e1271b353885d to your computer and use it in GitHub Desktop.
Save elmarputz/f76207b691e80c68128e1271b353885d to your computer and use it in GitHub Desktop.
<?php
//as soon as shopping cart is referenced the session context is created
SessionContext::create();
/**
* ShoppingCart
*
*
* @package
* @subpackage
* @author John Doe <jd@fbi.gov>
*/
class ShoppingCart extends BaseObject {
/**
* add an item to the shopping cart
*
* @param integer $bookId book id
* @return null
*/
public static function add($bookId) {
$c = self::getCart();
$c[$bookId] = $bookId;
self::storeCart($c);
}
/**
* remove an item from the shopping cart
*
* @param integer $bookId book id
* @return null
*/
public static function remove($bookId) {
$c = self::getCart();
unset($c[$bookId]);
self::storeCart($c);
}
/**
* empty the shopping cart
*
* @return null
*/
public static function clear() {
self::storeCart(array());
}
/**
* check if an item is in the shopping cart
*
* @param integer $bookId book id
* @return boolean
*/
public static function contains($bookId) {
$c = self::getCart();
return array_key_exists($bookId, $c);
}
/**
* get number of shopping cart items
*
* @return integer
*/
public static function size() {
return sizeof(self::getCart());
}
/**
* get the shopping cart contents
*
* @return ShoppingCart object | empty array
*/
public static function getAll() {
return self::getCart();
}
/**
* add an item to the shopping cart
*
* @return ShoppingCart object | empty array
*/
private static function getCart() {
return isset($_SESSION['cart']) ? $_SESSION['cart'] : array();
}
/**
* make shopping cart persistent
*
* @return null
*/
private static function storeCart(array $cart) {
$_SESSION['cart'] = $cart;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment