1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\SetlistFm\Service; |
11
|
|
|
|
12
|
|
|
use Core23\SetlistFm\Connection\ConnectionInterface; |
13
|
|
|
use Core23\SetlistFm\Exception\ApiException; |
14
|
|
|
use Core23\SetlistFm\Exception\NotFoundException; |
15
|
|
|
|
16
|
|
|
abstract class AbstractService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ConnectionInterface |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AbstractService constructor. |
25
|
|
|
* |
26
|
|
|
* @param ConnectionInterface $connection |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ConnectionInterface $connection) |
29
|
|
|
{ |
30
|
|
|
$this->connection = $connection; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Calls the API unsigned. |
35
|
|
|
* |
36
|
|
|
* @param string $method |
37
|
|
|
* @param array $params |
38
|
|
|
* @param string $requestMethod |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
* |
42
|
|
|
* @throws ApiException |
43
|
|
|
* @throws NotFoundException |
44
|
|
|
*/ |
45
|
|
|
final protected function call(string $method, array $params = array(), $requestMethod = 'GET'): array |
46
|
|
|
{ |
47
|
|
|
foreach ($params as $key => $value) { |
48
|
|
|
if ($value instanceof \DateTime) { |
49
|
|
|
$params[$key] = $this->toDateString($value); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->connection->call($method, $params, $requestMethod); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Formats a date to a timestamp. |
58
|
|
|
* |
59
|
|
|
* @param \DateTime|null $date |
60
|
|
|
* |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
|
|
private function toDateString(\DateTime $date = null) : ?string |
64
|
|
|
{ |
65
|
|
|
if (null === $date) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $date->format('d-m-Y'); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|