|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\Content\Rule; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Cart\Cart; |
|
6
|
|
|
use Shopware\Core\Checkout\Cart\Rule\CartRuleScope; |
|
7
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection; |
|
8
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @method void add(RuleEntity $entity) |
|
12
|
61 |
|
* @method void set(string $key, RuleEntity $entity) |
|
13
|
|
|
* @method RuleEntity[] getIterator() |
|
14
|
61 |
|
* @method RuleEntity[] getElements() |
|
15
|
|
|
* @method RuleEntity|null get(string $key) |
|
16
|
|
|
* @method RuleEntity|null first() |
|
17
|
61 |
|
* @method RuleEntity|null last() |
|
18
|
|
|
*/ |
|
19
|
|
|
class RuleCollection extends EntityCollection |
|
20
|
|
|
{ |
|
21
|
61 |
|
public function filterMatchingRules(Cart $cart, SalesChannelContext $context) |
|
22
|
|
|
{ |
|
23
|
|
|
return $this->filter( |
|
24
|
|
|
function (RuleEntity $rule) use ($cart, $context) { |
|
25
|
61 |
|
return $rule->getPayload()->match(new CartRuleScope($cart, $context)); |
|
26
|
61 |
|
} |
|
27
|
|
|
); |
|
28
|
21 |
|
} |
|
29
|
|
|
|
|
30
|
21 |
|
public function sortByPriority(): void |
|
31
|
|
|
{ |
|
32
|
|
|
$this->sort(function (RuleEntity $a, RuleEntity $b) { |
|
33
|
|
|
return $b->getPriority() <=> $a->getPriority(); |
|
34
|
|
|
}); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function equals(RuleCollection $rules): bool |
|
38
|
|
|
{ |
|
39
|
|
|
if ($this->count() !== $rules->count()) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
foreach ($this->elements as $element) { |
|
44
|
|
|
if (!$rules->has($element->getId())) { |
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getApiAlias(): string |
|
53
|
|
|
{ |
|
54
|
|
|
return 'rule_collection'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getExpectedClass(): string |
|
58
|
|
|
{ |
|
59
|
|
|
return RuleEntity::class; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|