treehousetim/shopcart

一个简单、无依赖、无UI的购物车库。

v0.0.8 2022-01-25 18:15 UTC

This package is auto-updated.

Last update: 2024-09-26 00:38:53 UTC


README

购物车核心功能PHP库

安装

composer require treehousetim/shopcart

接口和抽象类

为了使用shopCart,您需要实例化一个 treehousetim\shopCart\cart 对象和一个 treehousetim\shopCart\catalog 对象。

购物车构造函数需要一个目录对象作为参数。为了使目录对象能够工作,需要实现一个实现 catalogLoaderInterface 的产品加载器。

use \treehousetim\shopCart\catalog;
use \treehousetim\shopCart\cart;

$catalog = ( new catalog() )
	->setProductLoader( new \application\cart\myProductLoader() )
	->populate();

$cart = new cart( $catalog );
$cart->setTotalTypeLoader( (new \application\cart\myCatalogTotalTypeLoader()))
	->setFormatter( new \application\cart\myProductAmountFormatter() )
	->setStorageHandler( (new \treehousetim\shopCart\cartStorageSession() ) )
	->load();

catalogLoaderInterface

目录对象用于将产品加载到产品目录中。

interface catalogLoaderInterface
{
	public function hasProducts() : bool;
	public function nextProduct() : bool;
	public function getProduct() : product;
}

cartStorageInterface

您可以为购物车使用许多不同的存储选项。treehousetim\shopCart 为会话存储提供了一个实现。

创建此接口的实现后,您必须使用 $cart->setStorageHandler( $storageHandler ); 将其提供给您的购物车。

interface cartStorageInterface
{
	public function loadCart( cart $cart ) : cartStorageInterface;
	public function emptyCart( cart $cart ) : cartStorageInterface;

	public function saveItems( array $items ) : cartStorageInterface;
	public function saveData( array $data ) : cartStorageInterface;

	// used to clean up after all storage is complete
	public function finalize( cart $cart ) : cartStorageInterface;
}

catalogTotalTypeLoaderInterface

您必须实现一个符合此接口的类,以支持不同类型的产品总额。典型的电子商务购物车可能只需要一个目录总额用于价格,但还有其他业务需要不同产品属性的总额。

interface catalogTotalTypeLoaderInterface
{
	public function nextType() : bool;
	public function getType() : catalogTotalType;
	public function resetType();
}

以下是一个同时支持价格和一些特殊情况的“积分”的 catalogTotalTypeLoaderInterface 实现示例。

<?php namespace application\libraries\cart;

use treehousetim\shopCart\catalogTotalTypeLoaderInterface;
use treehousetim\shopCart\catalogTotalType;

class totalTypeLoader implements catalogTotalTypeLoaderInterface
{
	protected $types;

	public function __construct()
	{
		$this->types[] = (new catalogTotalType( catalogTotalType::tPRODUCT_PRICE ) );
		$this->types[] = (new catalogTotalType( catalogTotalType::tPRODUCT_FIELD ) )
			->setIdentifier( 1 )
			->setUnit( 'points' )
			->setLabel( 'Point' )
			->setProductField( 'productPoints' );
		}
	}
	//------------------------------------------------------------------------
	public function nextType() : bool
	{
		next( $this->types );
		if( key( $this->types ) === null )
		{
			return false;
		}

		return true;
	}
	//------------------------------------------------------------------------
	public function getType() : catalogTotalType
	{
		return current( $this->types );
	}
	//------------------------------------------------------------------------
	public function resetType()
	{
		reset( $this->types );
	}
}

totalFormatterInterface

interface totalFormatterInterface
{
	public function formatTotalType( string $value, catalogTotalType $type );
}

测试

如果您已克隆此存储库,可以运行测试(目前没有)。没有依赖项,但PHPUnit 已通过composer安装。

  1. composer install
  2. ./vendor/bin/phpunit --bootstrap vendor/autoload.php test