-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Tag.php
154 lines (139 loc) · 3.45 KB
/
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Neos\Media\Domain\Model;
/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Neos\Flow\Annotations as Flow;
/**
* A Tag, to organize Assets
*
* @Flow\Entity
*/
class Tag
{
/**
* @var string
* @Flow\Validate(type="StringLength", options={ "maximum"=255 })
* @Flow\Validate(type="NotEmpty")
*/
protected $label;
/**
* @var Collection<\Neos\Media\Domain\Model\AssetCollection>
* @ORM\ManyToMany(mappedBy="tags", cascade={"persist"})
* @ORM\OrderBy({"title"="ASC"})
* @Flow\Lazy
*/
protected $assetCollections;
/**
* @var Tag
* @ORM\ManyToOne(inversedBy="children", cascade={"persist"})
* @ORM\JoinColumn(onDelete="SET NULL")
* @Flow\Lazy
*/
protected $parent;
/**
* @var Collection<\Neos\Media\Domain\Model\Tag>
* @ORM\OneToMany(mappedBy="parent", orphanRemoval=true)
* @Flow\Lazy
*/
protected $children;
/**
* @param string $label
*/
public function __construct($label)
{
$this->label = $label;
$this->assetCollections = new ArrayCollection();
$this->children = new ArrayCollection();
}
/**
* Sets the label of this tag
*
* @param string $label
* @return void
*/
public function setLabel(string $label): void
{
$this->label = $label;
}
/**
* The label of this tag
*
* @return string
*/
public function getLabel(): string
{
return $this->label;
}
/**
* Return the asset collections this tag is included in
*
* @return Collection
*/
public function getAssetCollections(): Collection
{
return $this->assetCollections;
}
/**
* Set the asset collections that include this tag
*
* @param Collection $assetCollections
* @return void
*/
public function setAssetCollections(Collection $assetCollections): void
{
foreach ($this->assetCollections as $existingAssetCollection) {
$existingAssetCollection->removeTag($this);
}
foreach ($assetCollections as $newAssetCollection) {
$newAssetCollection->addTag($this);
}
foreach ($this->assetCollections as $assetCollection) {
if (!$assetCollections->contains($assetCollection)) {
$assetCollections->add($assetCollection);
}
}
$this->assetCollections = $assetCollections;
}
/**
* @return Tag|null
*/
public function getParent(): ?Tag
{
return $this->parent;
}
/**
* @param Tag $parent
*/
public function setParent(Tag $parent): void
{
$this->parent = $parent;
$parent->addChild($this);
}
/**
* @param Tag $child
*/
public function addChild(Tag $child): void
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
}
/**
* @return Collection
*/
public function getChildren(): Collection
{
return $this->children;
}
}