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,234 @@
1
+<?php
2
+
3
+namespace Telnyx;
4
+
5
+/**
6
+ * Class Collection
7
+ *
8
+ * @property string $object
9
+ * @property string $url
10
+ * @property mixed $data
11
+ *
12
+ * @package Telnyx
13
+ */
14
+class Collection extends TelnyxObject implements \IteratorAggregate
15
+{
16
+    const OBJECT_NAME = "list";
17
+
18
+    use ApiOperations\Request;
19
+
20
+    protected $_requestParams = [];
21
+
22
+    /**
23
+     * @return string The base URL for the given class.
24
+     */
25
+    public static function baseUrl()
26
+    {
27
+        return Telnyx::$apiBase;
28
+    }
29
+
30
+    public function setRequestParams($params)
31
+    {
32
+        $this->_requestParams = $params;
33
+    }
34
+
35
+    public function all($params = null, $opts = null)
36
+    {
37
+        list($url, $params) = $this->extractPathAndUpdateParams($params);
38
+
39
+        list($response, $opts) = $this->_request('get', $url, $params, $opts);
40
+        $this->_requestParams = $params;
41
+
42
+        // This is needed for nextPage() and previousPage()
43
+        $response['url'] = $url;
44
+
45
+        $obj = Util\Util::convertToTelnyxObject($response, $opts);
46
+        $obj->setRequestParams($params);
47
+
48
+        return $obj;
49
+    }
50
+
51
+    public function create($params = null, $opts = null)
52
+    {
53
+        list($url, $params) = $this->extractPathAndUpdateParams($params);
54
+
55
+        list($response, $opts) = $this->_request('post', $url, $params, $opts);
56
+        $this->_requestParams = $params;
57
+        return Util\Util::convertToTelnyxObject($response, $opts);
58
+    }
59
+
60
+    public function retrieve($id, $params = null, $opts = null)
61
+    {
62
+        list($url, $params) = $this->extractPathAndUpdateParams($params);
63
+
64
+        $id = Util\Util::utf8($id);
65
+        $extn = urlencode($id);
66
+        list($response, $opts) = $this->_request(
67
+            'get',
68
+            "$url/$extn",
69
+            $params,
70
+            $opts
71
+        );
72
+        $this->_requestParams = $params;
73
+        return Util\Util::convertToTelnyxObject($response, $opts);
74
+    }
75
+
76
+    /**
77
+     * @return \ArrayIterator An iterator that can be used to iterate
78
+     *    across objects in the current page.
79
+     */
80
+    public function getIterator()
81
+    {
82
+        return new \ArrayIterator($this->data);
83
+    }
84
+
85
+    /**
86
+     * @return Util\AutoPagingIterator An iterator that can be used to iterate
87
+     *    across all objects across all pages. As page boundaries are
88
+     *    encountered, the next page will be fetched automatically for
89
+     *    continued iteration.
90
+     */
91
+    public function autoPagingIterator()
92
+    {
93
+        return new Util\AutoPagingIterator($this, $this->_requestParams);
94
+    }
95
+
96
+    /**
97
+     * Returns an empty collection. This is returned from {@see nextPage()}
98
+     * when we know that there isn't a next page in order to replicate the
99
+     * behavior of the API when it attempts to return a page beyond the last.
100
+     *
101
+     * @param array|string|null $opts
102
+     * @return Collection
103
+     */
104
+    public static function emptyCollection($opts = null)
105
+    {
106
+        return Collection::constructFrom(['data' => []], $opts);
107
+    }
108
+
109
+    /**
110
+     * Returns true if the page object contains no element.
111
+     *
112
+     * @return boolean
113
+     */
114
+    public function isEmpty()
115
+    {
116
+        return empty($this->data);
117
+    }
118
+
119
+    /**
120
+     * See if there are more results
121
+     *
122
+     * @return boolean
123
+     */
124
+    public function hasMore() {
125
+        if (isset($this->meta)) {
126
+            if ($this->meta['page_number'] < $this->meta['total_pages']) {
127
+                return true;
128
+            }
129
+        }
130
+        return false;
131
+    }
132
+
133
+    /**
134
+     * See if there are previous results
135
+     *
136
+     * @return boolean
137
+     */
138
+    public function hasPrev() {
139
+        if (isset($this->meta)) {
140
+            if ($this->meta['page_number'] > 1) {
141
+                return true;
142
+            }
143
+        }
144
+        return false;
145
+    }
146
+
147
+    /**
148
+     * Fetches the next page in the resource list (if there is one).
149
+     *
150
+     * This method will try to respect the limit of the current page. If none
151
+     * was given, the default limit will be fetched again.
152
+     *
153
+     * @param array|null $params
154
+     * @param array|string|null $opts
155
+     * @return Collection
156
+     */
157
+    public function nextPage($params = null, $opts = null)
158
+    {
159
+        if (!$this->hasMore()) {
160
+            return static::emptyCollection($opts);
161
+        }
162
+
163
+        $params = array_merge(
164
+            $this->_requestParams,
165
+            $params ?: []
166
+        );
167
+
168
+        // Remove page number from the next request. Detect both syntaxes
169
+        if (isset($params['page']) && isset($params['page']['number'])) {
170
+            unset($params['page']['number']);
171
+        }
172
+        elseif (isset($params['page[number]'])) {
173
+            unset($params['page[number]']);
174
+        }
175
+
176
+        // Set a new page number
177
+        $params['page[number]'] = $this->meta['page_number'] + 1;
178
+
179
+        return $this->all($params, $opts);
180
+    }
181
+
182
+    /**
183
+     * Fetches the previous page in the resource list (if there is one).
184
+     *
185
+     * This method will try to respect the limit of the current page. If none
186
+     * was given, the default limit will be fetched again.
187
+     *
188
+     * @param array|null $params
189
+     * @param array|string|null $opts
190
+     * @return Collection
191
+     */
192
+    public function previousPage($params = null, $opts = null)
193
+    {
194
+        if (!$this->hasPrev()) {
195
+            return static::emptyCollection($opts);
196
+        }
197
+
198
+        $params = array_merge(
199
+            $this->_requestParams,
200
+            $params ?: []
201
+        );
202
+
203
+        // Remove page number from the next request. Detect both syntaxes
204
+        if (isset($params['page']) && isset($params['page']['number'])) {
205
+            unset($params['page']['number']);
206
+        }
207
+        elseif (isset($params['page[number]'])) {
208
+            unset($params['page[number]']);
209
+        }
210
+
211
+        // Set a new page number
212
+        $params['page[number]'] = $this->meta['page_number'] - 1;
213
+
214
+        return $this->all($params, $opts);
215
+    }
216
+
217
+    private function extractPathAndUpdateParams($params)
218
+    {
219
+        $url = parse_url($this->url);
220
+        if (!isset($url['path'])) {
221
+            throw new Exception\UnexpectedValueException("Could not parse list url into parts: {$url}");
222
+        }
223
+
224
+        if (isset($url['query'])) {
225
+            // If the URL contains a query param, parse it out into $params so they
226
+            // don't interact weirdly with each other.
227
+            $query = [];
228
+            parse_str($url['query'], $query);
229
+            $params = array_merge($params ?: [], $query);
230
+        }
231
+
232
+        return [$url['path'], $params];
233
+    }
234
+}