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