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\ToString; |
10
|
|
|
use LaraCrafts\UrlShortener\Concerns\ToUri; |
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
|
|
|
public function __construct(Client $client, $uri, array $options) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
$this->options = $options; |
33
|
|
|
$this->uri = $this->parseUri($uri); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set the domain to shorten to. |
38
|
|
|
* |
39
|
|
|
* @param string $domain |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
|
|
public function domain(string $domain) |
43
|
|
|
{ |
44
|
|
|
$driver = $this->getClient()->driver(); |
45
|
|
|
|
46
|
|
|
if (!$driver instanceof CustomDomains) { |
47
|
|
|
throw new UnsupportedOperationException('Applying a custom domain is not supported'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$driver->withDomain($this, $domain); |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the shortened URI. |
57
|
|
|
* |
58
|
|
|
* @return \Psr\Http\Message\UriInterface |
59
|
|
|
*/ |
60
|
|
|
public function get() |
61
|
|
|
{ |
62
|
|
|
$driver = $this->getClient()->driver(); |
63
|
|
|
|
64
|
|
|
if ($driver instanceof ToUri) { |
65
|
|
|
$uri = $driver->toUri($this->uri, $this->options); |
66
|
|
|
} elseif ($driver instanceof ToString) { |
67
|
|
|
$uri = $driver->toString($this->uri, $this->options); |
68
|
|
|
} else { |
69
|
|
|
throw new UnsupportedOperationException('URL shortening is not supported'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->parseUri($uri); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the client. |
77
|
|
|
* |
78
|
|
|
* @return \LaraCrafts\UrlShortener\Contracts\Client |
79
|
|
|
*/ |
80
|
|
|
public function getClient() |
81
|
|
|
{ |
82
|
|
|
return $this->client; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the options. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getOptions() |
91
|
|
|
{ |
92
|
|
|
return $this->options; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the URI. |
97
|
|
|
* |
98
|
|
|
* @return \Psr\Http\Message\UriInterface |
99
|
|
|
*/ |
100
|
|
|
public function getUri() |
101
|
|
|
{ |
102
|
|
|
return $this->uri; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Parse the given URI. |
107
|
|
|
* |
108
|
|
|
* @param \Psr\Http\Message\UriInterface|string $uri |
109
|
|
|
* @return \Psr\Http\Message\UriInterface |
110
|
|
|
*/ |
111
|
|
|
protected function parseUri($uri) |
112
|
|
|
{ |
113
|
|
|
if ($uri instanceof UriInterface) { |
114
|
|
|
return $uri; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (is_string($uri) || (is_object($uri) && method_exists($uri, '__toString'))) { |
|
|
|
|
118
|
|
|
return new Uri((string)$uri); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
throw new InvalidArgumentException('URI must be a string or UriInterface'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the suffix to shorten to. |
126
|
|
|
* |
127
|
|
|
* @param string $suffix |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function to(string $suffix) |
131
|
|
|
{ |
132
|
|
|
$driver = $this->getClient()->driver(); |
133
|
|
|
|
134
|
|
|
if (!$driver instanceof CustomSuffixes) { |
135
|
|
|
throw new UnsupportedOperationException('Applying a custom suffix is not supported'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$driver->withSuffix($this, $suffix); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Add a piece of data to the builder. |
145
|
|
|
* |
146
|
|
|
* @param array|string $key |
147
|
|
|
* @param mixed|null $value |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function with($key, $value = null) |
151
|
|
|
{ |
152
|
|
|
if (is_array($key)) { |
153
|
|
|
$this->options = array_merge($this->options, $key); |
154
|
|
|
} else { |
155
|
|
|
$this->options[$key] = $value; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the shortened URI as a string. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function __toString() |
167
|
|
|
{ |
168
|
|
|
return (string)$this->get(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|