Last active
January 3, 2016 11:49
-
-
Save ryonagana/8458882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
esta classe abstrata devera estar inclusa na sua classe customizda para produtos | |
os atributos id e name sao usado para buscas por isso é obrigatorio te-los na assinatura da classe | |
*/ | |
abstract class Product { | |
public $id = 0; | |
public $qty = 1; // all products must start with qty 1 not zero | |
public $name = ""; // it needs for using search! | |
public $price = 0.0; | |
public abstract function addQty(); | |
public abstract function removeQty(); | |
public abstract function setQty($qty); | |
} | |
/* | |
Classe Cart | |
Função: Armazena Produtos facilitando o uso de carrinho de compras para e-commerce | |
*/ | |
class Cart implements Iterator, Countable { | |
private $productList = array(); | |
private static $instance; | |
private $position = 0; | |
private $ids = array(); | |
public function __construct(){ | |
} | |
public function key(){ | |
return $this->position; | |
} | |
public function next(){ | |
$this->position++; | |
} | |
public function rewind(){ | |
$this->position = 0; | |
} | |
public function valid(){ | |
return (isset($this->$ids[$this->position])); | |
} | |
public function current(){ | |
$index = $this->productList[$this->position]; | |
return $index; | |
} | |
public static function InitCart(){ | |
if(empty(self::$instance)){ | |
$newclass = __CLASS__; | |
self::$instance = new $newclass; | |
return self::$instance; | |
} | |
return self::$instance; | |
} | |
public function make_seed(){ | |
list($usec, $sec) = explode(' ', microtime()); | |
return (float) $sec + ((float) $usec * 100000); | |
} | |
public function Signature(){ | |
mt_srand(make_seed()); | |
return 1.0 * mt_rand() ^ 4; | |
} | |
public function addProduct(Product $prod){ | |
if($this->xsearch($prod->id)){ | |
foreach ($this->productList as $value) { | |
if($prod->id == $value->id){ | |
$value->qty++; | |
} | |
} | |
}else { | |
array_push($this->productList, $prod); | |
} | |
} | |
public function showCartData(){ | |
var_dump($this->productList); | |
} | |
public function count(){ | |
return count($this->productList); | |
} | |
public function search($key){ | |
for($i = 0; $i < count($this->productList); ++$i){ | |
$produto = $this->productList[$i]; | |
if($produto->name == $key){ | |
return $produto; | |
} | |
//if($produto->nome) | |
} | |
return NULL; | |
} | |
public function xsearch($id){ | |
for($i = 0; $i < count($this->productList); ++$i){ | |
if($this->productList[$i]->id == $id){ | |
return true; | |
} | |
} | |
return false; | |
} | |
} // fim classe | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Produto extends Product { | |
public $descr; | |
public $franquia_id; | |
public $cliente_id; | |
public function addQty(){ | |
$this->qty++; | |
} | |
public function removeQty(){ | |
return ($this->qty >= 0) ? $this->qty-- : 0; | |
} | |
public function setQty($qty){ | |
$this->qty = $qty; | |
} | |
} | |
$cart = new Cart(); | |
$produto = new Produto(); | |
$produto->id = $produtoid; | |
$produto->name = $nome; | |
$produto->price = $preco; | |
$produto->descr = $descr; | |
$produto->franquia_id = $_SESSION['id']; | |
$carrinho->addProduct($produto); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment