File: D:/HostingSpaces/SBogers10/vangogh.komma.pro/app/Komma/Shop/Cart/HasCouponCodesTrait.php
<?php
namespace App\Komma\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 !== false)
{
array_splice($this->couponCodes, $foundIndex, 1);
return true;
}
return false;
}
/**
* Return all coupon codes;
*
* @return string[];
*/
public function getCouponCodes(): array
{
return $this->couponCodes;
}
}