1 | <?php namespace Arcanedev\Stripe\Http; |
||
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 = []) |
|
39 | |||
40 | /* ------------------------------------------------------------------------------------------------ |
||
41 | | Getters & Setters |
||
42 | | ------------------------------------------------------------------------------------------------ |
||
43 | */ |
||
44 | /** |
||
45 | * Get API Key. |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | 32 | public function getApiKey() |
|
53 | |||
54 | /** |
||
55 | * Set API Key. |
||
56 | * |
||
57 | * @param string $apiKey |
||
58 | * |
||
59 | * @return self |
||
60 | */ |
||
61 | 520 | public function setApiKey($apiKey) |
|
68 | |||
69 | /** |
||
70 | * Get Headers. |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 14 | public function getHeaders() |
|
78 | |||
79 | /** |
||
80 | * Set Headers. |
||
81 | * |
||
82 | * @param array $headers |
||
83 | * |
||
84 | * @return self |
||
85 | */ |
||
86 | 520 | protected function setHeaders($headers) |
|
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) |
|
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() |
|
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) |
|
198 | } |
||
199 |