Skip to content

Commit

Permalink
feature: add existing and incoming nodes to exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein committed Aug 4, 2023
1 parent 4092287 commit 0dd368c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Framework/FieldsAPI/Concerns/NameCollision.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public function checkNameCollisionDeep(Node $node)
}

/**
* @unreleased add existing and incoming nodes to exception
* @since 2.10.2
*
* @throws NameCollisionException
*/
public function checkNameCollision(Node $node)
{
if ($this->getNodeByName($node->getName())) {
throw new NameCollisionException($node->getName());
if ($existingNode = $this->getNodeByName($node->getName())) {
throw new NameCollisionException($node->getName(), $existingNode, $node);
}
}
}
36 changes: 35 additions & 1 deletion src/Framework/FieldsAPI/Exceptions/NameCollisionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Give\Framework\FieldsAPI\Exceptions;

use Give\Framework\Exceptions\Primitives\Exception;
use Give\Framework\FieldsAPI\Contracts\Node;

/**
* @unreleased add existing and incoming nodes to exception
* @since 2.10.2
*/
class NameCollisionException extends Exception
Expand All @@ -13,10 +15,26 @@ class NameCollisionException extends Exception
* @var string
*/
protected $nodeNameCollision;
/**
* @var Node
*/
protected $existingNode;
/**
* @var Node
*/
protected $incomingNode;

public function __construct($name, $code = 0, Exception $previous = null)
public function __construct(
string $name,
Node $existingNode,
Node $incomingNode,
int $code = 0,
Exception $previous = null
)
{
$this->nodeNameCollision = $name;
$this->existingNode = $existingNode;
$this->incomingNode = $incomingNode;

$message = "Node name collision for $name";
parent::__construct($message, $code, $previous);
Expand All @@ -29,4 +47,20 @@ public function getNodeNameCollision(): string
{
return $this->nodeNameCollision;
}

/**
* @unreleased
*/
public function getIncomingNode(): Node
{
return $this->incomingNode;
}

/**
* @unreleased
*/
public function getExistingNode(): Node
{
return $this->existingNode;
}
}

0 comments on commit 0dd368c

Please sign in to comment.