1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yabacon\Paystack\Http; |
4
|
|
|
|
5
|
|
|
use \Yabacon\Paystack\Contracts\RouteInterface; |
6
|
|
|
use \Yabacon\Paystack\Helpers\Router; |
7
|
|
|
|
8
|
|
|
class RequestBuilder |
9
|
|
|
{ |
10
|
|
|
protected $paystackObj; |
11
|
|
|
protected $interface; |
12
|
|
|
protected $request; |
13
|
|
|
|
14
|
|
|
public $payload = [ ]; |
15
|
|
|
public $sentargs = [ ]; |
16
|
|
|
|
17
|
2 |
|
public function __construct($paystackObj, $interface, array $payload = [ ], array $sentargs = [ ]) |
18
|
|
|
{ |
19
|
2 |
|
$this->request = new Request($paystackObj); |
20
|
2 |
|
$this->paystackObj = $paystackObj; |
21
|
2 |
|
$this->interface = $interface; |
22
|
2 |
|
$this->payload = $payload; |
23
|
2 |
|
$this->sentargs = $sentargs; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
public function build() |
27
|
|
|
{ |
28
|
|
|
$this->request->headers["Authorization"] = "Bearer " . $this->paystackObj->secret_key; |
29
|
|
|
$this->request->endpoint = Router::PAYSTACK_API_ROOT . $this->interface[RouteInterface::ENDPOINT_KEY]; |
30
|
|
|
$this->request->method = $this->interface[RouteInterface::METHOD_KEY]; |
31
|
|
|
$this->moveArgsToSentargs(); |
32
|
|
|
$this->putArgsIntoEndpoint($this->request->endpoint); |
33
|
|
|
$this->packagePayload(); |
34
|
|
|
return $this->request; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function packagePayload() |
38
|
|
|
{ |
39
|
|
|
if ($this->request->method === RouteInterface::GET_METHOD) { |
40
|
|
|
$this->request->body = json_encode($this->payload); |
41
|
|
|
} else { |
42
|
|
|
$this->request->endpoint = $this->request->endpoint . '?' . http_build_query($this->payload); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function putArgsIntoEndpoint(&$endpoint) |
47
|
|
|
{ |
48
|
1 |
|
foreach ($this->sentargs as $key => $value) { |
49
|
1 |
|
$endpoint = str_replace('{' . $key . '}', $value, $endpoint); |
50
|
1 |
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function moveArgsToSentargs() |
54
|
|
|
{ |
55
|
1 |
|
if (!array_key_exists(RouteInterface::ARGS_KEY, $this->interface)) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
1 |
|
$args = $this->interface[RouteInterface::ARGS_KEY]; |
59
|
1 |
|
foreach ($this->payload as $key => $value) { |
60
|
1 |
|
if (in_array($key, $args)) { |
61
|
1 |
|
$this->sentargs[$key] = $value; |
62
|
1 |
|
unset($this->payload[$key]); |
63
|
1 |
|
} |
64
|
1 |
|
} |
65
|
1 |
|
} |
66
|
|
|
} |
67
|
|
|
|