1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\UrlShortener; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Uri; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use LaraCrafts\UrlShortener\Concerns\CustomDomains; |
8
|
|
|
use LaraCrafts\UrlShortener\Concerns\CustomSuffixes; |
9
|
|
|
use LaraCrafts\UrlShortener\Concerns\ToPromise; |
10
|
|
|
use LaraCrafts\UrlShortener\Concerns\ToString; |
11
|
|
|
use LaraCrafts\UrlShortener\Concerns\ToUri; |
12
|
|
|
use LaraCrafts\UrlShortener\Contracts\Client; |
13
|
|
|
use LaraCrafts\UrlShortener\Contracts\UnsupportedOperationException; |
14
|
|
|
use Psr\Http\Message\UriInterface; |
15
|
|
|
|
16
|
|
|
class Builder |
17
|
|
|
{ |
18
|
|
|
protected $client; |
19
|
|
|
protected $options; |
20
|
|
|
protected $uri; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a new shortening builder instance. |
24
|
|
|
* |
25
|
|
|
* @param \LaraCrafts\UrlShortener\Contracts\Client $client |
26
|
|
|
* @param \Psr\Http\Message\UriInterface|string $uri |
27
|
|
|
* @param array $options |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Client $client, $uri, array $options) |
31
|
|
|
{ |
32
|
|
|
$this->client = $client; |
33
|
|
|
$this->options = $options; |
34
|
|
|
$this->uri = $this->parseUri($uri); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the shortened URI asynchronously. |
39
|
|
|
* |
40
|
|
|
* @return \GuzzleHttp\Promise\PromiseInterface |
41
|
|
|
*/ |
42
|
|
|
public function async() |
43
|
|
|
{ |
44
|
|
|
$driver = $this->getClient()->driver(); |
45
|
|
|
$promise = null; |
|
|
|
|
46
|
|
|
|
47
|
|
|
if ($driver instanceof ToPromise) { |
48
|
|
|
$promise = $driver->toPromise($this->uri, $this->options); |
49
|
|
|
} else { |
50
|
|
|
throw new UnsupportedOperationException('Async URL shortening is not supported'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $promise->then(function ($uri) { |
54
|
|
|
return $this->parseUri($uri); |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the domain to shorten to. |
60
|
|
|
* |
61
|
|
|
* @param string $domain |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function domain(string $domain) |
65
|
|
|
{ |
66
|
|
|
$driver = $this->getClient()->driver(); |
67
|
|
|
|
68
|
|
|
if (!$driver instanceof CustomDomains) { |
69
|
|
|
throw new UnsupportedOperationException('Applying a custom domain is not supported'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$driver->withDomain($this, $domain); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the shortened URI. |
79
|
|
|
* |
80
|
|
|
* @return \Psr\Http\Message\UriInterface |
81
|
|
|
*/ |
82
|
|
|
public function get() |
83
|
|
|
{ |
84
|
|
|
$driver = $this->getClient()->driver(); |
85
|
|
|
|
86
|
|
|
if ($driver instanceof ToUri) { |
87
|
|
|
$uri = $driver->toUri($this->uri, $this->options); |
88
|
|
|
} elseif ($driver instanceof ToString) { |
89
|
|
|
$uri = $driver->toString($this->uri, $this->options); |
90
|
|
|
} elseif ($driver instanceof ToPromise) { |
91
|
|
|
$uri = $driver->toPromise($this->uri, $this->options)->wait(); |
92
|
|
|
} else { |
93
|
|
|
throw new UnsupportedOperationException('URL shortening is not supported'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->parseUri($uri); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the client. |
101
|
|
|
* |
102
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Client |
103
|
|
|
*/ |
104
|
|
|
public function getClient() |
105
|
|
|
{ |
106
|
|
|
return $this->client; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the options. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getOptions() |
115
|
|
|
{ |
116
|
|
|
return $this->options; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the URI. |
121
|
|
|
* |
122
|
|
|
* @return \Psr\Http\Message\UriInterface |
123
|
|
|
*/ |
124
|
|
|
public function getUri() |
125
|
|
|
{ |
126
|
|
|
return $this->uri; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Parse the given URI. |
131
|
|
|
* |
132
|
|
|
* @param \Psr\Http\Message\UriInterface|string $uri |
133
|
|
|
* @return \Psr\Http\Message\UriInterface |
134
|
|
|
*/ |
135
|
|
|
protected function parseUri($uri) |
136
|
|
|
{ |
137
|
|
|
if ($uri instanceof UriInterface) { |
138
|
|
|
return $uri; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (is_string($uri) || (is_object($uri) && method_exists($uri, '__toString'))) { |
|
|
|
|
142
|
|
|
return new Uri((string)$uri); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
throw new InvalidArgumentException('URI must be a string or UriInterface'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the suffix to shorten to. |
150
|
|
|
* |
151
|
|
|
* @param string $suffix |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function to(string $suffix) |
155
|
|
|
{ |
156
|
|
|
$driver = $this->getClient()->driver(); |
157
|
|
|
|
158
|
|
|
if (!$driver instanceof CustomSuffixes) { |
159
|
|
|
throw new UnsupportedOperationException('Applying a custom suffix is not supported'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$driver->withSuffix($this, $suffix); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Add a piece of data to the builder. |
169
|
|
|
* |
170
|
|
|
* @param array|string $key |
171
|
|
|
* @param mixed|null $value |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function with($key, $value = null) |
175
|
|
|
{ |
176
|
|
|
if (is_array($key)) { |
177
|
|
|
$this->options = array_merge($this->options, $key); |
178
|
|
|
} else { |
179
|
|
|
$this->options[$key] = $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the shortened URI as a string. |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function __toString() |
191
|
|
|
{ |
192
|
|
|
return (string)$this->get(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|