| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,152 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\Util; |
|
| 4 |
+ |
|
| 5 |
+use Telnyx\Exception; |
|
| 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 |
+ 'Telnyx-Account', |
|
| 14 |
+ 'Telnyx-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 |
+ * @return array<string, string> |
|
| 30 |
+ */ |
|
| 31 |
+ public function __debugInfo() |
|
| 32 |
+ {
|
|
| 33 |
+ return [ |
|
| 34 |
+ 'apiKey' => $this->redactedApiKey(), |
|
| 35 |
+ 'headers' => $this->headers, |
|
| 36 |
+ 'apiBase' => $this->apiBase, |
|
| 37 |
+ ]; |
|
| 38 |
+ } |
|
| 39 |
+ |
|
| 40 |
+ /** |
|
| 41 |
+ * Unpacks an options array and merges it into the existing RequestOptions |
|
| 42 |
+ * object. |
|
| 43 |
+ * @param array|string|null $options a key => value array |
|
| 44 |
+ * |
|
| 45 |
+ * @return RequestOptions |
|
| 46 |
+ */ |
|
| 47 |
+ public function merge($options) |
|
| 48 |
+ {
|
|
| 49 |
+ $other_options = self::parse($options); |
|
| 50 |
+ if ($other_options->apiKey === null) {
|
|
| 51 |
+ $other_options->apiKey = $this->apiKey; |
|
| 52 |
+ } |
|
| 53 |
+ if ($other_options->apiBase === null) {
|
|
| 54 |
+ $other_options->apiBase = $this->apiBase; |
|
| 55 |
+ } |
|
| 56 |
+ $other_options->headers = array_merge($this->headers, $other_options->headers); |
|
| 57 |
+ return $other_options; |
|
| 58 |
+ } |
|
| 59 |
+ |
|
| 60 |
+ /** |
|
| 61 |
+ * Discards all headers that we don't want to persist across requests. |
|
| 62 |
+ */ |
|
| 63 |
+ public function discardNonPersistentHeaders() |
|
| 64 |
+ {
|
|
| 65 |
+ foreach ($this->headers as $k => $v) {
|
|
| 66 |
+ if (!in_array($k, self::$HEADERS_TO_PERSIST)) {
|
|
| 67 |
+ unset($this->headers[$k]); |
|
| 68 |
+ } |
|
| 69 |
+ } |
|
| 70 |
+ } |
|
| 71 |
+ |
|
| 72 |
+ /** |
|
| 73 |
+ * Unpacks an options array into an RequestOptions object |
|
| 74 |
+ * @param array|string|null $options a key => value array |
|
| 75 |
+ * |
|
| 76 |
+ * @return RequestOptions |
|
| 77 |
+ */ |
|
| 78 |
+ public static function parse($options, $strict = false) |
|
| 79 |
+ {
|
|
| 80 |
+ if ($options instanceof self) {
|
|
| 81 |
+ return $options; |
|
| 82 |
+ } |
|
| 83 |
+ |
|
| 84 |
+ if (is_null($options)) {
|
|
| 85 |
+ return new RequestOptions(null, [], null); |
|
| 86 |
+ } |
|
| 87 |
+ |
|
| 88 |
+ if (\is_string($options)) {
|
|
| 89 |
+ if ($strict) {
|
|
| 90 |
+ $message = 'Do not pass a string for request options. If you want to set the ' |
|
| 91 |
+ . 'API key, pass an array like ["api_key" => <apiKey>] instead.'; |
|
| 92 |
+ |
|
| 93 |
+ throw new \Telnyx\Exception\InvalidArgumentException($message); |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ return new RequestOptions($options, [], null); |
|
| 97 |
+ } |
|
| 98 |
+ |
|
| 99 |
+ if (\is_array($options)) {
|
|
| 100 |
+ $headers = []; |
|
| 101 |
+ $key = null; |
|
| 102 |
+ $base = null; |
|
| 103 |
+ |
|
| 104 |
+ if (\array_key_exists('api_key', $options)) {
|
|
| 105 |
+ $key = $options['api_key']; |
|
| 106 |
+ unset($options['api_key']); |
|
| 107 |
+ } |
|
| 108 |
+ if (\array_key_exists('idempotency_key', $options)) {
|
|
| 109 |
+ $headers['Idempotency-Key'] = $options['idempotency_key']; |
|
| 110 |
+ unset($options['idempotency_key']); |
|
| 111 |
+ } |
|
| 112 |
+ if (\array_key_exists('telnyx_account', $options)) {
|
|
| 113 |
+ $headers['Telnyx-Account'] = $options['telnyx_account']; |
|
| 114 |
+ unset($options['telnyx_account']); |
|
| 115 |
+ } |
|
| 116 |
+ if (\array_key_exists('telnyx_version', $options)) {
|
|
| 117 |
+ $headers['Telnyx-Version'] = $options['telnyx_version']; |
|
| 118 |
+ unset($options['telnyx_version']); |
|
| 119 |
+ } |
|
| 120 |
+ if (\array_key_exists('api_base', $options)) {
|
|
| 121 |
+ $base = $options['api_base']; |
|
| 122 |
+ unset($options['api_base']); |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ if ($strict && !empty($options)) {
|
|
| 126 |
+ $message = 'Got unexpected keys in options array: ' . \implode(', ', \array_keys($options));
|
|
| 127 |
+ |
|
| 128 |
+ throw new \Telnyx\Exception\InvalidArgumentException($message); |
|
| 129 |
+ } |
|
| 130 |
+ |
|
| 131 |
+ return new RequestOptions($key, $headers, $base); |
|
| 132 |
+ } |
|
| 133 |
+ |
|
| 134 |
+ $message = 'The second argument to Telnyx API method calls is an ' |
|
| 135 |
+ . 'optional per-request apiKey, which must be a string, or ' |
|
| 136 |
+ . 'per-request options, which must be an array. (HINT: you can set ' |
|
| 137 |
+ . 'a global apiKey by "Telnyx::setApiKey(<apiKey>)")'; |
|
| 138 |
+ throw new \Telnyx\Exception\InvalidArgumentException($message); |
|
| 139 |
+ } |
|
| 140 |
+ |
|
| 141 |
+ private function redactedApiKey() |
|
| 142 |
+ {
|
|
| 143 |
+ $pieces = \explode('_', $this->apiKey, 3);
|
|
| 144 |
+ $last = \array_pop($pieces); |
|
| 145 |
+ $redactedLast = \strlen($last) > 4 |
|
| 146 |
+ ? (\str_repeat('*', \strlen($last) - 4) . \substr($last, -4))
|
|
| 147 |
+ : $last; |
|
| 148 |
+ \array_push($pieces, $redactedLast); |
|
| 149 |
+ |
|
| 150 |
+ return \implode('_', $pieces);
|
|
| 151 |
+ } |
|
| 152 |
+} |