File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Discounts/Conditions/ProductCategoryHandler.php
<?php declare(strict_types=1);
namespace App\Discounts\Conditions;
use App\Cart\ShoppingCartItem;
use App\Categories\Models\Category;
use App\Discounts\DiscountableInterface;
class ProductCategoryHandler extends AbstractDiscountConditionHandler
{
protected ?int $type = DiscountConditionTypes::PRODUCT_CATEGORY;
public function canBeApplied(DiscountableInterface $discountable, string $operator, string $param): bool
{
if(!is_a($discountable, ShoppingCartItem::class)) return false;
return $discountable->getProductable()->categories->filter(function(Category $category) use($param, $operator) {
$result = false;
if($operator === 'in') {
if(in_array($category->id, $this->dissectParam($param), true)) $result = true;
} else if($operator === 'not_in') {
if(!in_array($category->id, $this->dissectParam($param), true)) $result = true;
} else if($operator === '<') {
if(strlen($category->id) < strlen($param)) $result = true;
} else if($operator === '<=') {
if(strlen($category->id) <= strlen($param)) $result = true;
} else if($operator === '==') {
if(strlen($category->id) == strlen($param)) $result = true;
} else if($operator === '!=') {
if(strlen($category->id) != strlen($param)) $result = true;
} else if($operator >= '>=') {
if(strlen($category->id) >= strlen($param)) $result = true;
} else if($operator > '>') {
if(strlen($category->id) > strlen($param)) $result = true;
}
return $result;
})->count() > 0;
}
private function dissectParam(string $param) {
return array_map(function($param) {
return intval(trim($param));
}, explode(',', $param));
}
}