|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yabacon; |
|
4
|
|
|
|
|
5
|
|
|
use \Yabacon\Paystack\Helpers\Router; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Paystack |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
public $secret_key; |
|
14
|
|
|
private $routes; |
|
15
|
|
|
public $use_guzzle = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Creates a new Paystack object |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $secret_key - Secret key for your account with Paystack |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public function __construct($secret_key) |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
4 |
|
if (!is_string($secret_key) || !(substr($secret_key, 0, 3)==='sk_')) { |
|
26
|
|
|
// Should never get here |
|
27
|
1 |
|
throw new \InvalidArgumentException('A Valid Paystack Secret Key must start with \'sk_\'.'); |
|
28
|
|
|
} |
|
29
|
3 |
|
$this->secret_key = $secret_key; |
|
30
|
3 |
|
$this->routes = $this->definedRoutes(); |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Router uses Guzzle for calls if GuzzleHttp is found |
|
35
|
|
|
*/ |
|
36
|
|
|
public function useGuzzle() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->use_guzzle = true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* definedRoutes |
|
43
|
|
|
* gets routes defined in the Yabacon\Paystack\Routes namespace |
|
44
|
|
|
* since we are using PSR-4. It is safe to assume that all php |
|
45
|
|
|
* files in the folder are names of the classes. |
|
46
|
|
|
* |
|
47
|
|
|
* @return the list of defined Routes |
|
48
|
|
|
* |
|
49
|
|
|
* @access private |
|
50
|
|
|
* @see Yabacon\Paystack\Routes\Router |
|
51
|
|
|
* @since 1.0 |
|
52
|
|
|
*/ |
|
53
|
3 |
|
private function definedRoutes() |
|
54
|
|
|
{ |
|
55
|
3 |
|
$routes = []; |
|
56
|
3 |
|
$files = scandir(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Paystack'.DIRECTORY_SEPARATOR.'Routes'); |
|
57
|
3 |
|
foreach ($files as $file) { |
|
58
|
3 |
|
if ('php'===pathinfo($file, PATHINFO_EXTENSION)) { |
|
59
|
3 |
|
$routes[] = strtolower(substr($file, 0, strrpos($file, "."))); |
|
60
|
3 |
|
} |
|
61
|
3 |
|
} |
|
62
|
3 |
|
return $routes; |
|
63
|
|
|
} |
|
64
|
|
|
/** |
|
65
|
|
|
* __call |
|
66
|
|
|
* Magic Method for fetch on routes |
|
67
|
|
|
* |
|
68
|
|
|
* @param $method - a string whose title case is a class in the |
|
69
|
|
|
* Yabacon\Paystack\Routes namespace implementing |
|
70
|
|
|
* Yabacon\Paystack\Contracts\RouteInterface |
|
71
|
|
|
* @param $args - arguments sent to the magic method |
|
72
|
|
|
* |
|
73
|
|
|
* @return the result of calling /{route}/get on the api |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
* @see Yabacon\Paystack\Routes\Router |
|
77
|
|
|
* @since 1.0 |
|
78
|
|
|
*/ |
|
79
|
|
|
public function __call($method, $args) |
|
80
|
|
|
{ |
|
81
|
|
|
/* |
|
82
|
|
|
attempt to call fetch when the route is called directly |
|
83
|
|
|
translates to /{root}/{get}/{id} |
|
84
|
|
|
*/ |
|
85
|
|
|
|
|
86
|
|
|
if (in_array($method, $this->routes, true) && count($args) === 1) { |
|
87
|
|
|
$route = new Router($method, $this); |
|
88
|
|
|
// no params, just one arg... the id |
|
89
|
|
|
$args = [[], [ Router::ID_KEY => $args[0] ] ]; |
|
90
|
|
|
return $route->__call('fetch', $args); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Not found is it plural? |
|
94
|
|
|
$is_plural = strripos($method, 's')===(strlen($method)-1); |
|
95
|
|
|
$singular_form = substr($method, 0, strlen($method)-1); |
|
96
|
|
|
|
|
97
|
|
|
if ($is_plural && in_array($singular_form, $this->routes, true)) { |
|
98
|
|
|
$route = new Router($singular_form, $this); |
|
99
|
|
|
if ((count($args) === 1 && is_array($args[0]))||(count($args) === 0)) { |
|
100
|
|
|
return $route->__call('getList', $args); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Should never get here |
|
105
|
|
|
throw new \InvalidArgumentException( |
|
106
|
|
|
'Route "' . $method . '" can only accept '. |
|
107
|
|
|
($is_plural ? |
|
108
|
|
|
'an optional array of paging arguments (perPage, page)' |
|
109
|
|
|
: 'an id or code') . '.' |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Call this function to register the custom auto loader for Paystack. |
|
115
|
|
|
* Required only if not using composer. |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function registerAutoloader() |
|
118
|
|
|
{ |
|
119
|
|
|
spl_autoload_register( |
|
120
|
|
|
/** |
|
121
|
|
|
* Paystack Autoloader |
|
122
|
|
|
* For use when library is being used without composer |
|
123
|
|
|
*/ |
|
124
|
|
|
function ($class_name) { |
|
125
|
|
|
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
|
126
|
|
|
$file .= str_replace([ 'Yabacon\\', '\\' ], ['', DIRECTORY_SEPARATOR ], $class_name) . '.php'; |
|
127
|
|
|
include_once $file; |
|
128
|
|
|
} |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* __get |
|
134
|
|
|
* Insert description here |
|
135
|
|
|
* |
|
136
|
|
|
* @param $name |
|
137
|
|
|
* |
|
138
|
|
|
* @return |
|
139
|
|
|
* |
|
140
|
|
|
* @access |
|
141
|
|
|
* @static |
|
142
|
|
|
* @see |
|
143
|
|
|
* @since |
|
144
|
|
|
*/ |
|
145
|
|
|
public function __get($name) |
|
146
|
|
|
{ |
|
147
|
|
|
if (in_array($name, $this->routes, true)) { |
|
148
|
|
|
return new Router($name, $this); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|