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