mirror of
https://github.com/kevin-DL/LaravelShoppingcart.git
synced 2026-01-11 18:54:33 +00:00
Add Calculator Contract,
Add some checking for calculators,
This commit is contained in:
@@ -3,10 +3,11 @@
|
||||
namespace Gloudemans\Shoppingcart\Calculation;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
use Gloudemans\Shoppingcart\Contracts\Calculator;
|
||||
|
||||
class DefaultCalculator
|
||||
class DefaultCalculator implements Calculator
|
||||
{
|
||||
public static function getAttribute(string $attribute, CartItem $cartItem)
|
||||
static function getAttribute(string $attribute, CartItem $cartItem)
|
||||
{
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace Gloudemans\Shoppingcart\Calculation;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
use Gloudemans\Shoppingcart\Contracts\Calculator;
|
||||
|
||||
class GrossPrice
|
||||
class GrossPrice implements Calculator
|
||||
{
|
||||
public static function getAttribute(string $attribute, CartItem $cartItem)
|
||||
static function getAttribute(string $attribute, CartItem $cartItem)
|
||||
{
|
||||
$decimals = config('cart.format.decimals', 2);
|
||||
|
||||
|
||||
@@ -4,9 +4,12 @@ namespace Gloudemans\Shoppingcart;
|
||||
|
||||
use Gloudemans\Shoppingcart\Calculation\DefaultCalculator;
|
||||
use Gloudemans\Shoppingcart\Contracts\Buyable;
|
||||
use Gloudemans\Shoppingcart\Contracts\Calculator;
|
||||
use Gloudemans\Shoppingcart\Exceptions\InvalidCalculatorException;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Support\Arr;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* @property-read mixed discount
|
||||
@@ -398,7 +401,14 @@ class CartItem implements Arrayable, Jsonable
|
||||
return round($this->weight * $this->qty, $decimals);
|
||||
}
|
||||
|
||||
return call_user_func(config('cart.calculator', DefaultCalculator::class).'::getAttribute', $attribute, $this);
|
||||
$class = new ReflectionClass(config('cart.calculator', DefaultCalculator::class));
|
||||
if (!$class->implementsInterface(Calculator::class))
|
||||
{
|
||||
throw new InvalidCalculatorException('The configured Calculator seems to be invalid. Calculators have to implement the Calculator Contract.');
|
||||
}
|
||||
|
||||
return call_user_func($class->getName().'::getAttribute', $attribute, $this);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
10
src/Contracts/Calculator.php
Normal file
10
src/Contracts/Calculator.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Contracts;
|
||||
|
||||
use Gloudemans\Shoppingcart\CartItem;
|
||||
|
||||
interface Calculator
|
||||
{
|
||||
static function getAttribute(string $attribute, CartItem $cartItem);
|
||||
}
|
||||
9
src/Exceptions/InvalidCalculatorException.php
Normal file
9
src/Exceptions/InvalidCalculatorException.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Gloudemans\Shoppingcart\Exceptions;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidCalculatorException extends RuntimeException
|
||||
{
|
||||
}
|
||||
Reference in New Issue
Block a user