1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Silverback\ApiComponentBundle\Entity\Component\AbstractComponent; |
8
|
|
|
use Silverback\ApiComponentBundle\Validator\Constraints as ACBAssert; |
9
|
|
|
|
10
|
|
|
trait ValidComponentTrait |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @ORM\Column(type="array") |
14
|
|
|
* @ACBAssert\ComponentTypeClasses() |
15
|
|
|
* @var ArrayCollection|AbstractComponent[] |
16
|
|
|
*/ |
17
|
|
|
protected $validComponents; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return ArrayCollection |
21
|
|
|
*/ |
22
|
5 |
|
public function getValidComponents(): ArrayCollection |
23
|
|
|
{ |
24
|
5 |
|
return $this->validComponents; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $component |
29
|
|
|
* @return ValidComponentTrait |
30
|
|
|
*/ |
31
|
5 |
|
public function addValidComponent(string $component): self |
32
|
|
|
{ |
33
|
5 |
|
if (!$this->validComponents->contains($component)) { |
34
|
5 |
|
$this->validComponents->add($component); |
35
|
|
|
} |
36
|
5 |
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $component |
41
|
|
|
* @return ValidComponentTrait |
42
|
|
|
*/ |
43
|
|
|
public function removeValidComponent(string $component): self |
44
|
|
|
{ |
45
|
|
|
$this->validComponents->removeElement($component); |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param ValidComponentInterface $entity |
51
|
|
|
* @param bool $force |
52
|
|
|
*/ |
53
|
5 |
|
protected function cascadeValidComponents(ValidComponentInterface $entity, bool $force = false): void |
54
|
|
|
{ |
55
|
5 |
|
if ($force) { |
56
|
|
|
// Set to true so force the cascade whether empty or not |
57
|
|
|
$this->validComponents = $entity->getValidComponents(); |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
// Default behaviour - cascade if set on parent |
61
|
5 |
|
$parentValidComponents = $entity->getValidComponents(); |
62
|
5 |
|
if ($parentValidComponents->count()) { |
63
|
5 |
|
$this->validComponents = $entity->getValidComponents(); |
64
|
|
|
} |
65
|
5 |
|
} |
66
|
|
|
} |
67
|
|
|
|