| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,107 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * Class Collection |
|
| 7 |
+ * |
|
| 8 |
+ * @property string $object |
|
| 9 |
+ * @property string $url |
|
| 10 |
+ * @property bool $has_more |
|
| 11 |
+ * @property mixed $data |
|
| 12 |
+ * |
|
| 13 |
+ * @package Telnyx |
|
| 14 |
+ */ |
|
| 15 |
+class Collection extends TelnyxObject implements \IteratorAggregate |
|
| 16 |
+{
|
|
| 17 |
+ const OBJECT_NAME = "list"; |
|
| 18 |
+ |
|
| 19 |
+ use ApiOperations\Request; |
|
| 20 |
+ |
|
| 21 |
+ protected $_requestParams = []; |
|
| 22 |
+ |
|
| 23 |
+ /** |
|
| 24 |
+ * @return string The base URL for the given class. |
|
| 25 |
+ */ |
|
| 26 |
+ public static function baseUrl() |
|
| 27 |
+ {
|
|
| 28 |
+ return Telnyx::$apiBase; |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ public function setRequestParams($params) |
|
| 32 |
+ {
|
|
| 33 |
+ $this->_requestParams = $params; |
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ public function all($params = null, $opts = null) |
|
| 37 |
+ {
|
|
| 38 |
+ list($url, $params) = $this->extractPathAndUpdateParams($params); |
|
| 39 |
+ |
|
| 40 |
+ list($response, $opts) = $this->_request('get', $url, $params, $opts);
|
|
| 41 |
+ $this->_requestParams = $params; |
|
| 42 |
+ return Util\Util::convertToTelnyxObject($response, $opts); |
|
| 43 |
+ } |
|
| 44 |
+ |
|
| 45 |
+ public function create($params = null, $opts = null) |
|
| 46 |
+ {
|
|
| 47 |
+ list($url, $params) = $this->extractPathAndUpdateParams($params); |
|
| 48 |
+ |
|
| 49 |
+ list($response, $opts) = $this->_request('post', $url, $params, $opts);
|
|
| 50 |
+ $this->_requestParams = $params; |
|
| 51 |
+ return Util\Util::convertToTelnyxObject($response, $opts); |
|
| 52 |
+ } |
|
| 53 |
+ |
|
| 54 |
+ public function retrieve($id, $params = null, $opts = null) |
|
| 55 |
+ {
|
|
| 56 |
+ list($url, $params) = $this->extractPathAndUpdateParams($params); |
|
| 57 |
+ |
|
| 58 |
+ $id = Util\Util::utf8($id); |
|
| 59 |
+ $extn = urlencode($id); |
|
| 60 |
+ list($response, $opts) = $this->_request( |
|
| 61 |
+ 'get', |
|
| 62 |
+ "$url/$extn", |
|
| 63 |
+ $params, |
|
| 64 |
+ $opts |
|
| 65 |
+ ); |
|
| 66 |
+ $this->_requestParams = $params; |
|
| 67 |
+ return Util\Util::convertToTelnyxObject($response, $opts); |
|
| 68 |
+ } |
|
| 69 |
+ |
|
| 70 |
+ /** |
|
| 71 |
+ * @return \ArrayIterator An iterator that can be used to iterate |
|
| 72 |
+ * across objects in the current page. |
|
| 73 |
+ */ |
|
| 74 |
+ public function getIterator() |
|
| 75 |
+ {
|
|
| 76 |
+ return new \ArrayIterator($this->data); |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ /** |
|
| 80 |
+ * @return Util\AutoPagingIterator An iterator that can be used to iterate |
|
| 81 |
+ * across all objects across all pages. As page boundaries are |
|
| 82 |
+ * encountered, the next page will be fetched automatically for |
|
| 83 |
+ * continued iteration. |
|
| 84 |
+ */ |
|
| 85 |
+ public function autoPagingIterator() |
|
| 86 |
+ {
|
|
| 87 |
+ return new Util\AutoPagingIterator($this, $this->_requestParams); |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ private function extractPathAndUpdateParams($params) |
|
| 91 |
+ {
|
|
| 92 |
+ $url = parse_url($this->url); |
|
| 93 |
+ if (!isset($url['path'])) {
|
|
| 94 |
+ throw new Error\Api("Could not parse list url into parts: $url");
|
|
| 95 |
+ } |
|
| 96 |
+ |
|
| 97 |
+ if (isset($url['query'])) {
|
|
| 98 |
+ // If the URL contains a query param, parse it out into $params so they |
|
| 99 |
+ // don't interact weirdly with each other. |
|
| 100 |
+ $query = []; |
|
| 101 |
+ parse_str($url['query'], $query); |
|
| 102 |
+ $params = array_merge($params ?: [], $query); |
|
| 103 |
+ } |
|
| 104 |
+ |
|
| 105 |
+ return [$url['path'], $params]; |
|
| 106 |
+ } |
|
| 107 |
+} |