1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ni-ju-san CMS. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Gripp <[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 Core23\LastFm\Connection; |
13
|
|
|
|
14
|
|
|
use Core23\LastFm\Exception\ApiException; |
15
|
|
|
|
16
|
|
|
abstract class AbstractConnection implements ConnectionInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Default Endpoint. |
20
|
|
|
*/ |
21
|
|
|
const DEFAULT_WS_ENDPOINT = 'http://ws.audioscrobbler.com/2.0/'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $apiKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $sharedSecret; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $uri; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AbstractConnection constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $apikey |
42
|
|
|
* @param string $sharedSecret |
43
|
|
|
* @param string $uri |
44
|
|
|
*/ |
45
|
|
|
public function __construct($apikey, $sharedSecret, $uri = null) |
46
|
|
|
{ |
47
|
|
|
if (null === $uri) { |
48
|
|
|
$uri = static::DEFAULT_WS_ENDPOINT; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->apiKey = $apikey; |
52
|
|
|
$this->sharedSecret = $sharedSecret; |
53
|
|
|
$this->uri = $uri; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function signedCall($method, array $params = array(), SessionInterface $session = null, $requestMethod = 'GET') |
60
|
|
|
{ |
61
|
|
|
// Call parameter |
62
|
|
|
$callParams = array( |
63
|
|
|
'method' => $method, |
64
|
|
|
'api_key' => $this->apiKey, |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// Add session key |
68
|
|
|
if (null !== $session) { |
69
|
|
|
$callParams['sk'] = $session->getKey(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$params = array_merge($callParams, $params); |
73
|
|
|
$params = $this->filterNull($params); |
74
|
|
|
$params = $this->encodeUTF8($params); |
75
|
|
|
|
76
|
|
|
// Sign parameter |
77
|
|
|
$params['api_sig'] = $this->signParams($params); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $this->call($params, $requestMethod); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function unsignedCall($method, array $params = array(), $requestMethod = 'GET') |
86
|
|
|
{ |
87
|
|
|
// Call parameter |
88
|
|
|
$callParameter = array( |
89
|
|
|
'method' => $method, |
90
|
|
|
'api_key' => $this->apiKey, |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$params = array_merge($callParameter, $params); |
94
|
|
|
$params = $this->filterNull($params); |
95
|
|
|
$params = $this->encodeUTF8($params); |
96
|
|
|
|
97
|
|
|
return $this->call($params, $requestMethod); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function getApiKey() |
104
|
|
|
{ |
105
|
|
|
return $this->apiKey; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getSharedSecret() |
112
|
|
|
{ |
113
|
|
|
return $this->sharedSecret; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Performs the webservice call. |
118
|
|
|
* |
119
|
|
|
* @param array $params |
120
|
|
|
* @param string $requestMethod |
121
|
|
|
* |
122
|
|
|
* @return array |
123
|
|
|
* |
124
|
|
|
* @throws ApiException |
125
|
|
|
*/ |
126
|
|
|
abstract protected function call(array $params, $requestMethod = 'GET'); |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Filter null values. |
130
|
|
|
* |
131
|
|
|
* @param array $object |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
private function filterNull($object) |
136
|
|
|
{ |
137
|
|
|
return array_filter($object, function ($val) { |
138
|
|
|
return !is_null($val); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** Converts any string or array of strings to UTF8. |
143
|
|
|
* @param mixed $object a String or an array |
144
|
|
|
* |
145
|
|
|
* @return mixed uTF8-string or array |
146
|
|
|
*/ |
147
|
|
|
private function encodeUTF8($object) |
148
|
|
|
{ |
149
|
|
|
if (is_array($object)) { |
150
|
|
|
return array_map(array($this, 'encodeUTF8'), $object); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return mb_convert_encoding($object, 'UTF-8', 'auto'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
private function signParams(array $params) |
160
|
|
|
{ |
161
|
|
|
ksort($params); |
162
|
|
|
|
163
|
|
|
$signature = ''; |
164
|
|
|
foreach ($params as $name => $value) { |
165
|
|
|
$signature .= $name.$value; |
166
|
|
|
} |
167
|
|
|
$signature .= $this->sharedSecret; |
168
|
|
|
|
169
|
|
|
return md5($signature); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.