| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,108 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\Util; |
|
| 4 |
+ |
|
| 5 |
+use Telnyx\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 |
+ '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 |
+ * 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('telnyx_account', $options)) {
|
|
| 91 |
+ $headers['Telnyx-Account'] = $options['telnyx_account']; |
|
| 92 |
+ } |
|
| 93 |
+ if (array_key_exists('telnyx_version', $options)) {
|
|
| 94 |
+ $headers['Telnyx-Version'] = $options['telnyx_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 Telnyx 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 "Telnyx::setApiKey(<apiKey>)")'; |
|
| 106 |
+ throw new Error\Api($message); |
|
| 107 |
+ } |
|
| 108 |
+} |