From e57d08cf371d7166507d6d5ff4f5a10d0baffd5e Mon Sep 17 00:00:00 2001 From: Nathan Bomshteyn Date: Wed, 25 Aug 2021 10:14:15 -0400 Subject: [PATCH] Add optional domain and group_guid params (#43) thanks to @bomshteyn --- src/Client/BitlyClient.php | 16 ++++++++++++---- src/Testing/BitlyClientFake.php | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Client/BitlyClient.php b/src/Client/BitlyClient.php index 9aff7f4..7dcef9a 100644 --- a/src/Client/BitlyClient.php +++ b/src/Client/BitlyClient.php @@ -41,13 +41,15 @@ public function __construct(ClientInterface $client, $token) /** * @param string $url raw URL. + * @param string|null $domain + * @param string|null $group_guid * + * @return string shorten URL. + * @throws \GuzzleHttp\Exception\GuzzleException * @throws \Shivella\Bitly\Exceptions\AccessDeniedException * @throws \Shivella\Bitly\Exceptions\InvalidResponseException - * - * @return string shorten URL. */ - public function getUrl(string $url): string + public function getUrl(string $url, ?string $domain = null, ?string $group_guid = null): string { $requestUrl = 'https://api-ssl.bitly.com/v4/shorten'; @@ -56,8 +58,14 @@ public function getUrl(string $url): string 'Content-Type' => 'application/json', ]; + $data = array_filter([ + 'long_url' => $url, + 'domain' => $domain, + 'group_guid' => $group_guid, + ]); + try { - $request = new Request('POST', $requestUrl, $header, json_encode(['long_url' => $url])); + $request = new Request('POST', $requestUrl, $header, json_encode($data)); $response = $this->client->send($request); } catch (RequestException $e) { diff --git a/src/Testing/BitlyClientFake.php b/src/Testing/BitlyClientFake.php index 2a3251f..bd5bd16 100644 --- a/src/Testing/BitlyClientFake.php +++ b/src/Testing/BitlyClientFake.php @@ -28,7 +28,7 @@ public function __construct() * @param string $url raw URL. * @return string shorten URL. */ - public function getUrl(string $url): string + public function getUrl(string $url, ?string $domain = NULL, ?string $group_guid = NULL): string { return 'http://bit.ly/'.substr(sha1($url), 0, 6); }