AutoPagingIterator.php
6 years ago
CaseInsensitiveArray.php
6 years ago
DefaultLogger.php
6 years ago
LoggerInterface.php
6 years ago
RandomGenerator.php
6 years ago
RequestOptions.php
6 years ago
Set.php
6 years ago
Util.php
6 years ago
RequestOptions.php
109 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Stripe\Util; |
| 4 | |
| 5 | use Stripe\Error; |
| 6 | |
| 7 | class RequestOptions |
| 8 | { |
| 9 | /** |
| 10 | * @var array A list of headers that should be persisted across requests. |
| 11 | */ |
| 12 | public static $HEADERS_TO_PERSIST = [ |
| 13 | 'Stripe-Account', |
| 14 | 'Stripe-Version', |
| 15 | ]; |
| 16 | |
| 17 | public $headers; |
| 18 | public $apiKey; |
| 19 | public $apiBase; |
| 20 | |
| 21 | public function __construct($key = null, $headers = [], $base = null) |
| 22 | { |
| 23 | $this->apiKey = $key; |
| 24 | $this->headers = $headers; |
| 25 | $this->apiBase = $base; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Unpacks an options array and merges it into the existing RequestOptions |
| 30 | * object. |
| 31 | * @param array|string|null $options a key => value array |
| 32 | * |
| 33 | * @return RequestOptions |
| 34 | */ |
| 35 | public function merge($options) |
| 36 | { |
| 37 | $other_options = self::parse($options); |
| 38 | if ($other_options->apiKey === null) { |
| 39 | $other_options->apiKey = $this->apiKey; |
| 40 | } |
| 41 | if ($other_options->apiBase === null) { |
| 42 | $other_options->apiBase = $this->apiBase; |
| 43 | } |
| 44 | $other_options->headers = array_merge($this->headers, $other_options->headers); |
| 45 | return $other_options; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Discards all headers that we don't want to persist across requests. |
| 50 | */ |
| 51 | public function discardNonPersistentHeaders() |
| 52 | { |
| 53 | foreach ($this->headers as $k => $v) { |
| 54 | if (!in_array($k, self::$HEADERS_TO_PERSIST)) { |
| 55 | unset($this->headers[$k]); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Unpacks an options array into an RequestOptions object |
| 62 | * @param array|string|null $options a key => value array |
| 63 | * |
| 64 | * @return RequestOptions |
| 65 | */ |
| 66 | public static function parse($options) |
| 67 | { |
| 68 | if ($options instanceof self) { |
| 69 | return $options; |
| 70 | } |
| 71 | |
| 72 | if (is_null($options)) { |
| 73 | return new RequestOptions(null, [], null); |
| 74 | } |
| 75 | |
| 76 | if (is_string($options)) { |
| 77 | return new RequestOptions($options, [], null); |
| 78 | } |
| 79 | |
| 80 | if (is_array($options)) { |
| 81 | $headers = []; |
| 82 | $key = null; |
| 83 | $base = null; |
| 84 | if (array_key_exists('api_key', $options)) { |
| 85 | $key = $options['api_key']; |
| 86 | } |
| 87 | if (array_key_exists('idempotency_key', $options)) { |
| 88 | $headers['Idempotency-Key'] = $options['idempotency_key']; |
| 89 | } |
| 90 | if (array_key_exists('stripe_account', $options)) { |
| 91 | $headers['Stripe-Account'] = $options['stripe_account']; |
| 92 | } |
| 93 | if (array_key_exists('stripe_version', $options)) { |
| 94 | $headers['Stripe-Version'] = $options['stripe_version']; |
| 95 | } |
| 96 | if (array_key_exists('api_base', $options)) { |
| 97 | $base = $options['api_base']; |
| 98 | } |
| 99 | return new RequestOptions($key, $headers, $base); |
| 100 | } |
| 101 | |
| 102 | $message = 'The second argument to Stripe API method calls is an ' |
| 103 | . 'optional per-request apiKey, which must be a string, or ' |
| 104 | . 'per-request options, which must be an array. (HINT: you can set ' |
| 105 | . 'a global apiKey by "Stripe::setApiKey(<apiKey>)")'; |
| 106 | throw new Error\Api($message); |
| 107 | } |
| 108 | } |
| 109 |