2017 © Pedro Peláez
 

project multi-basket

A universal component of the basket that can be used in any php framework.

image

koind/multi-basket

A universal component of the basket that can be used in any php framework.

  • Tuesday, July 17, 2018
  • by koind
  • Repository
  • 0 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Multi-basket

A universal component of the basket that can be used in any php framework., (*1)

Installation

Run the following command from you terminal:, (*2)

bash composer require "koind/multi-basket: ^1.0", (*3)

or add this to require section in your composer.json file:, (*4)

"koind/multi-basket": "^1.0", (*5)

then run composer update, (*6)

Usage

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., (*7)

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

Available Methods

The following methods are available:, (*8)

koind\Cart
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
koind\CartItem
public function getId(): int
public function getCount(): float
public function getPrice(): float
public function getCost(): float
koind\cost\CalculatorInterface

This interface implements all types of classes necessary for discounts., (*9)

public function getCost(array $items): float
koind\storage\StorageInterface

This interface implements all types of classes required for data storage., (*10)

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

Example usage class Cart

Get all products from the cart:, (*11)

$this->cart->getItems();

Add a new product to your cart:, (*12)

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

Changing the quantity of product:, (*13)

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

Get the total cost of goods, including all discounts:, (*14)

$this->cart->getCost();

Remove product from cart:, (*15)

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

Clear cart:, (*16)

$this->cart->clear();

Different storages

An example of a storage implementation for different frameworks., (*17)

koind\storage\LaravelSessionStorage - for Laravel
<?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);
    }
}
koind\storage\SymfonySessionStorage - for Symfony
<?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);
    }
}
koind\storage\YiiSessionStorage - for Yii
<?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, (*18)

Tests

Run the following command from you terminal:, (*19)

phpunit

The Versions

17/07 2018

dev-master

9999999-dev

A universal component of the basket that can be used in any php framework.

  Sources   Download

MIT

The Requires

  • php ^7.2

 

The Development Requires

by Damir Assilhan

17/07 2018

1.0

1.0.0.0

A universal component of the basket that can be used in any php framework.

  Sources   Download

MIT

The Requires

  • php ^7.2

 

The Development Requires

by Damir Assilhan