A universal component of the basket that can be used in any php framework.
Run the following command from you terminal:
composer require "koind/multi-basket: ^1.0"
or add this to require section in your composer.json file:
"koind/multi-basket": "^1.0"
then run composer update
First, you need to plug the required classes and initialized this classes. Next, create an instance of the Cart class by passing the appropriate arguments. Congratulations You can use the functionality of the basket.
<?php
use koind\storage\LaravelHybridStorage;
use koind\Cart;
use koind\cost\SimpleCost;
use koind\cost\MinCost;
use koind\cost\FridayCost;
use koind\cost\NewYearCost;
require __DIR__ . '/vendor/autoload.php';
$storage = new LaravelHybridStorage('cart');
$simpleCost = new SimpleCost();
$calculator = new MinCost(
new FridayCost($simpleCost, 5, date('Y-m-d')),
new NewYearCost($simpleCost, date('m'), 3)
);
$cart = new Cart($storage, $calculator);
$cart->add(5, 6, 100);
The following methods are available:
public function getItems(): array
public function add(int $id, int $count, int $price): void
public function subtractCount(int $id, int $count, int $price): void
public function getCost(): float
public function remove(int $id): void
public function clear(): void
public function getId(): int
public function getCount(): float
public function getPrice(): float
public function getCost(): float
This interface implements all types of classes necessary for discounts.
public function getCost(array $items): float
This interface implements all types of classes required for data storage.
public function load(): array
public function save(array $items): void
Get all products from the cart:
$this->cart->getItems();
Add a new product to your cart:
$this->cart->add(3, 4, 1000);
Changing the quantity of product:
$this->cart->subtractCount(3, 2, 1000);
Get the total cost of goods, including all discounts:
$this->cart->getCost();
Remove product from cart:
$this->cart->remove(3);
Clear cart:
$this->cart->clear();
An example of a storage implementation for different frameworks.
<?php
namespace koind\storage;
use koind\CartItem;
use Illuminate\Contracts\Session\Session;
class LaravelSessionStorage implements StorageInterface
{
private $session;
private $sessionKey;
public function __construct(Session $session, $sessionKey = 'cart')
{
$this->session = $session;
$this->sessionKey = $sessionKey;
}
/**
* @return CartItem[]
*/
public function load(): array
{
return $this->session->get($this->sessionKey, []);
}
/**
* @param CartItem[] $items
*/
public function save(array $items): void
{
$this->session->put($this->sessionKey, $items);
}
}
<?php
namespace koind\storage;
use koind\CartItem;
use Symfony\Component\HttpFoundation\Session\Session;
class SymfonySessionStorage implements StorageInterface
{
private $session;
private $sessionKey;
public function __construct(Session $session, $sessionKey = 'cart')
{
$this->session = $session;
$this->sessionKey = $sessionKey;
}
/**
* @return CartItem[]
*/
public function load(): array
{
return $this->session->get($this->sessionKey, []);
}
/**
* @param CartItem[] $items
*/
public function save(array $items): void
{
$this->session->set($this->sessionKey, $items);
}
}
<?php
namespace koind\storage;
use koind\CartItem;
use Yii;
class YiiSessionStorage implements StorageInterface
{
private $sessionKey;
public function __construct($sessionKey = 'cart')
{
$this->sessionKey = $sessionKey;
}
/**
* @return CartItem[]
*/
public function load(): array
{
return Yii::$app->session->get($this->sessionKey, []);
}
/**
* @param CartItem[] $items
*/
public function save(array $items): void
{
Yii::$app->session->set($this->sessionKey, $items);
}
}
If you need a component for another framework, you can easily implement the interface StorageInterface
Run the following command from you terminal:
phpunit