From df8f19a7b8e7ffae3ee97ecbfb4a771edddc4d14 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 31 Jan 2024 13:42:45 +0000 Subject: [PATCH] example: get individual tag --- example/02-get-customer-by-email.php | 2 +- example/03-get-list-of-tags.php | 1 + example/04-assign-tag-to-customer.php | 56 +++++++++++++++++++++++++++ src/Client.php | 10 +++++ src/TagNotFoundException.php | 4 ++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 example/04-assign-tag-to-customer.php create mode 100644 src/TagNotFoundException.php diff --git a/example/02-get-customer-by-email.php b/example/02-get-customer-by-email.php index eb1422d..ad60bdd 100644 --- a/example/02-get-customer-by-email.php +++ b/example/02-get-customer-by-email.php @@ -1,6 +1,6 @@ name; + echo " ($tag->id)"; echo "\n"; } diff --git a/example/04-assign-tag-to-customer.php b/example/04-assign-tag-to-customer.php new file mode 100644 index 0000000..aec3fd0 --- /dev/null +++ b/example/04-assign-tag-to-customer.php @@ -0,0 +1,56 @@ +getTag(id: $tagId); +} +catch(TagNotFoundException) { + fwrite(STDERR, "No tag found with ID $tagId\n"); + exit(3); +} + +$customer = null; +try { + $customer = $client->getCustomer(id: $customerId); +} +catch(CustomerNotFoundException) { + fwrite(STDERR, "No customer found with ID $customerId\n"); + exit(4); +} + +$client->addTagToCustomer($tag, $customer); diff --git a/src/Client.php b/src/Client.php index 637f219..dfb2f0b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -51,6 +51,16 @@ public function getCustomer( throw new CustomerNotFoundException($email ?? $id); } + public function getTag(string $id):Tag { + foreach($this->getAllTags() as $tag) { + if($tag->id === $id) { + return $tag; + } + } + + throw new TagNotFoundException($id); + } + /** @return array */ public function getAllTags():array { $endpoint = Endpoint::getAllTags; diff --git a/src/TagNotFoundException.php b/src/TagNotFoundException.php new file mode 100644 index 0000000..7a7b3d0 --- /dev/null +++ b/src/TagNotFoundException.php @@ -0,0 +1,4 @@ +