1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace TemplateNamespace\Entity\Relations\TemplateEntity\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\UsesPHPMetaDataInterface; |
10
|
|
|
use Symfony\Component\Validator\Constraints\Valid; |
11
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
12
|
|
|
use TemplateNamespace\Entities\TemplateEntity as TemplateEntity; |
13
|
|
|
use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\HasTemplateEntitiesInterface; |
14
|
|
|
use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\ReciprocatesTemplateEntityInterface; |
15
|
|
|
|
16
|
|
|
trait HasTemplateEntitiesAbstract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ArrayCollection|TemplateEntity[] |
20
|
|
|
*/ |
21
|
|
|
private $templateEntities; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ValidatorClassMetaData $metadata |
25
|
|
|
* |
26
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
27
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
28
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
29
|
|
|
*/ |
30
|
|
|
public static function getPropertyValidatorMetaForTemplateEntities(ValidatorClassMetaData $metadata): void |
31
|
|
|
{ |
32
|
|
|
$metadata->addPropertyConstraint( |
33
|
|
|
HasTemplateEntitiesInterface::PROPERTY_NAME_TEMPLATE_ENTITIES, |
34
|
|
|
new Valid() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ClassMetadataBuilder $manyToManyBuilder |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
abstract public static function getPropertyDoctrineMetaForTemplateEntities( |
44
|
|
|
ClassMetadataBuilder $manyToManyBuilder |
45
|
|
|
): void; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Collection|TemplateEntity[] |
49
|
|
|
*/ |
50
|
|
|
public function getTemplateEntities(): Collection |
51
|
|
|
{ |
52
|
|
|
return $this->templateEntities; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Collection|TemplateEntity[] $templateEntities |
57
|
|
|
* |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
|
|
public function setTemplateEntities(Collection $templateEntities): self |
61
|
|
|
{ |
62
|
|
|
$this->templateEntities = $templateEntities; |
|
|
|
|
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param TemplateEntity|null $templateEntity |
69
|
|
|
* @param bool $recip |
70
|
|
|
* |
71
|
|
|
* @return self |
72
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
73
|
|
|
*/ |
74
|
|
|
public function addTemplateEntity( |
75
|
|
|
?TemplateEntity $templateEntity, |
76
|
|
|
bool $recip = true |
77
|
|
|
): self { |
78
|
|
|
if($templateEntity === null) { |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (!$this->templateEntities->contains($templateEntity)) { |
83
|
|
|
$this->templateEntities->add($templateEntity); |
84
|
|
|
if ($this instanceof ReciprocatesTemplateEntityInterface && true === $recip) { |
85
|
|
|
$this->reciprocateRelationOnTemplateEntity($templateEntity); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param TemplateEntity $templateEntity |
94
|
|
|
* @param bool $recip |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
98
|
|
|
*/ |
99
|
|
|
public function removeTemplateEntity( |
100
|
|
|
TemplateEntity $templateEntity, |
101
|
|
|
bool $recip = true |
102
|
|
|
): self { |
103
|
|
|
$this->templateEntities->removeElement($templateEntity); |
104
|
|
|
if ($this instanceof ReciprocatesTemplateEntityInterface && true === $recip) { |
105
|
|
|
$this->removeRelationOnTemplateEntity($templateEntity); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Initialise the templateEntities property as a Doctrine ArrayCollection |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
116
|
|
|
*/ |
117
|
|
|
private function initTemplateEntities() |
118
|
|
|
{ |
119
|
|
|
$this->templateEntities = new ArrayCollection(); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.