| 1 |
<?php |
| 2 |
|
| 3 |
namespace Stripe\Util; |
| 4 |
|
| 5 |
class RequestOptions |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var array<string> a list of headers that should be persisted across requests |
| 9 |
*/ |
| 10 |
public static $HEADERS_TO_PERSIST = [ |
| 11 |
'Stripe-Account', |
| 12 |
'Stripe-Version', |
| 13 |
]; |
| 14 |
|
| 15 |
/** @var array<string, string> */ |
| 16 |
public $headers; |
| 17 |
|
| 18 |
/** @var null|string */ |
| 19 |
public $apiKey; |
| 20 |
|
| 21 |
/** @var null|string */ |
| 22 |
public $apiBase; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param null|string $key |
| 26 |
* @param array<string, string> $headers |
| 27 |
* @param null|string $base |
| 28 |
*/ |
| 29 |
public function __construct($key = null, $headers = [], $base = null) |
| 30 |
{ |
| 31 |
$this->apiKey = $key; |
| 32 |
$this->headers = $headers; |
| 33 |
$this->apiBase = $base; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return array<string, string> |
| 38 |
*/ |
| 39 |
public function __debugInfo() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
'apiKey' => $this->redactedApiKey(), |
| 43 |
'headers' => $this->headers, |
| 44 |
'apiBase' => $this->apiBase, |
| 45 |
]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Unpacks an options array and merges it into the existing RequestOptions |
| 50 |
* object. |
| 51 |
* |
| 52 |
* @param null|array|RequestOptions|string $options a key => value array |
| 53 |
* @param bool $strict when true, forbid string form and arbitrary keys in array form |
| 54 |
* |
| 55 |
* @return RequestOptions |
| 56 |
*/ |
| 57 |
public function merge($options, $strict = false) |
| 58 |
{ |
| 59 |
$other_options = self::parse($options, $strict); |
| 60 |
if (null === $other_options->apiKey) { |
| 61 |
$other_options->apiKey = $this->apiKey; |
| 62 |
} |
| 63 |
if (null === $other_options->apiBase) { |
| 64 |
$other_options->apiBase = $this->apiBase; |
| 65 |
} |
| 66 |
$other_options->headers = \array_merge($this->headers, $other_options->headers); |
| 67 |
|
| 68 |
return $other_options; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Discards all headers that we don't want to persist across requests. |
| 73 |
*/ |
| 74 |
public function discardNonPersistentHeaders() |
| 75 |
{ |
| 76 |
foreach ($this->headers as $k => $v) { |
| 77 |
if (!\in_array($k, self::$HEADERS_TO_PERSIST, true)) { |
| 78 |
unset($this->headers[$k]); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Unpacks an options array into an RequestOptions object. |
| 85 |
* |
| 86 |
* @param null|array|RequestOptions|string $options a key => value array |
| 87 |
* @param bool $strict when true, forbid string form and arbitrary keys in array form |
| 88 |
* |
| 89 |
* @throws \Stripe\Exception\InvalidArgumentException |
| 90 |
* |
| 91 |
* @return RequestOptions |
| 92 |
*/ |
| 93 |
public static function parse($options, $strict = false) |
| 94 |
{ |
| 95 |
if ($options instanceof self) { |
| 96 |
return clone $options; |
| 97 |
} |
| 98 |
|
| 99 |
if (null === $options) { |
| 100 |
return new RequestOptions(null, [], null); |
| 101 |
} |
| 102 |
|
| 103 |
if (\is_string($options)) { |
| 104 |
if ($strict) { |
| 105 |
$message = 'Do not pass a string for request options. If you want to set the ' |
| 106 |
. 'API key, pass an array like ["api_key" => <apiKey>] instead.'; |
| 107 |
|
| 108 |
throw new \Stripe\Exception\InvalidArgumentException($message); |
| 109 |
} |
| 110 |
|
| 111 |
return new RequestOptions($options, [], null); |
| 112 |
} |
| 113 |
|
| 114 |
if (\is_array($options)) { |
| 115 |
$headers = []; |
| 116 |
$key = null; |
| 117 |
$base = null; |
| 118 |
|
| 119 |
if (\array_key_exists('api_key', $options)) { |
| 120 |
$key = $options['api_key']; |
| 121 |
unset($options['api_key']); |
| 122 |
} |
| 123 |
if (\array_key_exists('idempotency_key', $options)) { |
| 124 |
$headers['Idempotency-Key'] = $options['idempotency_key']; |
| 125 |
unset($options['idempotency_key']); |
| 126 |
} |
| 127 |
if (\array_key_exists('stripe_account', $options)) { |
| 128 |
$headers['Stripe-Account'] = $options['stripe_account']; |
| 129 |
unset($options['stripe_account']); |
| 130 |
} |
| 131 |
if (\array_key_exists('stripe_version', $options)) { |
| 132 |
$headers['Stripe-Version'] = $options['stripe_version']; |
| 133 |
unset($options['stripe_version']); |
| 134 |
} |
| 135 |
if (\array_key_exists('api_base', $options)) { |
| 136 |
$base = $options['api_base']; |
| 137 |
unset($options['api_base']); |
| 138 |
} |
| 139 |
|
| 140 |
if ($strict && !empty($options)) { |
| 141 |
$message = 'Got unexpected keys in options array: ' . \implode(', ', \array_keys($options)); |
| 142 |
|
| 143 |
throw new \Stripe\Exception\InvalidArgumentException($message); |
| 144 |
} |
| 145 |
|
| 146 |
return new RequestOptions($key, $headers, $base); |
| 147 |
} |
| 148 |
|
| 149 |
$message = 'The second argument to Stripe API method calls is an ' |
| 150 |
. 'optional per-request apiKey, which must be a string, or ' |
| 151 |
. 'per-request options, which must be an array. (HINT: you can set ' |
| 152 |
. 'a global apiKey by "Stripe::setApiKey(<apiKey>)")'; |
| 153 |
|
| 154 |
throw new \Stripe\Exception\InvalidArgumentException($message); |
| 155 |
} |
| 156 |
|
| 157 |
private function redactedApiKey() |
| 158 |
{ |
| 159 |
$pieces = \explode('_', $this->apiKey, 3); |
| 160 |
$last = \array_pop($pieces); |
| 161 |
$redactedLast = \strlen($last) > 4 |
| 162 |
? (\str_repeat('*', \strlen($last) - 4) . \substr($last, -4)) |
| 163 |
: $last; |
| 164 |
$pieces[] = $redactedLast; |
| 165 |
|
| 166 |
return \implode('_', $pieces); |
| 167 |
} |
| 168 |
} |
| 169 |
|