1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the badams\GoogleUrl library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/badams/google-url |
7
|
|
|
* @package badams/google-url |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace badams\GoogleUrl\Actions; |
14
|
|
|
|
15
|
|
|
use badams\GoogleUrl\Exceptions\GoogleUrlException; |
16
|
|
|
use badams\GoogleUrl\Resources\Analytics; |
17
|
|
|
use badams\GoogleUrl\Resources\Url; |
18
|
|
|
use badams\GoogleUrl\ActionInterface; |
19
|
|
|
use GuzzleHttp\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Shorten |
23
|
|
|
* @package badams\GoogleUrl\Actions |
24
|
|
|
* @link https://developers.google.com/url-shortener/v1/url/insert |
25
|
|
|
*/ |
26
|
|
|
class Expand implements ActionInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $shortUrl; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
protected $projection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Shorten constructor. |
40
|
|
|
* @param $shortUrl |
41
|
|
|
* @throws GoogleUrlException |
42
|
|
|
* @internal param $longUrl |
43
|
|
|
*/ |
44
|
12 |
|
public function __construct($shortUrl, $projection = null) |
45
|
|
|
{ |
46
|
12 |
|
if (empty($shortUrl)) { |
47
|
3 |
|
throw new GoogleUrlException('No URL provided'); |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
if ($projection && !in_array($projection, [Analytics::FULL, Analytics::CLICKS, Analytics::TOP])) { |
51
|
3 |
|
throw new GoogleUrlException('Invalid Projection Parameter'); |
52
|
|
|
} |
53
|
|
|
|
54
|
6 |
|
$this->shortUrl = $shortUrl; |
55
|
6 |
|
$this->projection = $projection; |
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
6 |
|
public function getRequestMethod() |
62
|
|
|
{ |
63
|
6 |
|
return 'GET'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
6 |
|
public function getRequestOptions() |
70
|
|
|
{ |
71
|
|
|
return [ |
72
|
6 |
|
'query' => array_filter([ |
73
|
6 |
|
'shortUrl' => $this->shortUrl, |
74
|
6 |
|
'projection' => $this->projection |
75
|
4 |
|
]) |
76
|
4 |
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param ResponseInterface $response |
81
|
|
|
* @return Url |
82
|
|
|
*/ |
83
|
6 |
|
public function processResponse(ResponseInterface $response) |
84
|
|
|
{ |
85
|
6 |
|
$obj = json_decode($response->getBody()->getContents()); |
86
|
6 |
|
return Url::createFromJson($obj); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|