From 0c3d477946a83cd1413259d7f9b332fa24f4a344 Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Fri, 4 Aug 2023 15:23:11 -0400 Subject: [PATCH] Feature: add `$nodeNameCollision` property on `NameCollisionException` (#6864) * feature: add name collision property * feature: add existing and incoming nodes to exception --------- Co-authored-by: Jon Waldstein --- .../FieldsAPI/Concerns/NameCollision.php | 5 +- .../Exceptions/NameCollisionException.php | 51 ++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Framework/FieldsAPI/Concerns/NameCollision.php b/src/Framework/FieldsAPI/Concerns/NameCollision.php index dccb7bf66d..2fba1d7caa 100644 --- a/src/Framework/FieldsAPI/Concerns/NameCollision.php +++ b/src/Framework/FieldsAPI/Concerns/NameCollision.php @@ -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); } } } diff --git a/src/Framework/FieldsAPI/Exceptions/NameCollisionException.php b/src/Framework/FieldsAPI/Exceptions/NameCollisionException.php index 54d5645518..579b60608d 100644 --- a/src/Framework/FieldsAPI/Exceptions/NameCollisionException.php +++ b/src/Framework/FieldsAPI/Exceptions/NameCollisionException.php @@ -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; + } }