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