PHP code example of koind / multi-basket

1. Go to this page and download the library: Download koind/multi-basket library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

koind / multi-basket example snippets


 

use koind\storage\LaravelHybridStorage;
use koind\Cart;
use koind\cost\SimpleCost;
use koind\cost\MinCost;
use koind\cost\FridayCost;
use koind\cost\NewYearCost;

arCost($simpleCost, date('m'), 3)
);

$cart = new Cart($storage, $calculator);
$cart->add(5, 6, 100);

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

public function getCost(array $items): float

public function load(): array
public function save(array $items): void

$this->cart->getItems();

$this->cart->add(3, 4, 1000);

$this->cart->subtractCount(3, 2, 1000);

$this->cart->getCost();

$this->cart->remove(3);

$this->cart->clear();



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);
    }
}



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);
    }
}



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);
    }
}