1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace SyliusCart\Domain\ModelCollection; |
6
|
|
|
|
7
|
|
|
use SyliusCart\Domain\Exception\CartItemNotFoundException; |
8
|
|
|
use SyliusCart\Domain\Model\CartItem; |
9
|
|
|
use SyliusCart\Domain\ValueObject\ProductCode; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
final class CartItems implements CartItemCollection |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \ArrayObject |
18
|
|
|
*/ |
19
|
|
|
private $items = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $items |
23
|
|
|
*/ |
24
|
|
|
private function __construct(array $items = []) |
25
|
|
|
{ |
26
|
|
|
$this->items = new \ArrayObject($items); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $items |
31
|
|
|
* |
32
|
|
|
* @return CartItems |
33
|
|
|
*/ |
34
|
|
|
public static function fromArray(array $items): self |
35
|
|
|
{ |
36
|
|
|
return new self($items); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return CartItemCollection |
41
|
|
|
*/ |
42
|
|
|
public static function createEmpty(): CartItemCollection |
43
|
|
|
{ |
44
|
|
|
return new self(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function add(CartItem $cartItem): void |
51
|
|
|
{ |
52
|
|
|
if ($this->exists($cartItem)) { |
53
|
|
|
/** @var CartItem $existingCartItem */ |
54
|
|
|
$existingCartItem = $this->items->offsetGet((string) $cartItem->productCode()); |
55
|
|
|
$newCartItem = $existingCartItem->merge($cartItem); |
56
|
|
|
$this->items->offsetSet((string) $newCartItem->productCode(), $newCartItem); |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->items->offsetSet((string) $cartItem->productCode(), $cartItem); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function remove(CartItem $cartItem): void |
68
|
|
|
{ |
69
|
|
|
if (!$this->exists($cartItem)) { |
70
|
|
|
throw new CartItemNotFoundException(sprintf('Cart item with product code "%s" does not exist.', $cartItem->productCode())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->items->offsetUnset((string) $cartItem->productCode()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function findOneByProductCode(ProductCode $productCode): CartItem |
80
|
|
|
{ |
81
|
|
|
if (!$this->items->offsetExists((string) $productCode)) { |
82
|
|
|
throw new CartItemNotFoundException(sprintf('Cart item with product code "%s" does not exist.', $productCode)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->items->offsetGet((string) $productCode); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function exists(CartItem $cartItem): bool |
92
|
|
|
{ |
93
|
|
|
return $this->items->offsetExists((string) $cartItem->productCode()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function findAll(): array |
100
|
|
|
{ |
101
|
|
|
return $this->items->getArrayCopy(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function clear(): void |
105
|
|
|
{ |
106
|
|
|
$this->items = new \ArrayObject([]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getIterator(): \ArrayIterator |
113
|
|
|
{ |
114
|
|
|
return $this->items->getIterator(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function count(): int |
121
|
|
|
{ |
122
|
|
|
return $this->items->count(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function isEmpty(): bool |
129
|
|
|
{ |
130
|
|
|
return 0 === $this->items->count(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|