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\Product\Model; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Sylius\Component\Attribute\Model\AttributeValueInterface; |
17
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
18
|
|
|
use Sylius\Component\Resource\Model\ToggleableTrait; |
19
|
|
|
use Sylius\Component\Resource\Model\TranslatableTrait; |
20
|
|
|
use Sylius\Component\Resource\Model\TranslationInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
24
|
|
|
* @author Gonzalo Vilaseca <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Product implements ProductInterface |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
use TimestampableTrait, ToggleableTrait; |
29
|
|
|
use TranslatableTrait { |
30
|
|
|
__construct as private initializeTranslationsCollection; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
protected $id; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $code; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \DateTime |
45
|
|
|
*/ |
46
|
|
|
protected $availableOn; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \DateTime |
50
|
|
|
*/ |
51
|
|
|
protected $availableUntil; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Collection|AttributeValueInterface[] |
55
|
|
|
*/ |
56
|
|
|
protected $attributes; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Collection|ProductVariantInterface[] |
60
|
|
|
*/ |
61
|
|
|
protected $variants; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Collection|ProductOptionInterface[] |
65
|
|
|
*/ |
66
|
|
|
protected $options; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Collection|ProductAssociationInterface[] |
70
|
|
|
*/ |
71
|
|
|
protected $associations; |
72
|
|
|
|
73
|
|
|
public function __construct() |
74
|
|
|
{ |
75
|
|
|
$this->initializeTranslationsCollection(); |
76
|
|
|
|
77
|
|
|
$this->availableOn = new \DateTime(); |
78
|
|
|
$this->attributes = new ArrayCollection(); |
79
|
|
|
$this->associations = new ArrayCollection(); |
80
|
|
|
$this->variants = new ArrayCollection(); |
81
|
|
|
$this->options = new ArrayCollection(); |
82
|
|
|
$this->createdAt = new \DateTime(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function __toString() |
89
|
|
|
{ |
90
|
|
|
return $this->getName(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function getId() |
97
|
|
|
{ |
98
|
|
|
return $this->id; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getCode() |
105
|
|
|
{ |
106
|
|
|
return $this->code; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $code |
111
|
|
|
*/ |
112
|
|
|
public function setCode($code) |
113
|
|
|
{ |
114
|
|
|
$this->code = $code; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function getName() |
121
|
|
|
{ |
122
|
|
|
return $this->getTranslation()->getName(); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function setName($name) |
129
|
|
|
{ |
130
|
|
|
$this->getTranslation()->setName($name); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getSlug() |
137
|
|
|
{ |
138
|
|
|
return $this->getTranslation()->getSlug(); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function setSlug($slug = null) |
145
|
|
|
{ |
146
|
|
|
$this->getTranslation()->setSlug($slug); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getDescription() |
153
|
|
|
{ |
154
|
|
|
return $this->getTranslation()->getDescription(); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
public function setDescription($description) |
161
|
|
|
{ |
162
|
|
|
$this->getTranslation()->setDescription($description); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
|
|
public function getMetaKeywords() |
169
|
|
|
{ |
170
|
|
|
return $this->getTranslation()->getMetaKeywords(); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
|
|
public function setMetaKeywords($metaKeywords) |
177
|
|
|
{ |
178
|
|
|
$this->getTranslation()->setMetaKeywords($metaKeywords); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
*/ |
184
|
|
|
public function getMetaDescription() |
185
|
|
|
{ |
186
|
|
|
return $this->getTranslation()->getMetaDescription(); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
|
|
public function setMetaDescription($metaDescription) |
193
|
|
|
{ |
194
|
|
|
$this->getTranslation()->setMetaDescription($metaDescription); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
public function isAvailable() |
201
|
|
|
{ |
202
|
|
|
return (new DateRange($this->availableOn, $this->availableUntil))->isInRange(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritdoc} |
207
|
|
|
*/ |
208
|
|
|
public function getAvailableOn() |
209
|
|
|
{ |
210
|
|
|
return $this->availableOn; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* {@inheritdoc} |
215
|
|
|
*/ |
216
|
|
|
public function setAvailableOn(\DateTime $availableOn = null) |
217
|
|
|
{ |
218
|
|
|
$this->availableOn = $availableOn; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* {@inheritdoc} |
223
|
|
|
*/ |
224
|
|
|
public function getAvailableUntil() |
225
|
|
|
{ |
226
|
|
|
return $this->availableUntil; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* {@inheritdoc} |
231
|
|
|
*/ |
232
|
|
|
public function setAvailableUntil(\DateTime $availableUntil = null) |
233
|
|
|
{ |
234
|
|
|
$this->availableUntil = $availableUntil; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* {@inheritdoc} |
239
|
|
|
*/ |
240
|
|
|
public function getAttributes() |
241
|
|
|
{ |
242
|
|
|
return $this->attributes; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* {@inheritdoc} |
247
|
|
|
*/ |
248
|
|
|
public function addAttribute(AttributeValueInterface $attribute) |
249
|
|
|
{ |
250
|
|
|
if (!$this->hasAttribute($attribute)) { |
251
|
|
|
$attribute->setProduct($this); |
|
|
|
|
252
|
|
|
$this->attributes->add($attribute); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
|
public function removeAttribute(AttributeValueInterface $attribute) |
260
|
|
|
{ |
261
|
|
|
if ($this->hasAttribute($attribute)) { |
262
|
|
|
$this->attributes->removeElement($attribute); |
263
|
|
|
$attribute->setProduct(null); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
|
|
public function hasAttribute(AttributeValueInterface $attribute) |
271
|
|
|
{ |
272
|
|
|
return $this->attributes->contains($attribute); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
|
|
public function hasAttributeByCode($attributeCode) |
279
|
|
|
{ |
280
|
|
|
foreach ($this->attributes as $attribute) { |
281
|
|
|
if ($attribute->getAttribute()->getCode() === $attributeCode) { |
282
|
|
|
return true; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* {@inheritdoc} |
291
|
|
|
*/ |
292
|
|
|
public function getAttributeByCode($attributeCode) |
293
|
|
|
{ |
294
|
|
|
foreach ($this->attributes as $attribute) { |
295
|
|
|
if ($attributeCode === $attribute->getAttribute()->getCode()) { |
296
|
|
|
return $attribute; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return null; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* {@inheritdoc} |
305
|
|
|
*/ |
306
|
|
|
public function hasVariants() |
307
|
|
|
{ |
308
|
|
|
return !$this->getVariants()->isEmpty(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* {@inheritdoc} |
313
|
|
|
*/ |
314
|
|
|
public function getVariants() |
315
|
|
|
{ |
316
|
|
|
return $this->variants; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* {@inheritdoc} |
321
|
|
|
*/ |
322
|
|
|
public function getAvailableVariants() |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
return $this->variants->filter(function (ProductVariantInterface $variant) { |
325
|
|
|
return $variant->isAvailable(); |
326
|
|
|
}); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
|
|
public function addVariant(ProductVariantInterface $variant) |
333
|
|
|
{ |
334
|
|
|
if (!$this->hasVariant($variant)) { |
335
|
|
|
$variant->setProduct($this); |
336
|
|
|
$this->variants->add($variant); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* {@inheritdoc} |
342
|
|
|
*/ |
343
|
|
|
public function removeVariant(ProductVariantInterface $variant) |
344
|
|
|
{ |
345
|
|
|
if ($this->hasVariant($variant)) { |
346
|
|
|
$variant->setProduct(null); |
347
|
|
|
$this->variants->removeElement($variant); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* {@inheritdoc} |
353
|
|
|
*/ |
354
|
|
|
public function hasVariant(ProductVariantInterface $variant) |
355
|
|
|
{ |
356
|
|
|
return $this->variants->contains($variant); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* {@inheritdoc} |
361
|
|
|
*/ |
362
|
|
|
public function hasOptions() |
363
|
|
|
{ |
364
|
|
|
return !$this->options->isEmpty(); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* {@inheritdoc} |
369
|
|
|
*/ |
370
|
|
|
public function getOptions() |
371
|
|
|
{ |
372
|
|
|
return $this->options; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* {@inheritdoc} |
377
|
|
|
*/ |
378
|
|
|
public function addOption(ProductOptionInterface $option) |
379
|
|
|
{ |
380
|
|
|
if (!$this->hasOption($option)) { |
381
|
|
|
$this->options->add($option); |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* {@inheritdoc} |
387
|
|
|
*/ |
388
|
|
|
public function removeOption(ProductOptionInterface $option) |
389
|
|
|
{ |
390
|
|
|
if ($this->hasOption($option)) { |
391
|
|
|
$this->options->removeElement($option); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* {@inheritdoc} |
397
|
|
|
*/ |
398
|
|
|
public function hasOption(ProductOptionInterface $option) |
399
|
|
|
{ |
400
|
|
|
return $this->options->contains($option); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* {@inheritdoc} |
405
|
|
|
*/ |
406
|
|
|
public function getAssociations() |
407
|
|
|
{ |
408
|
|
|
return $this->associations; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* {@inheritdoc} |
413
|
|
|
*/ |
414
|
|
|
public function addAssociation(ProductAssociationInterface $association) |
415
|
|
|
{ |
416
|
|
|
if (!$this->hasAssociation($association)) { |
417
|
|
|
$this->associations->add($association); |
418
|
|
|
$association->setOwner($this); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* {@inheritdoc} |
424
|
|
|
*/ |
425
|
|
|
public function removeAssociation(ProductAssociationInterface $association) |
426
|
|
|
{ |
427
|
|
|
if ($this->hasAssociation($association)) { |
428
|
|
|
$association->setOwner(null); |
429
|
|
|
$this->associations->removeElement($association); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* {@inheritdoc} |
435
|
|
|
*/ |
436
|
|
|
public function hasAssociation(ProductAssociationInterface $association) |
|
|
|
|
437
|
|
|
{ |
438
|
|
|
return $this->associations->contains($association); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* {@inheritdoc} |
443
|
|
|
*/ |
444
|
|
|
public function isSimple() |
445
|
|
|
{ |
446
|
|
|
return 1 === $this->variants->count() && !$this->hasOptions(); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* {@inheritdoc} |
451
|
|
|
*/ |
452
|
|
|
public function isConfigurable() |
453
|
|
|
{ |
454
|
|
|
return !$this->isSimple(); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* {@inheritdoc} |
459
|
|
|
*/ |
460
|
|
|
protected function createTranslation() |
461
|
|
|
{ |
462
|
|
|
return new ProductTranslation(); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|