1
|
|
|
<?php namespace Arcanedev\Stripe\Http; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Stripe\Contracts\Http\RequestOptions as RequestOptionsContract; |
4
|
|
|
use Arcanedev\Stripe\Exceptions\ApiException; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class RequestOptions |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Stripe\Http |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class RequestOptions implements RequestOptionsContract |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** @var string */ |
19
|
|
|
public $apiKey; |
20
|
|
|
|
21
|
|
|
/** @var array */ |
22
|
|
|
public $headers; |
23
|
|
|
|
24
|
|
|
/* ------------------------------------------------------------------------------------------------ |
25
|
|
|
| Constructor |
26
|
|
|
| ------------------------------------------------------------------------------------------------ |
27
|
|
|
*/ |
28
|
|
|
/** |
29
|
|
|
* Create a RequestOptions instance. |
30
|
|
|
* |
31
|
|
|
* @param string|null $apiKey |
32
|
|
|
* @param array $headers |
33
|
|
|
*/ |
34
|
520 |
|
public function __construct($apiKey = null, array $headers = []) |
35
|
|
|
{ |
36
|
520 |
|
$this->setApiKey($apiKey); |
37
|
520 |
|
$this->setHeaders($headers); |
38
|
520 |
|
} |
39
|
|
|
|
40
|
|
|
/* ------------------------------------------------------------------------------------------------ |
41
|
|
|
| Getters & Setters |
42
|
|
|
| ------------------------------------------------------------------------------------------------ |
43
|
|
|
*/ |
44
|
|
|
/** |
45
|
|
|
* Get API Key. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
32 |
|
public function getApiKey() |
50
|
|
|
{ |
51
|
32 |
|
return $this->apiKey; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set API Key. |
56
|
|
|
* |
57
|
|
|
* @param string $apiKey |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
520 |
|
public function setApiKey($apiKey) |
62
|
|
|
{ |
63
|
520 |
|
if ( ! is_null($apiKey)) |
64
|
8 |
|
$this->apiKey = trim($apiKey); |
65
|
|
|
|
66
|
520 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get Headers. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
14 |
|
public function getHeaders() |
75
|
|
|
{ |
76
|
14 |
|
return $this->headers; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set Headers. |
81
|
|
|
* |
82
|
|
|
* @param array $headers |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
520 |
|
protected function setHeaders($headers) |
87
|
|
|
{ |
88
|
520 |
|
$this->headers = $headers; |
89
|
|
|
|
90
|
520 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* ------------------------------------------------------------------------------------------------ |
94
|
|
|
| Main Functions |
95
|
|
|
| ------------------------------------------------------------------------------------------------ |
96
|
|
|
*/ |
97
|
|
|
/** |
98
|
|
|
* Unpacks an options array and merges it into the existing RequestOptions object. |
99
|
|
|
* |
100
|
|
|
* @param array|string|null $options |
101
|
|
|
* |
102
|
|
|
* @return self |
103
|
|
|
*/ |
104
|
144 |
|
public function merge($options) |
105
|
|
|
{ |
106
|
144 |
|
$otherOptions = self::parse($options); |
107
|
|
|
|
108
|
144 |
|
if (is_null($otherOptions->apiKey)) |
109
|
144 |
|
$otherOptions->apiKey = $this->apiKey; |
110
|
|
|
|
111
|
144 |
|
$otherOptions->headers = array_merge($this->headers, $otherOptions->headers); |
112
|
|
|
|
113
|
144 |
|
return $otherOptions; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Unpacks an options array into an Options object. |
118
|
|
|
* |
119
|
|
|
* @param array|string|null $options |
120
|
|
|
* |
121
|
|
|
* @return self |
122
|
|
|
*/ |
123
|
412 |
|
public static function parse($options) |
124
|
|
|
{ |
125
|
412 |
|
self::checkOptions($options); |
126
|
|
|
|
127
|
410 |
|
if ($options instanceof self) |
128
|
152 |
|
return $options; |
129
|
|
|
|
130
|
410 |
|
if (is_null($options) || is_string($options)) |
131
|
396 |
|
return new self($options, []); |
132
|
|
|
|
133
|
|
|
// $options is array |
134
|
34 |
|
return new self( |
135
|
34 |
|
isset($options['api_key']) ? $options['api_key'] : null, |
136
|
34 |
|
self::prepareHeaders($options) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Prepare headers. |
142
|
|
|
* |
143
|
|
|
* @param array $options |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
34 |
|
private static function prepareHeaders($options = []) |
148
|
|
|
{ |
149
|
34 |
|
$headers = []; |
150
|
|
|
$keys = [ |
151
|
34 |
|
'idempotency_key' => 'Idempotency-Key', |
152
|
|
|
'stripe_account' => 'Stripe-Account', |
153
|
|
|
'stripe_version' => 'Stripe-Version', |
154
|
|
|
]; |
155
|
|
|
|
156
|
34 |
|
foreach ($keys as $keyFrom => $keyTo) { |
157
|
34 |
|
if (isset($options[$keyFrom])) |
158
|
34 |
|
$headers[$keyTo] = $options[$keyFrom]; |
159
|
|
|
} |
160
|
|
|
|
161
|
34 |
|
return $headers; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/* ------------------------------------------------------------------------------------------------ |
165
|
|
|
| Check Functions |
166
|
|
|
| ------------------------------------------------------------------------------------------------ |
167
|
|
|
*/ |
168
|
|
|
/** |
169
|
|
|
* Check if API exists. |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
4 |
|
public function hasApiKey() |
174
|
|
|
{ |
175
|
4 |
|
return ! is_null($this->apiKey) && ! empty($this->apiKey); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check Options. |
180
|
|
|
* |
181
|
|
|
* @param array|string|null $options |
182
|
|
|
* |
183
|
|
|
* @throws \Arcanedev\Stripe\Exceptions\ApiException |
184
|
|
|
*/ |
185
|
412 |
|
private static function checkOptions($options) |
186
|
|
|
{ |
187
|
|
|
if ( |
188
|
412 |
|
! ($options instanceof self) && |
189
|
412 |
|
! is_null($options) && |
190
|
412 |
|
! is_string($options) && |
191
|
412 |
|
! is_array($options) |
192
|
|
|
) { |
193
|
2 |
|
throw new ApiException( |
194
|
2 |
|
'Options must be a string, an array, or null, '.gettype($options).' given.' |
195
|
|
|
); |
196
|
|
|
} |
197
|
410 |
|
} |
198
|
|
|
} |
199
|
|
|
|