diff --git a/lib/Models/src/Roadiz/Core/AbstractEntities/LeafInterface.php b/lib/Models/src/Roadiz/Core/AbstractEntities/LeafInterface.php index 33424664..898a0ade 100644 --- a/lib/Models/src/Roadiz/Core/AbstractEntities/LeafInterface.php +++ b/lib/Models/src/Roadiz/Core/AbstractEntities/LeafInterface.php @@ -36,7 +36,7 @@ public function getParent(): ?LeafInterface; public function getParents(): array; /** - * @param static|null $parent + * @param LeafInterface|null $parent * @return $this */ public function setParent(?LeafInterface $parent = null): static; diff --git a/lib/Models/src/Roadiz/Core/AbstractEntities/LeafTrait.php b/lib/Models/src/Roadiz/Core/AbstractEntities/LeafTrait.php index 6d14c7d7..6c3119db 100644 --- a/lib/Models/src/Roadiz/Core/AbstractEntities/LeafTrait.php +++ b/lib/Models/src/Roadiz/Core/AbstractEntities/LeafTrait.php @@ -64,22 +64,6 @@ public function getParent(): ?LeafInterface return $this->parent; } - /** - * @param LeafInterface|null $parent - * @return $this - */ - public function setParent(?LeafInterface $parent = null): static - { - if ($parent === $this) { - throw new \InvalidArgumentException('An entity cannot have itself as a parent.'); - } - - $this->parent = $parent; - $this->parent?->addChild($this); - - return $this; - } - /** * @return LeafInterface[] */ diff --git a/lib/RoadizCoreBundle/src/Entity/Folder.php b/lib/RoadizCoreBundle/src/Entity/Folder.php index 8a68596e..549c6e65 100644 --- a/lib/RoadizCoreBundle/src/Entity/Folder.php +++ b/lib/RoadizCoreBundle/src/Entity/Folder.php @@ -339,11 +339,27 @@ public function getFullPath(): string $path = []; foreach ($parents as $parent) { - $path[] = $parent->getFolderName(); + if ($parent instanceof FolderInterface) { + $path[] = $parent->getFolderName(); + } } $path[] = $this->getFolderName(); return implode('/', $path); } + + public function setParent(?LeafInterface $parent = null): static + { + if ($parent === $this) { + throw new \InvalidArgumentException('An entity cannot have itself as a parent.'); + } + if (null !== $parent && !$parent instanceof Folder) { + throw new \InvalidArgumentException('A folder can only have a folder as a parent.'); + } + $this->parent = $parent; + $this->parent?->addChild($this); + + return $this; + } } diff --git a/lib/RoadizCoreBundle/src/Entity/Node.php b/lib/RoadizCoreBundle/src/Entity/Node.php index 43f929d1..02875828 100644 --- a/lib/RoadizCoreBundle/src/Entity/Node.php +++ b/lib/RoadizCoreBundle/src/Entity/Node.php @@ -993,6 +993,20 @@ public function __clone() } } + public function setParent(?LeafInterface $parent = null): static + { + if ($parent === $this) { + throw new \InvalidArgumentException('An entity cannot have itself as a parent.'); + } + if (null !== $parent && !($parent instanceof Node)) { + throw new \InvalidArgumentException('A node can only have a Node as a parent.'); + } + $this->parent = $parent; + $this->parent?->addChild($this); + + return $this; + } + /** * @return string */ diff --git a/lib/RoadizCoreBundle/src/Entity/Tag.php b/lib/RoadizCoreBundle/src/Entity/Tag.php index 07aad1b4..6e45b792 100644 --- a/lib/RoadizCoreBundle/src/Entity/Tag.php +++ b/lib/RoadizCoreBundle/src/Entity/Tag.php @@ -231,7 +231,9 @@ public function getFullPath(): string $path = []; foreach ($parents as $parent) { - $path[] = $parent->getTagName(); + if ($parent instanceof Tag) { + $path[] = $parent->getTagName(); + } } $path[] = $this->getTagName(); @@ -446,4 +448,18 @@ public function getDocuments(): array $this->getTranslatedTags()->first()->getDocuments() : []; } + + public function setParent(?LeafInterface $parent = null): static + { + if ($parent === $this) { + throw new \InvalidArgumentException('An entity cannot have itself as a parent.'); + } + if (null !== $parent && !$parent instanceof Tag) { + throw new \InvalidArgumentException('A tag can only have a Tag entity as a parent'); + } + $this->parent = $parent; + $this->parent?->addChild($this); + + return $this; + } }