1 | <?php namespace Arcanedev\Stripe\Http\Curl; |
||
12 | class HeaderBag implements HeaderBagContract |
||
13 | { |
||
14 | /* ------------------------------------------------------------------------------------------------ |
||
15 | | Properties |
||
16 | | ------------------------------------------------------------------------------------------------ |
||
17 | */ |
||
18 | /** @var array */ |
||
19 | protected $headers = []; |
||
20 | |||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Constructor |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | /** |
||
26 | * Create the HeaderBag instance. |
||
27 | */ |
||
28 | 20 | public function __construct() |
|
32 | |||
33 | /* ------------------------------------------------------------------------------------------------ |
||
34 | | Getters & Setters |
||
35 | | ------------------------------------------------------------------------------------------------ |
||
36 | */ |
||
37 | /** |
||
38 | * Get default headers. |
||
39 | * |
||
40 | * @param string $apiKey |
||
41 | * @param bool $hasFile |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 312 | private function getDefaults($apiKey, $hasFile = false) |
|
46 | { |
||
47 | 312 | $uaString = 'Stripe/v1 PhpBindings/' . Stripe::VERSION; |
|
48 | 312 | $ua = self::getUserAgent(); |
|
49 | 312 | $appInfo = Stripe::getAppInfo(); |
|
50 | |||
51 | 312 | if ( ! empty($appInfo)) { |
|
52 | 298 | $uaString .= ' ' . self::formatAppInfo($appInfo); |
|
53 | 298 | $ua['application'] = $appInfo; |
|
54 | } |
||
55 | |||
56 | $defaults = [ |
||
57 | 312 | 'X-Stripe-Client-User-Agent' => json_encode($ua), |
|
58 | 312 | 'User-Agent' => $uaString, |
|
59 | 312 | 'Authorization' => 'Bearer ' . $apiKey, |
|
60 | 312 | 'Content-Type' => $hasFile ? 'multipart/form-data' : 'application/x-www-form-urlencoded', |
|
61 | 'Expect' => null, |
||
62 | ]; |
||
63 | |||
64 | 312 | if (Stripe::hasApiVersion()) |
|
65 | 312 | $defaults['Stripe-Version'] = Stripe::getApiVersion(); |
|
66 | |||
67 | 312 | if (Stripe::hasAccountId()) |
|
68 | $defaults['Stripe-Account'] = Stripe::getAccountId(); |
||
69 | |||
70 | 312 | return $defaults; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get User Agent. |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | 312 | private static function getUserAgent() |
|
79 | { |
||
80 | 312 | $httplib = 'unknown'; |
|
81 | 312 | $ssllib = 'unknown'; |
|
82 | |||
83 | 312 | if (function_exists('curl_version')) { |
|
84 | 312 | $curlVersion = curl_version(); |
|
85 | 312 | $httplib = 'curl '.$curlVersion['version']; |
|
86 | 312 | $ssllib = $curlVersion['ssl_version']; |
|
87 | } |
||
88 | |||
89 | return [ |
||
90 | 312 | 'bindings_version' => Stripe::VERSION, |
|
91 | 312 | 'lang' => 'php', |
|
92 | 312 | 'lang_version' => phpversion(), |
|
93 | 312 | 'publisher' => 'stripe', |
|
94 | 312 | 'uname' => php_uname(), |
|
95 | 312 | 'httplib' => $httplib, |
|
96 | 312 | 'ssllib' => $ssllib, |
|
97 | ]; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Format the Application's information. |
||
102 | * |
||
103 | * @param array $appInfo |
||
104 | * |
||
105 | * @return string|null |
||
106 | */ |
||
107 | 298 | private static function formatAppInfo(array $appInfo) |
|
119 | |||
120 | /* ------------------------------------------------------------------------------------------------ |
||
121 | | Main functions |
||
122 | | ------------------------------------------------------------------------------------------------ |
||
123 | */ |
||
124 | /** |
||
125 | * Make Header Bag. |
||
126 | * |
||
127 | * @param string $apiKey |
||
128 | * @param array $headers |
||
129 | * @param bool $hasFile |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | 4 | public function make($apiKey, array $headers = [], $hasFile = false) |
|
137 | |||
138 | /** |
||
139 | * Prepare Headers. |
||
140 | * |
||
141 | * @param string $apiKey |
||
142 | * @param array $headers |
||
143 | * @param bool $hasFile |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | 308 | public function prepare($apiKey, array $headers = [], $hasFile = false) |
|
154 | |||
155 | /** |
||
156 | * Get all the headers. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | 308 | public function all() |
|
164 | |||
165 | /** |
||
166 | * Get all headers. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | 304 | public function get() |
|
180 | |||
181 | /** |
||
182 | * Add a Header to collection. |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @param string $value |
||
186 | * |
||
187 | * @return self |
||
188 | */ |
||
189 | 2 | public function set($name, $value) |
|
195 | |||
196 | /** |
||
197 | * Get all headers. |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | 4 | public function toArray() |
|
205 | |||
206 | /** |
||
207 | * Return headers count. |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | 4 | public function count() |
|
215 | } |
||
216 |