| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,119 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+namespace Telnyx\ApiOperations; |
|
| 4 |
+ |
|
| 5 |
+/** |
|
| 6 |
+ * Trait for resources that have nested resources. |
|
| 7 |
+ * |
|
| 8 |
+ * This trait should only be applied to classes that derive from TelnyxObject. |
|
| 9 |
+ */ |
|
| 10 |
+trait NestedResource |
|
| 11 |
+{
|
|
| 12 |
+ /** |
|
| 13 |
+ * @param string $method |
|
| 14 |
+ * @param string $url |
|
| 15 |
+ * @param array|null $params |
|
| 16 |
+ * @param array|string|null $options |
|
| 17 |
+ * |
|
| 18 |
+ * @return \Telnyx\TelnyxObject |
|
| 19 |
+ */ |
|
| 20 |
+ protected static function _nestedResourceOperation($method, $url, $params = null, $options = null) |
|
| 21 |
+ {
|
|
| 22 |
+ self::_validateParams($params); |
|
| 23 |
+ |
|
| 24 |
+ list($response, $opts) = static::_staticRequest($method, $url, $params, $options); |
|
| 25 |
+ $obj = \Telnyx\Util\Util::convertToTelnyxObject($response->json, $opts); |
|
| 26 |
+ print_r($obj); |
|
| 27 |
+ $obj->setLastResponse($response); |
|
| 28 |
+ return $obj; |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ /** |
|
| 32 |
+ * @param string $id |
|
| 33 |
+ * @param string $nestedPath |
|
| 34 |
+ * @param string|null $nestedId |
|
| 35 |
+ * |
|
| 36 |
+ * @return string |
|
| 37 |
+ */ |
|
| 38 |
+ protected static function _nestedResourceUrl($id, $nestedPath, $nestedId = null) |
|
| 39 |
+ {
|
|
| 40 |
+ $url = static::resourceUrl($id) . $nestedPath; |
|
| 41 |
+ if ($nestedId !== null) {
|
|
| 42 |
+ $url .= "/$nestedId"; |
|
| 43 |
+ } |
|
| 44 |
+ return $url; |
|
| 45 |
+ } |
|
| 46 |
+ |
|
| 47 |
+ /** |
|
| 48 |
+ * @param string $id |
|
| 49 |
+ * @param string $nestedPath |
|
| 50 |
+ * @param array|null $params |
|
| 51 |
+ * @param array|string|null $options |
|
| 52 |
+ * |
|
| 53 |
+ * @return \Telnyx\TelnyxObject |
|
| 54 |
+ */ |
|
| 55 |
+ protected static function _createNestedResource($id, $nestedPath, $params = null, $options = null) |
|
| 56 |
+ {
|
|
| 57 |
+ $url = static::_nestedResourceUrl($id, $nestedPath); |
|
| 58 |
+ return self::_nestedResourceOperation('post', $url, $params, $options);
|
|
| 59 |
+ } |
|
| 60 |
+ |
|
| 61 |
+ /** |
|
| 62 |
+ * @param string $id |
|
| 63 |
+ * @param string $nestedPath |
|
| 64 |
+ * @param string|null $nestedId |
|
| 65 |
+ * @param array|null $params |
|
| 66 |
+ * @param array|string|null $options |
|
| 67 |
+ * |
|
| 68 |
+ * @return \Telnyx\TelnyxObject |
|
| 69 |
+ */ |
|
| 70 |
+ protected static function _retrieveNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
|
| 71 |
+ {
|
|
| 72 |
+ $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); |
|
| 73 |
+ return self::_nestedResourceOperation('get', $url, $params, $options);
|
|
| 74 |
+ } |
|
| 75 |
+ |
|
| 76 |
+ /** |
|
| 77 |
+ * @param string $id |
|
| 78 |
+ * @param string $nestedPath |
|
| 79 |
+ * @param string|null $nestedId |
|
| 80 |
+ * @param array|null $params |
|
| 81 |
+ * @param array|string|null $options |
|
| 82 |
+ * |
|
| 83 |
+ * @return \Telnyx\TelnyxObject |
|
| 84 |
+ */ |
|
| 85 |
+ protected static function _updateNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
|
| 86 |
+ {
|
|
| 87 |
+ $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); |
|
| 88 |
+ return self::_nestedResourceOperation('post', $url, $params, $options);
|
|
| 89 |
+ } |
|
| 90 |
+ |
|
| 91 |
+ /** |
|
| 92 |
+ * @param string $id |
|
| 93 |
+ * @param string $nestedPath |
|
| 94 |
+ * @param string|null $nestedId |
|
| 95 |
+ * @param array|null $params |
|
| 96 |
+ * @param array|string|null $options |
|
| 97 |
+ * |
|
| 98 |
+ * @return \Telnyx\TelnyxObject |
|
| 99 |
+ */ |
|
| 100 |
+ protected static function _deleteNestedResource($id, $nestedPath, $nestedId, $params = null, $options = null) |
|
| 101 |
+ {
|
|
| 102 |
+ $url = static::_nestedResourceUrl($id, $nestedPath, $nestedId); |
|
| 103 |
+ return self::_nestedResourceOperation('delete', $url, $params, $options);
|
|
| 104 |
+ } |
|
| 105 |
+ |
|
| 106 |
+ /** |
|
| 107 |
+ * @param string $id |
|
| 108 |
+ * @param string $nestedPath |
|
| 109 |
+ * @param array|null $params |
|
| 110 |
+ * @param array|string|null $options |
|
| 111 |
+ * |
|
| 112 |
+ * @return \Telnyx\TelnyxObject |
|
| 113 |
+ */ |
|
| 114 |
+ protected static function _allNestedResources($id, $nestedPath, $params = null, $options = null) |
|
| 115 |
+ {
|
|
| 116 |
+ $url = static::_nestedResourceUrl($id, $nestedPath); |
|
| 117 |
+ return self::_nestedResourceOperation('get', $url, $params, $options);
|
|
| 118 |
+ } |
|
| 119 |
+} |