dolibarr/htdocs/includes/stripe/stripe-php/lib/Util/RequestOptions.php

174 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2017-04-18 05:44:08 +02:00
<?php
namespace Stripe\Util;
class RequestOptions
{
2018-03-07 19:25:05 +01:00
/**
2021-01-03 23:37:44 +01:00
* @var array<string> a list of headers that should be persisted across requests
2018-03-07 19:25:05 +01:00
*/
public static $HEADERS_TO_PERSIST = [
'Stripe-Account',
'Stripe-Version',
];
2021-01-03 23:37:44 +01:00
/** @var array<string, string> */
2017-04-18 05:44:08 +02:00
public $headers;
2021-01-03 23:37:44 +01:00
/** @var null|string */
2017-04-18 05:44:08 +02:00
public $apiKey;
2021-01-03 23:37:44 +01:00
/** @var null|string */
2019-05-03 02:22:27 +02:00
public $apiBase;
2017-04-18 05:44:08 +02:00
2021-01-03 23:37:44 +01:00
/**
* @param null|string $key
* @param array<string, string> $headers
* @param null|string $base
*/
2019-05-03 02:22:27 +02:00
public function __construct($key = null, $headers = [], $base = null)
2017-04-18 05:44:08 +02:00
{
$this->apiKey = $key;
$this->headers = $headers;
2019-05-03 02:22:27 +02:00
$this->apiBase = $base;
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
/**
* @return array<string, string>
*/
public function __debugInfo()
{
return [
'apiKey' => $this->redactedApiKey(),
'headers' => $this->headers,
'apiBase' => $this->apiBase,
];
}
2017-04-18 05:44:08 +02:00
/**
* Unpacks an options array and merges it into the existing RequestOptions
* object.
2021-01-03 23:37:44 +01:00
*
* @param null|array|RequestOptions|string $options a key => value array
* @param bool $strict when true, forbid string form and arbitrary keys in array form
2017-04-18 05:44:08 +02:00
*
* @return RequestOptions
*/
2021-01-03 23:37:44 +01:00
public function merge($options, $strict = false)
2017-04-18 05:44:08 +02:00
{
2021-01-03 23:37:44 +01:00
$other_options = self::parse($options, $strict);
if (null === $other_options->apiKey) {
2017-04-18 05:44:08 +02:00
$other_options->apiKey = $this->apiKey;
}
2021-01-03 23:37:44 +01:00
if (null === $other_options->apiBase) {
2019-05-03 02:22:27 +02:00
$other_options->apiBase = $this->apiBase;
}
2021-01-03 23:37:44 +01:00
$other_options->headers = \array_merge($this->headers, $other_options->headers);
2017-04-18 05:44:08 +02:00
return $other_options;
}
2018-03-07 19:25:05 +01:00
/**
* Discards all headers that we don't want to persist across requests.
*/
public function discardNonPersistentHeaders()
{
foreach ($this->headers as $k => $v) {
2021-01-03 23:37:44 +01:00
if (!\in_array($k, self::$HEADERS_TO_PERSIST, true)) {
2018-03-07 19:25:05 +01:00
unset($this->headers[$k]);
}
}
}
2017-04-18 05:44:08 +02:00
/**
2021-01-03 23:37:44 +01:00
* Unpacks an options array into an RequestOptions object.
*
* @param null|array|RequestOptions|string $options a key => value array
* @param bool $strict when true, forbid string form and arbitrary keys in array form
*
* @throws \Stripe\Exception\InvalidArgumentException
2017-04-18 05:44:08 +02:00
*
* @return RequestOptions
*/
2021-01-03 23:37:44 +01:00
public static function parse($options, $strict = false)
2017-04-18 05:44:08 +02:00
{
if ($options instanceof self) {
2023-04-24 18:42:04 +02:00
return clone $options;
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (null === $options) {
2019-05-03 02:22:27 +02:00
return new RequestOptions(null, [], null);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\is_string($options)) {
if ($strict) {
$message = 'Do not pass a string for request options. If you want to set the '
. 'API key, pass an array like ["api_key" => <apiKey>] instead.';
throw new \Stripe\Exception\InvalidArgumentException($message);
}
2019-05-03 02:22:27 +02:00
return new RequestOptions($options, [], null);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\is_array($options)) {
2018-03-07 19:25:05 +01:00
$headers = [];
2017-04-18 05:44:08 +02:00
$key = null;
2019-05-03 02:22:27 +02:00
$base = null;
2021-01-03 23:37:44 +01:00
if (\array_key_exists('api_key', $options)) {
2017-04-18 05:44:08 +02:00
$key = $options['api_key'];
2021-01-03 23:37:44 +01:00
unset($options['api_key']);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\array_key_exists('idempotency_key', $options)) {
2017-04-18 05:44:08 +02:00
$headers['Idempotency-Key'] = $options['idempotency_key'];
2021-01-03 23:37:44 +01:00
unset($options['idempotency_key']);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\array_key_exists('stripe_account', $options)) {
2017-04-18 05:44:08 +02:00
$headers['Stripe-Account'] = $options['stripe_account'];
2021-01-03 23:37:44 +01:00
unset($options['stripe_account']);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\array_key_exists('stripe_version', $options)) {
2017-04-18 05:44:08 +02:00
$headers['Stripe-Version'] = $options['stripe_version'];
2021-01-03 23:37:44 +01:00
unset($options['stripe_version']);
2017-04-18 05:44:08 +02:00
}
2021-01-03 23:37:44 +01:00
if (\array_key_exists('api_base', $options)) {
2019-05-03 02:22:27 +02:00
$base = $options['api_base'];
2021-01-03 23:37:44 +01:00
unset($options['api_base']);
}
if ($strict && !empty($options)) {
$message = 'Got unexpected keys in options array: ' . \implode(', ', \array_keys($options));
throw new \Stripe\Exception\InvalidArgumentException($message);
2019-05-03 02:22:27 +02:00
}
2021-01-03 23:37:44 +01:00
2019-05-03 02:22:27 +02:00
return new RequestOptions($key, $headers, $base);
2017-04-18 05:44:08 +02:00
}
$message = 'The second argument to Stripe API method calls is an '
. 'optional per-request apiKey, which must be a string, or '
. 'per-request options, which must be an array. (HINT: you can set '
. 'a global apiKey by "Stripe::setApiKey(<apiKey>)")';
2021-01-03 23:37:44 +01:00
throw new \Stripe\Exception\InvalidArgumentException($message);
}
2023-04-24 18:42:04 +02:00
/** @return string */
2021-01-03 23:37:44 +01:00
private function redactedApiKey()
{
2023-04-24 18:42:04 +02:00
if (null === $this->apiKey) {
return '';
}
2021-01-03 23:37:44 +01:00
$pieces = \explode('_', $this->apiKey, 3);
$last = \array_pop($pieces);
$redactedLast = \strlen($last) > 4
? (\str_repeat('*', \strlen($last) - 4) . \substr($last, -4))
: $last;
$pieces[] = $redactedLast;
return \implode('_', $pieces);
2017-04-18 05:44:08 +02:00
}
}