File: D:/HostingSpaces/SBogers10/somerenslust.komma.pro/app/KommaApp/Shop/Discounts/DiscountableTypes.php
<?php
namespace App\KommaApp\Shop\Discounts;
use App\KommaApp\Shop\Cart\ShoppingCartItem;
use App\KommaApp\Shop\Cart\ShoppingCartItemInterface;
use App\KommaApp\Shop\Cart\ShoppingCartService;
use App\KommaApp\Shop\Cart\ShoppingCartServiceInterface;
class DiscountableTypes
{
const None = 0;
const ShoppingCartItem = 1;
const ShoppingCart = 2;
const ShoppingCartAndShoppingCartItem = 3;
/**
* @return int[] All types as an array of integers
*/
static function getAsArray() {
return self::getAllTypes();
}
/**
* Check if the passed type is really a type that is defined as a constant in this class.
*
* @param mixed $type
* @param bool $strict If true it will only consider a type as valid if it is an int. If it for example is a numeric string it won't consider it valid
* @return bool Returns true if the type is a valid one, false otherwise
*/
static function isValidType($type, $strict = true) {
return in_array($type, self::getAllTypes(), $strict);
}
/**
* Tells you which value belongs to the discountable. Used for storing the reference efficiently in the database
*
* @param DiscountableInterface|string|int $discountable
* @return int
*/
public static function toDiscountableTypeConstantValue($discountable)
{
if(!is_string($discountable) && !is_a($discountable, DiscountableInterface::class)) throw new \InvalidArgumentException('Discountable must be an instance of DiscountableInterface or a class name referring to an DiscountableInterface.');
switch ($discountable)
{
case ShoppingCartItem::class:
case ShoppingCartItemInterface::class:
return DiscountableTypes::ShoppingCartItem;
case ShoppingCartService::class:
case ShoppingCartServiceInterface::class:
return DiscountableTypes::ShoppingCart;
case DiscountableInterface::class:
return DiscountableTypes::ShoppingCartAndShoppingCartItem;
}
return DiscountableTypes::None;
}
/**
* @param $discountable
* @return string
*/
public static function typeConstantToDiscountableInterface($discountable)
{
switch ($discountable)
{
case 1:
return ShoppingCartItemInterface::class;
break;
case 2:
return ShoppingCartServiceInterface::class;
break;
case 3:
return DiscountableInterface::class;
break;
}
}
/**
* Returns an array containing integers representing the defined types.
*
* @return int[]
*/
private static function getAllTypes()
{
$thisClassAsReflectionClass = new \ReflectionClass(__CLASS__);
return $thisClassAsReflectionClass->getConstants();
}
}