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 Webmozart\Assert\Assert; |
17
|
|
|
use Sylius\Component\Attribute\Model\AttributeValueInterface; |
18
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
19
|
|
|
use Sylius\Component\Resource\Model\ToggleableTrait; |
20
|
|
|
use Sylius\Component\Resource\Model\TranslatableTrait; |
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->createdAt = new \DateTime(); |
78
|
|
|
$this->availableOn = new \DateTime(); |
79
|
|
|
$this->attributes = new ArrayCollection(); |
80
|
|
|
$this->associations = new ArrayCollection(); |
81
|
|
|
$this->variants = new ArrayCollection(); |
82
|
|
|
$this->options = new ArrayCollection(); |
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 getAttributesByLocale($localeCode, $fallbackLocaleCode) |
249
|
|
|
{ |
250
|
|
|
$attributes = $this->attributes->filter( |
251
|
|
|
function (ProductAttributeValueInterface $attribute) use ($fallbackLocaleCode) { |
252
|
|
|
return $attribute->getLocaleCode() === $fallbackLocaleCode; |
253
|
|
|
} |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$attributesWithFallback = []; |
257
|
|
|
foreach ($attributes as $attribute) { |
258
|
|
|
$attributesWithFallback[] = $this->getAttributeInDifferentLocale($attribute, $localeCode); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $attributesWithFallback; |
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* {@inheritdoc} |
266
|
|
|
*/ |
267
|
|
|
public function addAttribute(AttributeValueInterface $attribute) |
268
|
|
|
{ |
269
|
|
|
Assert::isInstanceOf( |
270
|
|
|
$attribute, |
271
|
|
|
ProductAttributeValueInterface::class, |
272
|
|
|
'Attribute objects added to a Product object have to implement ProductAttributeValueInterface' |
273
|
|
|
); |
274
|
|
|
|
275
|
|
|
if (!$this->hasAttribute($attribute)) { |
276
|
|
|
$attribute->setProduct($this); |
|
|
|
|
277
|
|
|
$this->attributes->add($attribute); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* {@inheritdoc} |
283
|
|
|
*/ |
284
|
|
|
public function removeAttribute(AttributeValueInterface $attribute) |
285
|
|
|
{ |
286
|
|
|
Assert::isInstanceOf( |
287
|
|
|
$attribute, |
288
|
|
|
ProductAttributeValueInterface::class, |
289
|
|
|
'Attribute objects removed from a Product object have to implement ProductAttributeValueInterface' |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
if ($this->hasAttribute($attribute)) { |
293
|
|
|
$this->attributes->removeElement($attribute); |
294
|
|
|
$attribute->setProduct(null); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* {@inheritdoc} |
300
|
|
|
*/ |
301
|
|
|
public function hasAttribute(AttributeValueInterface $attribute) |
302
|
|
|
{ |
303
|
|
|
return $this->attributes->contains($attribute); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* {@inheritdoc} |
308
|
|
|
*/ |
309
|
|
|
public function hasAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
310
|
|
|
{ |
311
|
|
|
if (null === $localeCode) { |
312
|
|
|
$localeCode = $this->getTranslation()->getLocale(); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
foreach ($this->attributes as $attribute) { |
316
|
|
|
if ($attribute->getAttribute()->getCode() === $attributeCode && |
317
|
|
|
$attribute->getLocaleCode() === $localeCode) { |
318
|
|
|
return true; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return false; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* {@inheritdoc} |
327
|
|
|
*/ |
328
|
|
|
public function getAttributeByCodeAndLocale($attributeCode, $localeCode = null) |
329
|
|
|
{ |
330
|
|
|
if (null === $localeCode) { |
331
|
|
|
$localeCode = $this->getTranslation()->getLocale(); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
foreach ($this->attributes as $attribute) { |
335
|
|
|
if ($attribute->getAttribute()->getCode() === $attributeCode && |
336
|
|
|
$attribute->getLocaleCode() === $localeCode) { |
337
|
|
|
return $attribute; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return null; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* {@inheritdoc} |
346
|
|
|
*/ |
347
|
|
|
public function hasVariants() |
348
|
|
|
{ |
349
|
|
|
return !$this->getVariants()->isEmpty(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* {@inheritdoc} |
354
|
|
|
*/ |
355
|
|
|
public function getVariants() |
356
|
|
|
{ |
357
|
|
|
return $this->variants; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* {@inheritdoc} |
362
|
|
|
*/ |
363
|
|
|
public function getAvailableVariants() |
364
|
|
|
{ |
365
|
|
|
return $this->variants->filter(function (ProductVariantInterface $variant) { |
366
|
|
|
return $variant->isAvailable(); |
367
|
|
|
}); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* {@inheritdoc} |
372
|
|
|
*/ |
373
|
|
|
public function addVariant(ProductVariantInterface $variant) |
374
|
|
|
{ |
375
|
|
|
if (!$this->hasVariant($variant)) { |
376
|
|
|
$variant->setProduct($this); |
377
|
|
|
$this->variants->add($variant); |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* {@inheritdoc} |
383
|
|
|
*/ |
384
|
|
|
public function removeVariant(ProductVariantInterface $variant) |
385
|
|
|
{ |
386
|
|
|
if ($this->hasVariant($variant)) { |
387
|
|
|
$variant->setProduct(null); |
388
|
|
|
$this->variants->removeElement($variant); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* {@inheritdoc} |
394
|
|
|
*/ |
395
|
|
|
public function hasVariant(ProductVariantInterface $variant) |
396
|
|
|
{ |
397
|
|
|
return $this->variants->contains($variant); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* {@inheritdoc} |
402
|
|
|
*/ |
403
|
|
|
public function hasOptions() |
404
|
|
|
{ |
405
|
|
|
return !$this->options->isEmpty(); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* {@inheritdoc} |
410
|
|
|
*/ |
411
|
|
|
public function getOptions() |
412
|
|
|
{ |
413
|
|
|
return $this->options; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* {@inheritdoc} |
418
|
|
|
*/ |
419
|
|
|
public function addOption(ProductOptionInterface $option) |
420
|
|
|
{ |
421
|
|
|
if (!$this->hasOption($option)) { |
422
|
|
|
$this->options->add($option); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* {@inheritdoc} |
428
|
|
|
*/ |
429
|
|
|
public function removeOption(ProductOptionInterface $option) |
430
|
|
|
{ |
431
|
|
|
if ($this->hasOption($option)) { |
432
|
|
|
$this->options->removeElement($option); |
433
|
|
|
} |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* {@inheritdoc} |
438
|
|
|
*/ |
439
|
|
|
public function hasOption(ProductOptionInterface $option) |
440
|
|
|
{ |
441
|
|
|
return $this->options->contains($option); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* {@inheritdoc} |
446
|
|
|
*/ |
447
|
|
|
public function getAssociations() |
448
|
|
|
{ |
449
|
|
|
return $this->associations; |
|
|
|
|
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* {@inheritdoc} |
454
|
|
|
*/ |
455
|
|
|
public function addAssociation(ProductAssociationInterface $association) |
456
|
|
|
{ |
457
|
|
|
if (!$this->hasAssociation($association)) { |
458
|
|
|
$this->associations->add($association); |
459
|
|
|
$association->setOwner($this); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* {@inheritdoc} |
465
|
|
|
*/ |
466
|
|
|
public function removeAssociation(ProductAssociationInterface $association) |
467
|
|
|
{ |
468
|
|
|
if ($this->hasAssociation($association)) { |
469
|
|
|
$association->setOwner(null); |
470
|
|
|
$this->associations->removeElement($association); |
471
|
|
|
} |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* {@inheritdoc} |
476
|
|
|
*/ |
477
|
|
|
public function hasAssociation(ProductAssociationInterface $association) |
478
|
|
|
{ |
479
|
|
|
return $this->associations->contains($association); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* {@inheritdoc} |
484
|
|
|
*/ |
485
|
|
|
public function isSimple() |
486
|
|
|
{ |
487
|
|
|
return 1 === $this->variants->count() && !$this->hasOptions(); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* {@inheritdoc} |
492
|
|
|
*/ |
493
|
|
|
public function isConfigurable() |
494
|
|
|
{ |
495
|
|
|
return !$this->isSimple(); |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* {@inheritdoc} |
500
|
|
|
*/ |
501
|
|
|
protected function createTranslation() |
502
|
|
|
{ |
503
|
|
|
return new ProductTranslation(); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* {@inheritdoc} |
508
|
|
|
*/ |
509
|
|
|
private function getAttributeInDifferentLocale(ProductAttributeValueInterface $attributeValue, $localeCode) |
510
|
|
|
{ |
511
|
|
|
if (!$this->hasAttributeByCodeAndLocale($attributeValue->getCode(), $localeCode)) { |
512
|
|
|
return $attributeValue; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
$attributeValueInDifferentLocale = $this->getAttributeByCodeAndLocale($attributeValue->getCode(), $localeCode); |
516
|
|
|
if ('' === $attributeValueInDifferentLocale->getValue() |
517
|
|
|
|| null === $attributeValueInDifferentLocale->getValue()) { |
518
|
|
|
return $attributeValue; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
return $attributeValueInDifferentLocale; |
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: