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
|
|
|
namespace Sylius\Component\Core\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface; |
17
|
|
|
use Sylius\Component\Product\Model\Product as BaseProduct; |
18
|
|
|
use Sylius\Component\Review\Model\ReviewInterface; |
19
|
|
|
use Sylius\Component\Shipping\Model\ShippingCategoryInterface; |
20
|
|
|
use Sylius\Component\Taxonomy\Model\TaxonInterface as BaseTaxonInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
24
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
25
|
|
|
* @author Anna Walasek <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Product extends BaseProduct implements ProductInterface, ReviewableProductInterface |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $variantSelectionMethod; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Collection|BaseTaxonInterface[] |
36
|
|
|
*/ |
37
|
|
|
protected $taxons; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ShippingCategoryInterface |
41
|
|
|
*/ |
42
|
|
|
protected $shippingCategory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ChannelInterface[]|Collection |
46
|
|
|
*/ |
47
|
|
|
protected $channels; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var BaseTaxonInterface |
51
|
|
|
*/ |
52
|
|
|
protected $mainTaxon; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Collection|ReviewInterface[] |
56
|
|
|
*/ |
57
|
|
|
protected $reviews; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var float |
61
|
|
|
*/ |
62
|
|
|
protected $averageRating = 0; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Collection|ImageInterface[] |
66
|
|
|
*/ |
67
|
|
|
protected $images; |
68
|
|
|
|
69
|
|
|
public function __construct() |
70
|
|
|
{ |
71
|
|
|
parent::__construct(); |
72
|
|
|
|
73
|
|
|
$this->taxons = new ArrayCollection(); |
74
|
|
|
$this->channels = new ArrayCollection(); |
75
|
|
|
$this->reviews = new ArrayCollection(); |
76
|
|
|
$this->images = new ArrayCollection(); |
77
|
|
|
|
78
|
|
|
$this->variantSelectionMethod = self::VARIANT_SELECTION_CHOICE; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getVariantSelectionMethod() |
85
|
|
|
{ |
86
|
|
|
return $this->variantSelectionMethod; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function setVariantSelectionMethod($variantSelectionMethod) |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if (!in_array($variantSelectionMethod, [self::VARIANT_SELECTION_CHOICE, self::VARIANT_SELECTION_MATCH])) { |
95
|
|
|
throw new \InvalidArgumentException(sprintf('Wrong variant selection method "%s" given.', $variantSelectionMethod)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->variantSelectionMethod = $variantSelectionMethod; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function isVariantSelectionMethodChoice() |
105
|
|
|
{ |
106
|
|
|
return self::VARIANT_SELECTION_CHOICE === $this->variantSelectionMethod; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getVariantSelectionMethodLabel() |
113
|
|
|
{ |
114
|
|
|
$labels = self::getVariantSelectionMethodLabels(); |
115
|
|
|
|
116
|
|
|
return $labels[$this->variantSelectionMethod]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function getTaxons($rootTaxonCode = null) |
123
|
|
|
{ |
124
|
|
|
if (null !== $rootTaxonCode) { |
125
|
|
|
return $this->taxons->filter(function (BaseTaxonInterface $taxon) use ($rootTaxonCode) { |
126
|
|
|
return $rootTaxonCode === strtolower($taxon->getRoot()->getCode()); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->taxons; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function addTaxon(BaseTaxonInterface $taxon) |
137
|
|
|
{ |
138
|
|
|
if (!$this->hasTaxon($taxon)) { |
139
|
|
|
$this->taxons->add($taxon); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
|
public function removeTaxon(BaseTaxonInterface $taxon) |
147
|
|
|
{ |
148
|
|
|
if ($this->hasTaxon($taxon)) { |
149
|
|
|
$this->taxons->removeElement($taxon); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function hasTaxon(BaseTaxonInterface $taxon) |
157
|
|
|
{ |
158
|
|
|
return $this->taxons->contains($taxon); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function getShippingCategory() |
165
|
|
|
{ |
166
|
|
|
return $this->shippingCategory; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function setShippingCategory(ShippingCategoryInterface $category = null) |
173
|
|
|
{ |
174
|
|
|
$this->shippingCategory = $category; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function getChannels() |
181
|
|
|
{ |
182
|
|
|
return $this->channels; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritdoc} |
187
|
|
|
*/ |
188
|
|
|
public function addChannel(BaseChannelInterface $channel) |
189
|
|
|
{ |
190
|
|
|
if (!$this->hasChannel($channel)) { |
191
|
|
|
$this->channels->add($channel); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
|
|
public function removeChannel(BaseChannelInterface $channel) |
199
|
|
|
{ |
200
|
|
|
if ($this->hasChannel($channel)) { |
201
|
|
|
$this->channels->removeElement($channel); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function hasChannel(BaseChannelInterface $channel) |
209
|
|
|
{ |
210
|
|
|
return $this->channels->contains($channel); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public static function getVariantSelectionMethodLabels() |
217
|
|
|
{ |
218
|
|
|
return [ |
219
|
|
|
self::VARIANT_SELECTION_CHOICE => 'Variant choice', |
220
|
|
|
self::VARIANT_SELECTION_MATCH => 'Options matching', |
221
|
|
|
]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function getShortDescription() |
228
|
|
|
{ |
229
|
|
|
return $this->getTranslation()->getShortDescription(); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
|
|
public function setShortDescription($shortDescription) |
236
|
|
|
{ |
237
|
|
|
$this->getTranslation()->setShortDescription($shortDescription); |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritdoc} |
242
|
|
|
*/ |
243
|
|
|
public function getMainTaxon() |
244
|
|
|
{ |
245
|
|
|
return $this->mainTaxon; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* {@inheritdoc} |
250
|
|
|
*/ |
251
|
|
|
public function setMainTaxon(TaxonInterface $mainTaxon = null) |
252
|
|
|
{ |
253
|
|
|
$this->mainTaxon = $mainTaxon; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function getReviews() |
260
|
|
|
{ |
261
|
|
|
return $this->reviews; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
|
|
public function addReview(ReviewInterface $review) |
268
|
|
|
{ |
269
|
|
|
$this->reviews->add($review); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* {@inheritdoc} |
274
|
|
|
*/ |
275
|
|
|
public function removeReview(ReviewInterface $review) |
276
|
|
|
{ |
277
|
|
|
$this->reviews->remove($review); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritdoc} |
282
|
|
|
*/ |
283
|
|
|
public function getAverageRating() |
284
|
|
|
{ |
285
|
|
|
return $this->averageRating; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritdoc} |
290
|
|
|
*/ |
291
|
|
|
public function setAverageRating($averageRating) |
292
|
|
|
{ |
293
|
|
|
$this->averageRating = $averageRating; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* {@inheritdoc} |
298
|
|
|
*/ |
299
|
|
|
public function getPrice() |
300
|
|
|
{ |
301
|
|
|
if ($this->variants->isEmpty()) { |
302
|
|
|
return null; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $this->variants->first()->getPrice(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* {@inheritdoc} |
310
|
|
|
*/ |
311
|
|
|
public function getImages() |
312
|
|
|
{ |
313
|
|
|
return $this->images; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritdoc} |
318
|
|
|
*/ |
319
|
|
|
public function getImageByCode($code) |
|
|
|
|
320
|
|
|
{ |
321
|
|
|
foreach ($this->images as $image) { |
322
|
|
|
if ($code === $image->getCode()) { |
323
|
|
|
return $image; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
return null; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* {@inheritdoc} |
332
|
|
|
*/ |
333
|
|
|
public function hasImages() |
334
|
|
|
{ |
335
|
|
|
return !$this->images->isEmpty(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* {@inheritdoc} |
340
|
|
|
*/ |
341
|
|
|
public function hasImage(ImageInterface $image) |
342
|
|
|
{ |
343
|
|
|
return $this->images->contains($image); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* {@inheritdoc} |
348
|
|
|
*/ |
349
|
|
|
public function addImage(ImageInterface $image) |
350
|
|
|
{ |
351
|
|
|
$image->setOwner($this); |
352
|
|
|
$this->images->add($image); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* {@inheritdoc} |
357
|
|
|
*/ |
358
|
|
|
public function removeImage(ImageInterface $image) |
359
|
|
|
{ |
360
|
|
|
if ($this->hasImage($image)) { |
361
|
|
|
$image->setOwner(null); |
362
|
|
|
$this->images->removeElement($image); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* {@inheritdoc} |
368
|
|
|
*/ |
369
|
|
|
protected function createTranslation() |
370
|
|
|
{ |
371
|
|
|
return new ProductTranslation(); |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|