1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Concise package. |
5
|
|
|
* |
6
|
|
|
* (c) Antoine Corcy <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Concise\Provider; |
13
|
|
|
|
14
|
|
|
use Concise\Provider; |
15
|
|
|
use Http\Client\HttpClient; |
16
|
|
|
use Http\Message\RequestFactory; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Tinycc implements Provider |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const ENDPOINT = 'http://tiny.cc'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $params = [ |
32
|
|
|
'c' => 'rest_api', |
33
|
|
|
'version' => '2.0.3', |
34
|
|
|
'format' => 'json', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $login |
39
|
|
|
* @param string $apiKey |
40
|
|
|
* @param HttpClient $httpClient |
41
|
|
|
* @param RequestFactory $requestFactory |
42
|
|
|
*/ |
43
|
4 |
|
public function __construct($login, $apiKey, HttpClient $httpClient, RequestFactory $requestFactory) |
44
|
|
|
{ |
45
|
4 |
|
$this->params['login'] = $login; |
46
|
4 |
|
$this->params['apiKey'] = $apiKey; |
47
|
|
|
|
48
|
4 |
|
$this->httpClient = $httpClient; |
|
|
|
|
49
|
4 |
|
$this->requestFactory = $requestFactory; |
|
|
|
|
50
|
4 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function shorten($url) |
56
|
|
|
{ |
57
|
1 |
|
$params = array_merge($this->params, [ |
58
|
1 |
|
'm' => 'shorten', |
59
|
1 |
|
'longUrl' => trim($url), |
60
|
1 |
|
]); |
61
|
|
|
|
62
|
1 |
|
$url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params)); |
63
|
|
|
|
64
|
1 |
|
$request = $this->requestFactory->createRequest('GET', $url); |
65
|
|
|
|
66
|
1 |
|
$response = $this->httpClient->sendRequest($request); |
67
|
|
|
|
68
|
1 |
|
$response = json_decode((string) $response->getBody()); |
69
|
|
|
|
70
|
1 |
|
return $response->results->short_url; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
1 |
|
public function expand($url) |
77
|
|
|
{ |
78
|
1 |
|
$params = array_merge($this->params, [ |
79
|
1 |
|
'm' => 'expand', |
80
|
1 |
|
'shortUrl' => trim($url), |
81
|
1 |
|
]); |
82
|
|
|
|
83
|
1 |
|
$url = sprintf('%s?%s', self::ENDPOINT, http_build_query($params)); |
84
|
|
|
|
85
|
1 |
|
$request = $this->requestFactory->createRequest('GET', $url); |
86
|
|
|
|
87
|
1 |
|
$response = $this->httpClient->sendRequest($request); |
88
|
|
|
|
89
|
1 |
|
$response = json_decode((string) $response->getBody()); |
90
|
|
|
|
91
|
1 |
|
return $response->results->longUrl; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: