Skip to content

Commit

Permalink
fix(LeafInterface): Do not test exact class when setting LeafInterfac…
Browse files Browse the repository at this point in the history
…e parent to allow doctrine proxies.
  • Loading branch information
ambroisemaupate committed Jun 1, 2023
1 parent 3b5996d commit 56ed76d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 0 additions & 16 deletions lib/Models/src/Roadiz/Core/AbstractEntities/LeafTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
*/
Expand Down
18 changes: 17 additions & 1 deletion lib/RoadizCoreBundle/src/Entity/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions lib/RoadizCoreBundle/src/Entity/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
18 changes: 17 additions & 1 deletion lib/RoadizCoreBundle/src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 56ed76d

Please sign in to comment.