HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Shop/Discounts/DiscountableTypes.php
<?php

namespace App\Komma\Shop\Discounts;

use App\Komma\Shop\Cart\ShoppingCartItem;
use App\Komma\Shop\Cart\ShoppingCartItemInterface;
use App\Komma\Shop\Cart\ShoppingCartService;
use App\Komma\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
     */
    public 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
     */
    public 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 self::ShoppingCartItem;
            case ShoppingCartService::class:
            case ShoppingCartServiceInterface::class:
                return self::ShoppingCart;
            case DiscountableInterface::class:
                return self::ShoppingCartAndShoppingCartItem;
        }

        return self::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();
    }
}