1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
// phpcs:disable |
3
|
|
|
namespace TemplateNamespace\Entity\Relations\TemplateEntity\Traits; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\DoctrineStaticMeta; |
9
|
|
|
use Symfony\Component\Validator\Constraints\Count; |
10
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
11
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
12
|
|
|
use Symfony\Component\Validator\Exception\InvalidOptionsException; |
13
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException; |
14
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
15
|
|
|
use TemplateNamespace\Entity\Interfaces\TemplateEntityInterface; |
16
|
|
|
use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\HasRequiredTemplateEntitiesInterface; |
17
|
|
|
use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\HasRequiredTemplateEntityInterface; |
18
|
|
|
use TemplateNamespace\Entity\Relations\TemplateEntity\Interfaces\ReciprocatesTemplateEntityInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait HasTemplateEntitiesAbstract |
22
|
|
|
* |
23
|
|
|
* The base trait for relations to multiple TemplateEntities |
24
|
|
|
* |
25
|
|
|
* @package TemplateNamespace\Entity\Relations\TemplateEntity\Traits |
26
|
|
|
*/ |
27
|
|
|
// phpcs:enable |
28
|
|
|
trait HasRequiredTemplateEntitiesAbstract |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var ArrayCollection|TemplateEntityInterface[] |
32
|
|
|
*/ |
33
|
|
|
private $templateEntities; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ValidatorClassMetaData $metadata |
37
|
|
|
* |
38
|
|
|
* @throws MissingOptionsException |
39
|
|
|
* @throws InvalidOptionsException |
40
|
|
|
* @throws ConstraintDefinitionException |
41
|
|
|
*/ |
42
|
|
|
public static function validatorMetaForPropertyTemplateEntities( |
43
|
|
|
ValidatorClassMetaData $metadata |
44
|
|
|
): void { |
45
|
|
|
$metadata->addPropertyConstraint( |
46
|
|
|
HasRequiredTemplateEntitiesInterface::PROPERTY_NAME_TEMPLATE_ENTITIES, |
47
|
|
|
new Count(['min' => 1]) |
48
|
|
|
); |
49
|
|
|
$metadata->addPropertyConstraint( |
50
|
|
|
HasRequiredTemplateEntitiesInterface::PROPERTY_NAME_TEMPLATE_ENTITIES, |
51
|
|
|
new NotBlank() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ClassMetadataBuilder $manyToManyBuilder |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
abstract public static function metaForTemplateEntities( |
61
|
|
|
ClassMetadataBuilder $manyToManyBuilder |
62
|
|
|
): void; |
63
|
|
|
|
64
|
|
|
private static function dsmInitRequiredRelationForTemplateEntity(DoctrineStaticMeta $dsm): void |
65
|
|
|
{ |
66
|
|
|
$dsm->setRequiredRelationProperty( |
67
|
|
|
new DoctrineStaticMeta\RequiredRelation( |
68
|
|
|
HasRequiredTemplateEntitiesInterface::PROPERTY_NAME_TEMPLATE_ENTITIES, |
69
|
|
|
TemplateEntityInterface::class, |
70
|
|
|
true |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Collection|TemplateEntityInterface[] |
77
|
|
|
*/ |
78
|
|
|
public function getTemplateEntities(): Collection |
79
|
|
|
{ |
80
|
|
|
$return = new ArrayCollection(); |
81
|
|
|
foreach ($this->templateEntities as $entity) { |
82
|
|
|
$return->add($entity); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Collection|TemplateEntityInterface[] $templateEntities |
90
|
|
|
* |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setTemplateEntities( |
94
|
|
|
Collection $templateEntities |
95
|
|
|
): self { |
96
|
|
|
foreach ($this->templateEntities as $templateEntity) { |
97
|
|
|
$this->removeTemplateEntity($templateEntity); |
98
|
|
|
} |
99
|
|
|
foreach ($templateEntities as $newTemplateEntity) { |
100
|
|
|
$this->addTemplateEntity($newTemplateEntity); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param TemplateEntityInterface $templateEntity |
108
|
|
|
* @param bool $recip |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
112
|
|
|
*/ |
113
|
|
|
public function removeTemplateEntity( |
114
|
|
|
TemplateEntityInterface $templateEntity, |
115
|
|
|
bool $recip = true |
116
|
|
|
): self { |
117
|
|
|
$this->removeFromEntityCollectionAndNotify('templateEntities', $templateEntity); |
|
|
|
|
118
|
|
|
if ($this instanceof ReciprocatesTemplateEntityInterface && true === $recip) { |
119
|
|
|
$this->removeRelationOnTemplateEntity( |
120
|
|
|
$templateEntity |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param TemplateEntityInterface $templateEntity |
129
|
|
|
* @param bool $recip |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
133
|
|
|
*/ |
134
|
|
|
public function addTemplateEntity( |
135
|
|
|
TemplateEntityInterface $templateEntity, |
136
|
|
|
bool $recip = true |
137
|
|
|
): self { |
138
|
|
|
$this->addToEntityCollectionAndNotify('templateEntities', $templateEntity); |
|
|
|
|
139
|
|
|
if ($this instanceof ReciprocatesTemplateEntityInterface && true === $recip) { |
140
|
|
|
$this->reciprocateRelationOnTemplateEntity( |
141
|
|
|
$templateEntity |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Initialise the templateEntities property as a Doctrine ArrayCollection |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) |
153
|
|
|
*/ |
154
|
|
|
private function initTemplateEntities(): self |
155
|
|
|
{ |
156
|
|
|
if (null !== $this->templateEntities) { |
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
$this->templateEntities = new ArrayCollection(); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|