|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Component\Core\Model; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection; |
|
18
|
|
|
use Doctrine\Common\Comparable; |
|
19
|
|
|
use Sylius\Component\Product\Model\ProductVariant as BaseVariant; |
|
20
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
|
21
|
|
|
use Sylius\Component\Taxation\Model\TaxCategoryInterface; |
|
22
|
|
|
|
|
23
|
|
|
class ProductVariant extends BaseVariant implements ProductVariantInterface, Comparable |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var int */ |
|
26
|
|
|
protected $version = 1; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
protected $onHold = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
protected $onHand = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** @var bool */ |
|
35
|
|
|
protected $tracked = false; |
|
36
|
|
|
|
|
37
|
|
|
/** @var float */ |
|
38
|
|
|
protected $weight; |
|
39
|
|
|
|
|
40
|
|
|
/** @var float */ |
|
41
|
|
|
protected $width; |
|
42
|
|
|
|
|
43
|
|
|
/** @var float */ |
|
44
|
|
|
protected $height; |
|
45
|
|
|
|
|
46
|
|
|
/** @var float */ |
|
47
|
|
|
protected $depth; |
|
48
|
|
|
|
|
49
|
|
|
/** @var TaxCategoryInterface */ |
|
50
|
|
|
protected $taxCategory; |
|
51
|
|
|
|
|
52
|
|
|
/** @var ShippingCategoryInterface */ |
|
53
|
|
|
protected $shippingCategory; |
|
54
|
|
|
|
|
55
|
|
|
/** @var Collection */ |
|
56
|
|
|
protected $channelPricings; |
|
57
|
|
|
|
|
58
|
|
|
/** @var bool */ |
|
59
|
|
|
protected $shippingRequired = true; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var Collection|ProductImageInterface[] |
|
63
|
|
|
* |
|
64
|
|
|
* @psalm-var Collection<array-key, ProductImageInterface> |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $images; |
|
67
|
|
|
|
|
68
|
|
|
public function __construct() |
|
69
|
|
|
{ |
|
70
|
|
|
parent::__construct(); |
|
71
|
|
|
|
|
72
|
|
|
/** @var ArrayCollection<array-key, ChannelPricingInterface> $this->channelPricings */ |
|
73
|
|
|
$this->channelPricings = new ArrayCollection(); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** @var ArrayCollection<array-key, ProductImageInterface> $this->images */ |
|
76
|
|
|
$this->images = new ArrayCollection(); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function __toString(): string |
|
80
|
|
|
{ |
|
81
|
|
|
$string = (string) $this->getProduct()->getName(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$this->getOptionValues()->isEmpty()) { |
|
84
|
|
|
$string .= '('; |
|
85
|
|
|
|
|
86
|
|
|
foreach ($this->getOptionValues() as $option) { |
|
87
|
|
|
$string .= $option->getOption()->getName() . ': ' . $option->getValue() . ', '; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$string = substr($string, 0, -2) . ')'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $string; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getVersion(): ?int |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->version; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setVersion(?int $version): void |
|
102
|
|
|
{ |
|
103
|
|
|
$this->version = $version; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function isInStock(): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return 0 < $this->onHand; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getOnHold(): ?int |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->onHold; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function setOnHold(?int $onHold): void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->onHold = $onHold; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getOnHand(): ?int |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->onHand; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function setOnHand(?int $onHand): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->onHand = (0 > $onHand) ? 0 : $onHand; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function isTracked(): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->tracked; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function setTracked(bool $tracked): void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->tracked = $tracked; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function getInventoryName(): ?string |
|
|
|
|
|
|
142
|
|
|
{ |
|
143
|
|
|
return $this->getProduct()->getName(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getShippingCategory(): ?ShippingCategoryInterface |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->shippingCategory; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function setShippingCategory(?ShippingCategoryInterface $category): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->shippingCategory = $category; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function getWeight(): ?float |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->weight; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function setWeight(?float $weight): void |
|
162
|
|
|
{ |
|
163
|
|
|
$this->weight = $weight; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function getWidth(): ?float |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->width; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function setWidth(?float $width): void |
|
172
|
|
|
{ |
|
173
|
|
|
$this->width = $width; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function getHeight(): ?float |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->height; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function setHeight(?float $height): void |
|
182
|
|
|
{ |
|
183
|
|
|
$this->height = $height; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getDepth(): ?float |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->depth; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
public function setDepth(?float $depth): void |
|
192
|
|
|
{ |
|
193
|
|
|
$this->depth = $depth; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function getShippingWeight(): ?float |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->getWeight(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function getShippingWidth(): ?float |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->getWidth(); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function getShippingHeight(): ?float |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->getHeight(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getShippingDepth(): ?float |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->getDepth(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getShippingVolume(): ?float |
|
217
|
|
|
{ |
|
218
|
|
|
return $this->depth * $this->height * $this->width; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
public function getTaxCategory(): ?TaxCategoryInterface |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->taxCategory; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public function setTaxCategory(?TaxCategoryInterface $category): void |
|
227
|
|
|
{ |
|
228
|
|
|
$this->taxCategory = $category; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function getChannelPricings(): Collection |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->channelPricings; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function getChannelPricingForChannel(ChannelInterface $channel): ?ChannelPricingInterface |
|
|
|
|
|
|
237
|
|
|
{ |
|
238
|
|
|
if ($this->channelPricings->containsKey($channel->getCode())) { |
|
239
|
|
|
return $this->channelPricings->get($channel->getCode()); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return null; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function hasChannelPricingForChannel(ChannelInterface $channel): bool |
|
246
|
|
|
{ |
|
247
|
|
|
return null !== $this->getChannelPricingForChannel($channel); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function hasChannelPricing(ChannelPricingInterface $channelPricing): bool |
|
251
|
|
|
{ |
|
252
|
|
|
return $this->channelPricings->contains($channelPricing); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
public function addChannelPricing(ChannelPricingInterface $channelPricing): void |
|
256
|
|
|
{ |
|
257
|
|
|
if (!$this->hasChannelPricing($channelPricing)) { |
|
258
|
|
|
$channelPricing->setProductVariant($this); |
|
259
|
|
|
$this->channelPricings->set($channelPricing->getChannelCode(), $channelPricing); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
public function removeChannelPricing(ChannelPricingInterface $channelPricing): void |
|
264
|
|
|
{ |
|
265
|
|
|
if ($this->hasChannelPricing($channelPricing)) { |
|
266
|
|
|
$channelPricing->setProductVariant(null); |
|
267
|
|
|
$this->channelPricings->remove($channelPricing->getChannelCode()); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function isShippingRequired(): bool |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->shippingRequired; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function setShippingRequired(bool $shippingRequired): void |
|
277
|
|
|
{ |
|
278
|
|
|
$this->shippingRequired = $shippingRequired; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220 |
|
283
|
|
|
* @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220 |
|
284
|
|
|
*/ |
|
285
|
|
|
public function getImages(): Collection |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->images; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @psalm-suppress InvalidReturnType https://github.com/doctrine/collections/pull/220 |
|
292
|
|
|
* @psalm-suppress InvalidReturnStatement https://github.com/doctrine/collections/pull/220 |
|
293
|
|
|
*/ |
|
294
|
|
|
public function getImagesByType(string $type): Collection |
|
295
|
|
|
{ |
|
296
|
|
|
return $this->images->filter(function (ProductImageInterface $image) use ($type): bool { |
|
297
|
|
|
return $type === $image->getType(); |
|
298
|
|
|
}); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function hasImages(): bool |
|
302
|
|
|
{ |
|
303
|
|
|
return !$this->images->isEmpty(); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
public function hasImage(ProductImageInterface $image): bool |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->images->contains($image); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
public function addImage(ProductImageInterface $image): void |
|
312
|
|
|
{ |
|
313
|
|
|
if ($this->hasImage($image)) { |
|
314
|
|
|
return; |
|
315
|
|
|
} |
|
316
|
|
|
$image->setOwner($this->getProduct()); |
|
317
|
|
|
$image->addProductVariant($this); |
|
318
|
|
|
$this->images->add($image); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
public function removeImage(ProductImageInterface $image): void |
|
322
|
|
|
{ |
|
323
|
|
|
if ($this->hasImage($image)) { |
|
324
|
|
|
$this->images->removeElement($image); |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @inheritDoc |
|
330
|
|
|
*/ |
|
331
|
|
|
public function compareTo($other): int |
|
332
|
|
|
{ |
|
333
|
|
|
return $this->code === $other->getCode() ? 0 : 1; |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..