From a8237d67ccc7edd5db7c2867d1cb8f9533016c8b Mon Sep 17 00:00:00 2001 From: Alfonso Bribiesca Date: Wed, 31 Jul 2024 09:41:14 -0600 Subject: [PATCH] add a method to set host --- src/ArkClient.php | 17 +++++++++++++++++ tests/ArkClientTest.php | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/ArkClient.php b/src/ArkClient.php index a792c08..a168517 100644 --- a/src/ArkClient.php +++ b/src/ArkClient.php @@ -101,6 +101,23 @@ public function __call($name, $args) } } + /** + * Set the host for the given type. + * + * @param string $host + * @param string $type + * + * @throws InvalidArgumentException if the type is not 'api', 'transactions', or 'evm' + */ + public function setHost(string $host, string $type): void + { + if (! in_array($type, ['api', 'transactions', 'evm'], true)) { + throw new \InvalidArgumentException('Invalid host type.'); + } + + $this->hosts[$type] = $host; + } + /** * @return array{ * api: string, diff --git a/tests/ArkClientTest.php b/tests/ArkClientTest.php index be25da1..1dc5c13 100644 --- a/tests/ArkClientTest.php +++ b/tests/ArkClientTest.php @@ -101,6 +101,27 @@ public function should_accept_custom_handler() $this->assertSame($handler, $connection->getHttpClient()->getConfig('handler')); } + /** @test */ + public function should_set_host() + { + $client = $this->getClient(); + + $newHost = 'https://new-host.com/api'; + $client->setHost($newHost, 'api'); + + $this->assertSame($newHost, $client->getHosts()['api']); + } + + /** @test */ + public function should_throw_exception_if_host_type_is_invalid() + { + $client = $this->getClient(); + + $this->expectException(\InvalidArgumentException::class); + + $client->setHost('https://new-host.com/api', 'other'); + } + /** * Get a new client instance. *