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/komma.pro/app/KommaApp/Shop/Cart/HasCouponCodesTrait.php
<?php


namespace App\KommaApp\Shop\Cart;


trait HasCouponCodesTrait
{
    /** @var string[] $couponCodes */
    private $couponCodes = [];

    /**
     * Adds a coupon code. Notice that this does not mean that it is valid.
     * Validation is done via the DiscountServiceInterface
     *
     * @param string $code
     * @return mixed
     */
    public function addCouponCode(string $code): bool
    {
        if(!in_array($code, $this->couponCodes))
        {
            $this->couponCodes[] = $code;
            return true;
        } return false;
    }

    /**
     * Removes a coupon code and returns true if it was removed and false if not.
     *
     * @param string $code
     * @return bool
     */
    public function removeCouponCode(string $code): bool
    {
        $foundIndex = false;
        foreach($this->couponCodes as $index => $couponCode)
        {
            if($code == $couponCode) {
                $foundIndex = $index;
                break;
            }
        }

        if($foundIndex)
        {
            array_splice($this->couponCodes, $foundIndex, 1);
            return true;
        }

        return false;
    }

    /**
     * Return all coupon codes;
     *
     * @return string[];
     */
    public function getCouponCodes(): array
    {
        return $this->couponCodes;
    }
}