|
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\Association\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Sylius\Component\Resource\Model\TimestampableTrait; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Leszek Prabucki <[email protected]> |
|
20
|
|
|
* @author Łukasz Chruściel <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Association implements AssociationInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use TimestampableTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $id; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var AssociationTypeInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $type; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var AssociableInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $owner; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Collection<AssociableInterface> |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $associatedObjects; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->createdAt = new \DateTime(); |
|
49
|
|
|
$this->updatedAt = new \DateTime(); |
|
50
|
|
|
$this->associatedObjects = new ArrayCollection(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getId() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->id; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getType() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->type; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function setType(AssociationTypeInterface $type) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->type = $type; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getOwner() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->owner; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setOwner(AssociableInterface $owner = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->owner = $owner; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getAssociatedObjects() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->associatedObjects; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function hasAssociatedObject(AssociableInterface $associatedObject) |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->associatedObjects->contains($associatedObject); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function addAssociatedObject(AssociableInterface $associatedObject) |
|
113
|
|
|
{ |
|
114
|
|
|
if (!$this->hasAssociatedObject($associatedObject)) { |
|
115
|
|
|
$this->associatedObjects->add($associatedObject); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
|
|
public function removeAssociatedObject(AssociableInterface $associatedObject) |
|
123
|
|
|
{ |
|
124
|
|
|
if (!$this->hasAssociatedObject($associatedObject)) { |
|
125
|
|
|
$this->associatedObjects->removeElement($associatedObject); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|