koind/multi-basket
Composer 安装命令:
composer create-project koind/multi-basket
包简介
A universal component of the basket that can be used in any php framework.
README 文档
README
A universal component of the basket that can be used in any php framework.
Installation
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
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.
<?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:
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.
public function getCost(array $items): float
koind\storage\StorageInterface
This interface implements all types of classes required for data storage.
public function load(): array public function save(array $items): void
Example usage class Cart
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();
Different storages
An example of a storage implementation for different frameworks.
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
Tests
Run the following command from you terminal:
phpunit
koind/multi-basket 适用场景与选型建议
koind/multi-basket 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 koind/multi-basket 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 koind/multi-basket 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-07-17