Skip to content

Commit fe76f7c

Browse files
author
bryanadamloh97
committed
Added two new drivers
1 parent ea9f500 commit fe76f7c

File tree

7 files changed

+128
-2
lines changed

7 files changed

+128
-2
lines changed

config/url-shortener.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,27 @@
44
'default' => env('URL_SHORTENER_DRIVER', 'bit_ly'),
55

66
'shorteners' => [
7+
78
'bit_ly' => [
89
'driver' => 'bit_ly',
910
'domain' => env('URL_SHORTENER_PREFIX', 'bit.ly'),
1011
'token' => env('URL_SHORTENER_API_TOKEN')
1112
],
13+
1214
'tiny_url' => [
1315
'driver' => 'tiny_url',
1416
'domain' => env('URL_SHORTENER_PREFIX', 'tinyurl.com'),
1517
'token' => env('URL_SHORTENER_API_TOKEN')
18+
],
19+
20+
'shorte_st' => [
21+
'driver' => 'shorte_st',
22+
'token' => env('URL_SHORTENER_API_TOKEN')
23+
],
24+
25+
'is_gd' => [
26+
'driver' => 'is_gd',
27+
'statistic' => env('URL_SHORTENER_ANALYTICS', false)
1628
]
1729
]
1830
];

src/Drivers/BitLyDriverShortener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(ClientInterface $client, $token, $domain)
3434
*/
3535
public function shortenAsync($url, array $options = [])
3636
{
37-
$options = array_merge_recursive(Arr::add($this->object, 'json.long_url', $url), $options);
37+
$options = array_merge_recursive(Arr::add($this->object, 'json.long_url', $url), ['json' => $options]);
3838
$request = new Request('POST', '/v4/shorten');
3939

4040
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use GuzzleHttp\Psr7\Request;
7+
use Illuminate\Support\Arr;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class IsGdDriverShortener extends DriverShortener
11+
{
12+
protected $client;
13+
protected $object;
14+
15+
public function __construct(ClientInterface $client, $statistic)
16+
{
17+
$this->client = $client;
18+
$this->object = [
19+
'allow_redirects' => false,
20+
'base_uri' => 'https://is.gd',
21+
'query' => [
22+
'format' => 'json',
23+
'logstats' => intval($statistic)
24+
]
25+
];
26+
}
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
public function shortenAsync($url, array $options = [])
32+
{
33+
$options = array_merge_recursive(Arr::add($this->object, 'query.url', $url), ['query' => $options]);
34+
$request = new Request('GET', '/create.php');
35+
36+
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
37+
return str_replace('http://', 'https://', json_decode($response->getBody()->getContents())->shorturl);
38+
});
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace CodeOfDigital\LaravelUrlShortener\Drivers;
4+
5+
use GuzzleHttp\ClientInterface;
6+
use GuzzleHttp\Psr7\Request;
7+
use Illuminate\Support\Arr;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class ShorteStDriverShortener extends DriverShortener
11+
{
12+
protected $client;
13+
protected $object;
14+
15+
public function __construct(ClientInterface $client, $token)
16+
{
17+
$this->client = $client;
18+
$this->object = [
19+
'allow_redirects' => false,
20+
'verify' => false,
21+
'base_uri' => 'https://api.shorte.st',
22+
'headers' => [
23+
'Accept' => 'application/json',
24+
'Public-API-Token' => $token,
25+
'Content-Type' => 'application/json'
26+
],
27+
'json' => []
28+
];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function shortenAsync($url, array $options = [])
35+
{
36+
$options = array_merge_recursive(Arr::add($this->object, 'json.urlToShorten', $url), ['json' => $options]);
37+
$request = new Request('PUT', '/v1/data/url');
38+
39+
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {
40+
return json_decode($response->getBody()->getContents())->shortenedUrl;
41+
});
42+
}
43+
}

src/Drivers/TinyUrlDriverShortener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(ClientInterface $client, $token, $domain)
3434
*/
3535
public function shortenAsync($url, array $options = [])
3636
{
37-
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), $options);
37+
$options = array_merge_recursive(Arr::add($this->object, 'json.url', $url), ['json' => $options]);
3838
$request = new Request('POST', '/create');
3939

4040
return $this->client->sendAsync($request, $options)->then(function (ResponseInterface $response) {

src/Facades/UrlShortener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Facade;
77

88
/**
9+
* @method static mixed driver($name = null)
910
* @method static string shorten($url, array $options = [])
1011
* @method static PromiseInterface shortenAsync($url, array $options = [])
1112
*

src/UrlShortener.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use CodeOfDigital\LaravelUrlShortener\Contracts\UrlFactory;
66
use CodeOfDigital\LaravelUrlShortener\Drivers\BitLyDriverShortener;
7+
use CodeOfDigital\LaravelUrlShortener\Drivers\IsGdDriverShortener;
8+
use CodeOfDigital\LaravelUrlShortener\Drivers\ShorteStDriverShortener;
79
use CodeOfDigital\LaravelUrlShortener\Drivers\TinyUrlDriverShortener;
810
use GuzzleHttp\ClientInterface;
911
use GuzzleHttp\Promise\PromiseInterface;
@@ -74,6 +76,34 @@ protected function createTinyUrlDriver(array $config)
7476
);
7577
}
7678

79+
/**
80+
* Create an instance of Shorte.st driver
81+
*
82+
* @param array $config
83+
* @return ShorteStDriverShortener
84+
*/
85+
protected function createShorteStDriver(array $config)
86+
{
87+
return new ShorteStDriverShortener(
88+
$this->app->make(ClientInterface::class),
89+
Arr::get($config, 'token')
90+
);
91+
}
92+
93+
/**
94+
* Create an instance of Is.gd driver
95+
*
96+
* @param array $config
97+
* @return IsGdDriverShortener
98+
*/
99+
protected function createIsGdDriver(array $config)
100+
{
101+
return new IsGdDriverShortener(
102+
$this->app->make(ClientInterface::class),
103+
Arr::get($config, 'statistic')
104+
);
105+
}
106+
77107
/**
78108
* Get the default URL shortener driver
79109
*

0 commit comments

Comments
 (0)