/
Client.php
143 lines (122 loc) · 3.46 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace EmanueleMinotto\Embedly;
use GuzzleHttp\Client as GuzzleHttp_Client;
use GuzzleHttp\ClientInterface as GuzzleHttp_ClientInterface;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Event\BeforeEvent;
/**
* Embed.ly API client.
*
* @author Emanuele Minotto <minottoemanuele@gmail.com>
*
* @method array extract(array $params)
* @method array oembed(array $params)
*/
class Client extends GuzzleClient
{
/**
* Embed.ly API version.
*/
const API_VERSION = 1;
/**
* API key.
*
* @var string|null
*/
private $apiKey = null;
/**
* Class constructor.
*
* @param string|null $apiKey API key (optional).
* @param \GuzzleHttp\ClientInterface|null $httpClient Alternative Guzzle HTTP client (optional).
*/
public function __construct($apiKey = null, GuzzleHttp_ClientInterface $httpClient = null)
{
// Embed.ly API description
$description = json_decode(file_get_contents(__DIR__.'/description.json'), true);
parent::__construct(
$httpClient ?: new GuzzleHttp_Client(),
new Description($description)
);
$this->setApiKey($apiKey);
$this->getHttpClient()->getEmitter()->once(
'before',
function (BeforeEvent $event) {
$this->getBeforeEvent($event);
}
);
}
/**
* API key setter.
*
* @param string|null $apiKey
*/
public function setApiKey($apiKey = null)
{
$this->apiKey = (string) $apiKey;
return $this;
}
/**
* API key getter.
*
* @return string|null
*/
public function getApiKey()
{
return $this->apiKey;
}
/**
* Guzzle event used to set the key parameter.
*
* @codeCoverageIgnore
*
* @param BeforeEvent $event
*/
private function getBeforeEvent(BeforeEvent $event)
{
$request = $event->getRequest();
if ('api.embed.ly' === $request->getHost()) {
$requestQuery = $request->getQuery();
$requestQuery->setEncodingType(false);
$requestQuery->set('key', $this->apiKey);
$request->setQuery($requestQuery);
}
}
/**
* The display based routes.
*
* @param string $method Display method, can be crop, resize, fill or empty.
* @param array $params Request options.
*
* @link http://embed.ly/docs/api/display/endpoints/1/display
* @link http://embed.ly/docs/api/display/endpoints/1/display/crop
* @link http://embed.ly/docs/api/display/endpoints/1/display/fill
* @link http://embed.ly/docs/api/display/endpoints/1/display/resize
*
* @return string
*/
public function display($method = null, array $params = array())
{
$httpClient = $this->getHttpClient();
if (trim($method)) {
$method = '/'.ltrim($method, '/');
}
$params['key'] = $this->getApiKey();
$response = $httpClient->get('http://i.embed.ly/1/display'.$method, [
'query' => $params,
]);
return (string) $response->getBody();
}
/**
* Method used to encode URLs imploding them with a comma.
*
* @param array $values
*
* @return string
*/
public static function encodeUrls(array $values)
{
return implode(',', array_map('urlencode', $values));
}
}