From 477f9754256c692df09130fa8a56ae486bdc4b2f Mon Sep 17 00:00:00 2001 From: Julien LIBERT Date: Wed, 28 Aug 2024 11:39:12 +0200 Subject: [PATCH] Add detach association --- src/Api/Association.php | 13 ++++++++++++- src/Api/Builder.php | 13 ++++++++++++- src/Api/Model.php | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Api/Association.php b/src/Api/Association.php index 8c087ee..493d6ed 100644 --- a/src/Api/Association.php +++ b/src/Api/Association.php @@ -51,6 +51,17 @@ public function attach($targetId) ); } + public function detach($targetId) + { + if ($targetId instanceof Model) { + $targetId = $targetId->id; + } + + $this->sourceBuilder()->deleteAssociation( + $this->target, $targetId + ); + } + public function sourceBuilder(): Builder { return $this->source->builder(); @@ -65,4 +76,4 @@ public function __call($method, $parameters) { return $this->forwardCallTo($this->builder(), $method, $parameters); } -} \ No newline at end of file +} diff --git a/src/Api/Builder.php b/src/Api/Builder.php index c359d7c..de85fa1 100644 --- a/src/Api/Builder.php +++ b/src/Api/Builder.php @@ -340,6 +340,17 @@ public function associate(Model $target, $targetId) )->json(); } + public function deleteAssociation(Model $target, $targetId) + { + return $this->client()->delete( + $this->object->endpoint('associate', [ + 'association' => $target->type(), + 'associationId' => $targetId, + 'associationType' => Str::singular($this->object->type()) . "_to_" . Str::singular($target->type()) + ]) + )->json(); + } + public function client(): Client { return $this->client; @@ -408,4 +419,4 @@ public function __call($method, $parameters) 'Call to undefined method %s::%s()', static::class, $method )); } -} \ No newline at end of file +} diff --git a/src/Api/Model.php b/src/Api/Model.php index da4e50d..846a2fa 100644 --- a/src/Api/Model.php +++ b/src/Api/Model.php @@ -93,7 +93,7 @@ public function fill(array $properties): static private function isAllowedProperty(string $key): bool { - return $key === 'email' || + return $key === 'email' || !(HubSpot::isType($key) || HubSpot::isType(Str::plural($key))); }