1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CatalogBundle\Entity; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Doctrine\Common\Collections\Collection; |
17
|
|
|
use Knp\DoctrineBehaviors\Model\Blameable\Blameable; |
18
|
|
|
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable; |
19
|
|
|
use Knp\DoctrineBehaviors\Model\Translatable\Translatable; |
20
|
|
|
use WellCommerce\Bundle\CoreBundle\Doctrine\Behaviours\Identifiable; |
21
|
|
|
use WellCommerce\Bundle\CoreBundle\Entity\EntityInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class AttributeValue |
25
|
|
|
* |
26
|
|
|
* @author Adam Piotrowski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class AttributeValue implements EntityInterface |
29
|
|
|
{ |
30
|
|
|
use Identifiable; |
31
|
|
|
use Translatable; |
32
|
|
|
use Timestampable; |
33
|
|
|
use Blameable; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Collection |
37
|
|
|
*/ |
38
|
|
|
protected $attributes; |
39
|
|
|
|
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->attributes = new ArrayCollection(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getAttributes(): Collection |
46
|
|
|
{ |
47
|
|
|
return $this->attributes; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function setAttributes(Collection $attributes) |
51
|
|
|
{ |
52
|
|
|
$this->syncOldAttributes($attributes); |
53
|
|
|
$this->syncNewAttributes($attributes); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function addAttribute(Attribute $attribute) |
57
|
|
|
{ |
58
|
|
|
$this->attributes->add($attribute); |
59
|
|
|
$attribute->addValue($this); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function translate($locale = null, $fallbackToDefault = true): AttributeValueTranslation |
63
|
|
|
{ |
64
|
|
|
return $this->doTranslate($locale, $fallbackToDefault); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function syncOldAttributes(Collection $attributes) |
68
|
|
|
{ |
69
|
|
|
$this->attributes->map(function (Attribute $attribute) use ($attributes) { |
70
|
|
|
if (false === $attributes->contains($attribute)) { |
71
|
|
|
$attribute->removeValue($this); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function syncNewAttributes(Collection $attributes) |
77
|
|
|
{ |
78
|
|
|
$attributes->map(function (Attribute $attribute) { |
79
|
|
|
if (false === $this->attributes->contains($attribute)) { |
80
|
|
|
$attribute->addValue($this); |
81
|
|
|
} |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|