|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace My\Test\Project\Entity\Relations\Order\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
8
|
|
|
use Symfony\Component\Validator\Constraints\Valid; |
|
9
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
10
|
|
|
use My\Test\Project\Entities\Order as Order; |
|
11
|
|
|
use My\Test\Project\Entity\Relations\Order\Interfaces\HasOrdersInterface; |
|
12
|
|
|
use My\Test\Project\Entity\Relations\Order\Interfaces\ReciprocatesOrderInterface; |
|
13
|
|
|
|
|
14
|
|
|
trait HasOrdersAbstract |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var ArrayCollection|Order[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $orders; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param ValidatorClassMetaData $metadata |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
|
25
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
|
26
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function getPropertyValidatorMetaForOrders(ValidatorClassMetaData $metadata): void |
|
29
|
|
|
{ |
|
30
|
|
|
$metadata->addPropertyConstraint( |
|
31
|
|
|
HasOrdersInterface::PROPERTY_NAME_ORDERS, |
|
32
|
|
|
new Valid() |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param ClassMetadataBuilder $manyToManyBuilder |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
abstract public static function getPropertyDoctrineMetaForOrders( |
|
42
|
|
|
ClassMetadataBuilder $manyToManyBuilder |
|
43
|
|
|
): void; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return Collection|Order[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getOrders(): Collection |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->orders; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param Collection|Order[] $orders |
|
55
|
|
|
* |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setOrders(Collection $orders): HasOrdersInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$this->orders = $orders; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Order|null $order |
|
67
|
|
|
* @param bool $recip |
|
68
|
|
|
* |
|
69
|
|
|
* @return self |
|
70
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
71
|
|
|
*/ |
|
72
|
|
|
public function addOrder( |
|
73
|
|
|
?Order $order, |
|
74
|
|
|
bool $recip = true |
|
75
|
|
|
): HasOrdersInterface { |
|
76
|
|
|
if ($order === null) { |
|
77
|
|
|
return $this; |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!$this->orders->contains($order)) { |
|
81
|
|
|
$this->orders->add($order); |
|
82
|
|
|
if ($this instanceof ReciprocatesOrderInterface && true === $recip) { |
|
83
|
|
|
$this->reciprocateRelationOnOrder($order); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param Order $order |
|
92
|
|
|
* @param bool $recip |
|
93
|
|
|
* |
|
94
|
|
|
* @return self |
|
95
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
96
|
|
|
*/ |
|
97
|
|
|
public function removeOrder( |
|
98
|
|
|
Order $order, |
|
99
|
|
|
bool $recip = true |
|
100
|
|
|
): HasOrdersInterface { |
|
101
|
|
|
$this->orders->removeElement($order); |
|
102
|
|
|
if ($this instanceof ReciprocatesOrderInterface && true === $recip) { |
|
103
|
|
|
$this->removeRelationOnOrder($order); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Initialise the orders property as a Doctrine ArrayCollection |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
|
114
|
|
|
*/ |
|
115
|
|
|
private function initOrders() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->orders = new ArrayCollection(); |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.