Browse code

Added README.md appinfo/info.xml appinfo/signature.json lib/Controller/AuthorApiController.php and the providers directory

DoubleBastionAdmin authored on 20/08/2022 16:33:00
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,259 @@
1
+<?php
2
+
3
+namespace Telnyx\Util;
4
+
5
+use Telnyx\TelnyxObject;
6
+
7
+abstract class Util
8
+{
9
+    private static $isMbstringAvailable = null;
10
+    private static $isHashEqualsAvailable = null;
11
+
12
+    /**
13
+     * Whether the provided array (or other) is a list rather than a dictionary.
14
+     * A list is defined as an array for which all the keys are consecutive
15
+     * integers starting at 0. Empty arrays are considered to be lists.
16
+     *
17
+     * @param array|mixed $array
18
+     *
19
+     * @return bool true if the given object is a list
20
+     */
21
+    public static function isList($array)
22
+    {
23
+        if (!\is_array($array)) {
24
+            return false;
25
+        }
26
+        if ($array === []) {
27
+            return true;
28
+        }
29
+        if (\array_keys($array) !== \range(0, \count($array) - 1)) {
30
+            return false;
31
+        }
32
+        
33
+        return true;
34
+    }
35
+
36
+    /**
37
+     * Converts a response from the Telnyx API to the corresponding PHP object.
38
+     *
39
+     * @param array $resp The response from the Telnyx API.
40
+     * @param array $opts
41
+     * @return TelnyxObject|array
42
+     */
43
+    public static function convertToTelnyxObject($resp, $opts)
44
+    {
45
+        $types = \Telnyx\Util\ObjectTypes::mapping;
46
+        if (self::isList($resp)) {
47
+            $mapped = [];
48
+            foreach ($resp as $i) {
49
+                \array_push($mapped, self::convertToTelnyxObject($i, $opts));
50
+            }
51
+
52
+            return $mapped;
53
+        }
54
+        if (\is_array($resp)) {
55
+            if (isset($resp['record_type']) && \is_string($resp['record_type']) && isset($types[$resp['record_type']])) {
56
+                $class = $types[$resp['record_type']];
57
+            } elseif (isset($resp['meta']) || isset($resp['metadata']) || isset($resp['data'][0])) { // Only Collections will have 'meta' and this is how we detect collections
58
+                $class = \Telnyx\Collection::class;
59
+            } else {
60
+                $class = \Telnyx\TelnyxObject::class;
61
+            }
62
+
63
+            return $class::constructFrom($resp, $opts);
64
+        }
65
+
66
+        return $resp;
67
+    }
68
+
69
+    /**
70
+     * @param string|mixed $value A string to UTF8-encode.
71
+     *
72
+     * @return string|mixed The UTF8-encoded string, or the object passed in if
73
+     *    it wasn't a string.
74
+     */
75
+    public static function utf8($value)
76
+    {
77
+        if (self::$isMbstringAvailable === null) {
78
+            self::$isMbstringAvailable = function_exists('mb_detect_encoding');
79
+
80
+            if (!self::$isMbstringAvailable) {
81
+                trigger_error("It looks like the mbstring extension is not enabled. " .
82
+                    "UTF-8 strings will not properly be encoded. Ask your system " .
83
+                    "administrator to enable the mbstring extension, or write to " .
84
+                    "support@telnyx.com if you have any questions.", E_USER_WARNING);
85
+            }
86
+        }
87
+
88
+        if (is_string($value) && self::$isMbstringAvailable && mb_detect_encoding($value, "UTF-8", true) != "UTF-8") {
89
+            return utf8_encode($value);
90
+        } else {
91
+            return $value;
92
+        }
93
+    }
94
+
95
+    /**
96
+     * Compares two strings for equality. The time taken is independent of the
97
+     * number of characters that match.
98
+     *
99
+     * @param string $a one of the strings to compare.
100
+     * @param string $b the other string to compare.
101
+     * @return bool true if the strings are equal, false otherwise.
102
+     */
103
+    public static function secureCompare($a, $b)
104
+    {
105
+        if (self::$isHashEqualsAvailable === null) {
106
+            self::$isHashEqualsAvailable = function_exists('hash_equals');
107
+        }
108
+
109
+        if (self::$isHashEqualsAvailable) {
110
+            return hash_equals($a, $b);
111
+        } else {
112
+            if (strlen($a) != strlen($b)) {
113
+                return false;
114
+            }
115
+
116
+            $result = 0;
117
+            for ($i = 0; $i < strlen($a); $i++) {
118
+                $result |= ord($a[$i]) ^ ord($b[$i]);
119
+            }
120
+            return ($result == 0);
121
+        }
122
+    }
123
+
124
+    /**
125
+     * Recursively goes through an array of parameters. If a parameter is an instance of
126
+     * ApiResource, then it is replaced by the resource's ID.
127
+     * Also clears out null values.
128
+     *
129
+     * @param mixed $h
130
+     * @return mixed
131
+     */
132
+    public static function objectsToIds($h)
133
+    {
134
+        if ($h instanceof \Telnyx\ApiResource) {
135
+            return $h->id;
136
+        } elseif (static::isList($h)) {
137
+            $results = [];
138
+            foreach ($h as $v) {
139
+                array_push($results, static::objectsToIds($v));
140
+            }
141
+            return $results;
142
+        } elseif (is_array($h)) {
143
+            $results = [];
144
+            foreach ($h as $k => $v) {
145
+                if (is_null($v)) {
146
+                    continue;
147
+                }
148
+                $results[$k] = static::objectsToIds($v);
149
+            }
150
+            return $results;
151
+        } else {
152
+            return $h;
153
+        }
154
+    }
155
+
156
+    /**
157
+     * @param array $params
158
+     *
159
+     * @return string
160
+     */
161
+    public static function encodeParameters($params)
162
+    {
163
+        $flattenedParams = self::flattenParams($params);
164
+        $pieces = [];
165
+        foreach ($flattenedParams as $param) {
166
+            list($k, $v) = $param;
167
+            array_push($pieces, self::urlEncode($k) . '=' . self::urlEncode($v));
168
+        }
169
+        return implode('&', $pieces);
170
+    }
171
+
172
+    /**
173
+     * @param array $params
174
+     * @param string|null $parentKey
175
+     *
176
+     * @return array
177
+     */
178
+    public static function flattenParams($params, $parentKey = null)
179
+    {
180
+        $result = [];
181
+
182
+        foreach ($params as $key => $value) {
183
+            $calculatedKey = $parentKey ? "{$parentKey}[{$key}]" : $key;
184
+
185
+            if (self::isList($value)) {
186
+                $result = array_merge($result, self::flattenParamsList($value, $calculatedKey));
187
+            } elseif (is_array($value)) {
188
+                $result = array_merge($result, self::flattenParams($value, $calculatedKey));
189
+            } else {
190
+                array_push($result, [$calculatedKey, $value]);
191
+            }
192
+        }
193
+
194
+        return $result;
195
+    }
196
+
197
+    /**
198
+     * @param array $value
199
+     * @param string $calculatedKey
200
+     *
201
+     * @return array
202
+     */
203
+    public static function flattenParamsList($value, $calculatedKey)
204
+    {
205
+        $result = [];
206
+
207
+        foreach ($value as $i => $elem) {
208
+            if (self::isList($elem)) {
209
+                $result = array_merge($result, self::flattenParamsList($elem, $calculatedKey));
210
+            } elseif (is_array($elem)) {
211
+                $result = array_merge($result, self::flattenParams($elem, "{$calculatedKey}[{$i}]"));
212
+            } else {
213
+                array_push($result, ["{$calculatedKey}[{$i}]", $elem]);
214
+            }
215
+        }
216
+
217
+        return $result;
218
+    }
219
+
220
+    /**
221
+     * @param string $key A string to URL-encode.
222
+     *
223
+     * @return string The URL-encoded string.
224
+     */
225
+    public static function urlEncode($key)
226
+    {
227
+        $s = urlencode($key);
228
+
229
+        // Don't use strict form encoding by changing the square bracket control
230
+        // characters back to their literals. This is fine by the server, and
231
+        // makes these parameter strings easier to read.
232
+        $s = str_replace('%5B', '[', $s);
233
+        $s = str_replace('%5D', ']', $s);
234
+
235
+        return $s;
236
+    }
237
+
238
+    public static function normalizeId($id)
239
+    {
240
+        if (is_array($id)) {
241
+            $params = $id;
242
+            $id = $params['id'];
243
+            unset($params['id']);
244
+        } else {
245
+            $params = [];
246
+        }
247
+        return [$id, $params];
248
+    }
249
+
250
+    /**
251
+     * Returns UNIX timestamp in milliseconds
252
+     *
253
+     * @return integer current time in millis
254
+     */
255
+    public static function currentTimeMillis()
256
+    {
257
+        return (int) round(microtime(true) * 1000);
258
+    }
259
+}