-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
219 lines (178 loc) · 5.44 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* Issuu Client API (http://www.issuu.com)
*
* @link http://github.com/Astronuts/Issuu-PHP-Class
* @copyright Copyright (c) 2013 Theta Design AS (http://www.thetadesign.no)
* @author Chris Magnussen <chris at thetadesign dot no>
* @license See LICENSE - New BSD License
* @package Core\Issuu
*/
/**
* @uses Core\Issuu\Options
*/
namespace Core\Issuu;
use Core\Issuu\Options;
use Core\Issuu\Http\Request;
/**
* @category Core
* @package Core\Issuu
* @subpackage Client
*/
class Client extends Options {
/**
* Adapter options
* @var mixed
*/
protected $options;
/**
* API Endpoint
* @var string $_endpoint
*/
private $_endpoint = 'http://api.issuu.com/1_0';
/**
* Signed request as built by API request
* @var MD5 $_signature
*/
private $_signature;
/**
* Application settings
* @var array $_appSettings
*/
private $_appSettings = array();
/**
* @var mixed
*/
protected $postData = null;
/**
* @var mixed
*/
public $response;
/**
* Construct with authorization keys
*
* @param string $key API key
* @param string $secret API secret key
* @return void
*/
public function __construct($key, $secret)
{
$this->_appSettings['apiKey'] = $key;
$this->_appSettings['secretKey'] = $secret;
}
/**
* Set API options
*
* @param array $options
* @return void
*/
public function setOptions($options)
{
if (!$options instanceof Options)
$options = new Options($options);
$this->options = $options;
}
/**
* Get all options
*
* @return \Core\Issuu\Options
*/
public function getOptions()
{
return $this->options;
}
public function adapter($adapter)
{
$namespace = 'Core\\Issuu\\Adapter\\'.$adapter;
return new $namespace($this->_appSettings, $this);
}
public function request($data = null)
{
if ($data !== null)
$this->postData = $data;
$request = new Request();
$query = $this->buildQuery();
$request->setMethod(!is_null($data) ? Request::METHOD_POST : Request::METHOD_GET);
$request->setUri(!is_null($data) ? $this->_endpoint : $this->_endpoint.'?'.$query);
$this->response = json_decode($request->send(!is_null($data) ? $query : null));
return $this->options->responseType == 'full' ? $this->response : $this->response->rsp->_content->result->_content;
}
/**
* Build query
*
* @return string
*/
protected function buildQuery()
{
if (!empty($this->_appSettings))
$this->createSignature();
$query = array("signature" => $this->_signature);
if ($this->postData !== null)
$query = array_merge($query, $this->postData);
$options = (array) $this->options;
array_map(function($key, $val) use (&$query) {
if (strstr($key, '*'))
$key = substr($key, 3);
if ($val !== '' && $val !== 0 && $val !== null)
$query[$key] = $val;
}, array_keys($options), $options);
ksort($query);
$query['apiKey'] = $this->_appSettings['apiKey'];
$query = http_build_query(array_filter($query));
return $query;
}
/**
* Create signature to use with every request
*
* @return void
*/
protected function createSignature()
{
$signature = $this->_appSettings['secretKey'];
$ph = array();
$options = (array) $this->options;
$options['apiKey'] = $this->_appSettings['apiKey'];
if ($this->postData !== null)
$options = array_merge($options, $this->postData);
array_map(function($key, $val) use (&$ph) {
if (strstr($key, '*'))
$key = substr($key, 3);
if ($val !== '' && $val !== 0 && $val !== null)
$ph[$key] = $val;
}, array_keys($options), $options);
ksort($ph);
foreach ($ph as $key => $val)
$signature .= $key.$val;
$this->_signature = md5($signature);
}
public function getSignature()
{
return $this->_signature;
}
/**
* Get option by key
*
* @param string $method
* @param mixed $args
* @throws \Exception
* @return string|boolean
*/
public function __call($method, $args)
{
$key = strtolower(substr($method, 3, 1)) . substr($method, 4);
$value = isset($this->options->$key) ? $this->options->$key : null;
switch (substr($method, 0, 3)) {
case 'get':
if (property_exists($this->options, $key)) {
return $this->options->$key;
}
break;
case 'has':
return property_exists($this->options, $key);
break;
}
throw new \Exception('Method "' . $method . '" does not exist and was not trapped in __call()');
}
public function setApiKey($key) { $this->_appSettings['secret'] = $key; }
public function setApiSecret($secret) { $this->_appSettings['secret'] = $secret; }
}