1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nxmad\Larapay\Abstracts; |
6
|
|
|
|
7
|
|
|
use Nxmad\Larapay\Units\Redirect; |
8
|
|
|
use Illuminate\Config\Repository; |
9
|
|
|
use Nxmad\Larapay\Contracts\Fields; |
10
|
|
|
use Nxmad\Larapay\Traits\HasMutations; |
11
|
|
|
use Illuminate\Http\Request as HttpRequest; |
12
|
|
|
|
13
|
|
|
abstract class Request implements Fields |
14
|
|
|
{ |
15
|
|
|
use HasMutations; |
16
|
|
|
|
17
|
|
|
const GET = 'GET'; |
18
|
|
|
const POST = 'POST'; |
19
|
|
|
const CUSTOM = '_'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The gateway instance. |
23
|
|
|
* |
24
|
|
|
* @var Gateway |
25
|
|
|
*/ |
26
|
|
|
protected $gateway; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The HTTP request instance if presented. |
30
|
|
|
* |
31
|
|
|
* @var HttpRequest|null |
32
|
|
|
*/ |
33
|
|
|
protected $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The list of required fields by request. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $required = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The list of fields to transform. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $aliases = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The HTTP method of request. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $method = Redirect::GET; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Request constructor. |
58
|
|
|
* |
59
|
|
|
* @param Gateway $gateway |
60
|
|
|
* @param HttpRequest|null $request |
61
|
|
|
*/ |
62
|
|
|
public function __construct(Gateway $gateway, ?HttpRequest $request = null) |
63
|
|
|
{ |
64
|
|
|
$this->gateway = $gateway; |
65
|
|
|
$this->request = $request; |
66
|
|
|
$this->query = new Repository; |
67
|
|
|
|
68
|
|
|
if ($request) { |
69
|
|
|
$this->fill($request->all()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get original query before mutation. |
75
|
|
|
* |
76
|
|
|
* @return Repository |
77
|
|
|
*/ |
78
|
|
|
public function getOriginal() |
79
|
|
|
{ |
80
|
|
|
return $this->query; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get endpoint for current request. |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
|
|
public function endpoint() |
89
|
|
|
{ |
90
|
|
|
return $this->gateway->getEndpoint($this); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Transform request to redirect. |
95
|
|
|
* |
96
|
|
|
* @return Redirect |
97
|
|
|
*/ |
98
|
|
|
public function redirect() |
99
|
|
|
{ |
100
|
|
|
return new Redirect($this->endpoint(), $this->method, $this->all()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Fires before send. |
105
|
|
|
*/ |
106
|
|
|
public function prepare() |
107
|
|
|
{ |
108
|
|
|
// ... |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function send() |
115
|
|
|
{ |
116
|
|
|
if (in_array(self::SIGNATURE, $this->required)) { |
117
|
|
|
$this->set(self::SIGNATURE, $this->gateway->sign($this)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($this->required as $field) { |
121
|
|
|
if (! $this->has($field)) { |
122
|
|
|
throw new \RuntimeException("Required field [{$field}] is not presented."); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->prepare(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Convert no_one_should_use_this_case to CamelCase. |
131
|
|
|
* |
132
|
|
|
* @param string $str |
133
|
|
|
* @param string $sep |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
protected function camelize(string $str, string $sep = '_'): string |
138
|
|
|
{ |
139
|
|
|
return str_replace($sep, '', ucwords($str, $sep)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|