1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digikraaft\Paystack; |
4
|
|
|
|
5
|
|
|
use Digikraaft\Paystack\Exceptions\InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
class ApiResource |
8
|
|
|
{ |
9
|
|
|
const OBJECT_NAME = 'apiresource'; |
10
|
|
|
|
11
|
|
|
use ApiOperations\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return string the base URL for the given class |
15
|
|
|
*/ |
16
|
|
|
public static function baseUrl() |
17
|
|
|
{ |
18
|
|
|
return Paystack::$apiBase; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return string the endpoint URL for the given class |
23
|
|
|
*/ |
24
|
|
|
public static function classUrl() |
25
|
|
|
{ |
26
|
|
|
$base = static::OBJECT_NAME; |
27
|
|
|
|
28
|
|
|
return "{$base}"; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param null|string $id the ID of the resource |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException if $id is null |
35
|
|
|
* |
36
|
|
|
* @return string the endpoint URL for the given class |
37
|
|
|
*/ |
38
|
|
|
public static function resourceUrl($id) |
39
|
|
|
{ |
40
|
|
|
if (null === $id) { |
41
|
|
|
$message = 'Invalid ID supplied. ID cannot be null'; |
42
|
|
|
|
43
|
|
|
throw new InvalidArgumentException($message); |
44
|
|
|
} |
45
|
|
|
$id = Util\Util::utf8($id); |
46
|
|
|
$base = static::classUrl(); |
47
|
|
|
$extn = urlencode($id); |
48
|
|
|
|
49
|
|
|
return "{$base}/{$extn}"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $slug |
54
|
|
|
* |
55
|
|
|
* @return string the endpoint URL for the given class |
56
|
|
|
*/ |
57
|
|
|
public static function endPointUrl($slug) |
58
|
|
|
{ |
59
|
|
|
$slug = Util\Util::utf8($slug); |
60
|
|
|
$base = static::classUrl(); |
61
|
|
|
|
62
|
|
|
return "{$base}/{$slug}"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $slug |
67
|
|
|
* @param $params array of query parameters |
68
|
|
|
* |
69
|
|
|
* @return string the endpoint URL for the given class |
70
|
|
|
*/ |
71
|
|
|
public static function buildQueryString($slug, $params = null) |
72
|
|
|
{ |
73
|
|
|
$url = self::endPointUrl($slug); |
74
|
|
|
if (! empty($params)) { |
75
|
|
|
$url .= '?'.http_build_query($params); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $url; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|