|
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
|
3 |
|
public function __construct($paystackObj, $interface, array $payload = [ ], array $sentargs = [ ]) |
|
18
|
|
|
{ |
|
19
|
3 |
|
$this->request = new Request($paystackObj); |
|
20
|
3 |
|
$this->paystackObj = $paystackObj; |
|
21
|
3 |
|
$this->interface = $interface; |
|
22
|
3 |
|
$this->payload = $payload; |
|
23
|
3 |
|
$this->sentargs = $sentargs; |
|
24
|
3 |
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function build() |
|
27
|
|
|
{ |
|
28
|
1 |
|
$this->request->headers["Authorization"] = "Bearer " . $this->paystackObj->secret_key; |
|
29
|
1 |
|
$this->request->endpoint = Router::PAYSTACK_API_ROOT . $this->interface[RouteInterface::ENDPOINT_KEY]; |
|
30
|
1 |
|
$this->request->method = $this->interface[RouteInterface::METHOD_KEY]; |
|
31
|
1 |
|
$this->moveArgsToSentargs(); |
|
32
|
1 |
|
$this->putArgsIntoEndpoint($this->request->endpoint); |
|
33
|
1 |
|
$this->packagePayload(); |
|
34
|
1 |
|
return $this->request; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function packagePayload() |
|
38
|
|
|
{ |
|
39
|
1 |
|
if (is_array($this->payload) && count($this->payload)) { |
|
40
|
1 |
|
if ($this->request->method === RouteInterface::GET_METHOD) { |
|
41
|
1 |
|
$this->request->endpoint = $this->request->endpoint . '?' . http_build_query($this->payload); |
|
42
|
1 |
|
} else { |
|
43
|
1 |
|
$this->request->body = json_encode($this->payload); |
|
44
|
|
|
} |
|
45
|
1 |
|
} |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function putArgsIntoEndpoint(&$endpoint) |
|
49
|
|
|
{ |
|
50
|
2 |
|
foreach ($this->sentargs as $key => $value) { |
|
51
|
2 |
|
$endpoint = str_replace('{' . $key . '}', $value, $endpoint); |
|
52
|
2 |
|
} |
|
53
|
2 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function moveArgsToSentargs() |
|
56
|
|
|
{ |
|
57
|
2 |
|
if (!array_key_exists(RouteInterface::ARGS_KEY, $this->interface)) { |
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
2 |
|
$args = $this->interface[RouteInterface::ARGS_KEY]; |
|
61
|
2 |
|
foreach ($this->payload as $key => $value) { |
|
62
|
1 |
|
if (in_array($key, $args)) { |
|
63
|
1 |
|
$this->sentargs[$key] = $value; |
|
64
|
1 |
|
unset($this->payload[$key]); |
|
65
|
1 |
|
} |
|
66
|
2 |
|
} |
|
67
|
2 |
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|