Skip to content

Commit

Permalink
Feature: add $nodeNameCollision property on NameCollisionException (
Browse files Browse the repository at this point in the history
#6864)

* feature: add name collision property

* feature: add existing and incoming nodes to exception

---------

Co-authored-by: Jon Waldstein <[email protected]>
  • Loading branch information
jonwaldstein and Jon Waldstein authored Aug 4, 2023
1 parent b9db161 commit 0c3d477
Show file tree
Hide file tree
Showing 2 changed files with 53 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);
}
}
}
51 changes: 50 additions & 1 deletion src/Framework/FieldsAPI/Exceptions/NameCollisionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,64 @@
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
{
public function __construct($name, $code = 0, Exception $previous = null)
/**
* @var string
*/
protected $nodeNameCollision;
/**
* @var Node
*/
protected $existingNode;
/**
* @var Node
*/
protected $incomingNode;

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);
}

/**
* @unreleased
*/
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 0c3d477

Please sign in to comment.