vendor/stripe/stripe-php/lib/Util/RequestOptions.php line 10

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