BaseApi.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace OntraportAPI;
  3. /**
  4. * Class BaseApi
  5. *
  6. * @author ONTRAPORT
  7. *
  8. * @package OntraportAPI
  9. */
  10. abstract class BaseApi
  11. {
  12. /**
  13. * @var Ontraport instance
  14. */
  15. protected $client;
  16. /**
  17. * @var string endpoint for all objects retrieve meta functions
  18. */
  19. const META = "meta";
  20. /**
  21. * @var string endpoint for all objects saveorupdate functions
  22. */
  23. const SAVE_OR_UPDATE = "saveorupdate";
  24. /**
  25. * @var string endpoint for all objects getInfo functions
  26. */
  27. const GET_INFO = "getInfo";
  28. /**
  29. * @var string constant for Content-Type header value type JSON-encoded
  30. */
  31. const CONTENT_TYPE_JSON = "json";
  32. /**
  33. * @var string constant for Content-Type header value type form-encoded
  34. */
  35. const CONTENT_TYPE_FORM = "form";
  36. /**
  37. * @var string endpoint for object
  38. */
  39. protected $_endpoint = NULL;
  40. /**
  41. * @var string plural endpoint for object
  42. */
  43. protected $_endpointPlural = NULL;
  44. /**
  45. * @param Ontraport $client
  46. */
  47. public function __construct(Ontraport $client)
  48. {
  49. $this->client = $client;
  50. }
  51. /**
  52. * @brief Retrieve a single specified object
  53. *
  54. * @param mixed[] $requestParams The parameters to submit with GET request.
  55. * Varies by object.
  56. *
  57. * @return string JSON formatted HTTP response or error message
  58. */
  59. protected function _retrieveSingle($requestParams)
  60. {
  61. $requiredParams = array("id");
  62. return $this->client->request($requestParams, $this->_endpoint, "get", $requiredParams, $options = NULL);
  63. }
  64. /**
  65. * @brief Retrieve multiple objects according to specific criteria, handle pagination
  66. *
  67. * @param $requestParams mixed[] Array of parameters to submit with GET request. If "ids"
  68. * are not specified, all will be selected.
  69. * Possible array keys: "objectID" (required),"ids","start","range","sort","sortDir",
  70. * "condition","search","searchNotes","group_ids","performAll",
  71. * "externs","listFields"
  72. *
  73. * @return string JSON formatted array of paginated response data: each page of data will be an element in that array
  74. */
  75. protected function _retrieveMultiplePaginated($requestParams)
  76. {
  77. $collection = json_decode($this->_retrieveCollectionInfo($requestParams), true);
  78. $requestParams["start"] = $requestParams["start"] ?: 0;
  79. $requestParams["range"] = $requestParams["range"] ?: 50;
  80. $object_data = array();
  81. while ($requestParams["start"] < $collection["data"]["count"])
  82. {
  83. $object_data[] = json_decode($this->_retrieveMultiple($requestParams), true);
  84. $requestParams["start"] += $requestParams["range"];
  85. }
  86. return json_encode($object_data);
  87. }
  88. /**
  89. * @brief Retrieve multiple objects according to specific criteria
  90. *
  91. * @param mixed[] $requestParams Array of parameters to submit with GET request.
  92. * Varies by object.
  93. *
  94. * @return string JSON formatted HTTP response or error message.
  95. */
  96. protected function _retrieveMultiple($requestParams)
  97. {
  98. return $this->client->request($requestParams, $this->_endpointPlural, "get", $requiredParams = NULL, $options = NULL);
  99. }
  100. /**
  101. * @brief Create an object
  102. *
  103. * @param mixed[] $requestParams Array of parameters to submit with POST request.
  104. * Varies by object.
  105. *
  106. * @return string JSON formatted HTTP response or error message
  107. */
  108. protected function _create($requestParams)
  109. {
  110. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  111. return $this->client->request($requestParams, $this->_endpointPlural, "post", $requiredParams = NULL, $options);
  112. }
  113. /**
  114. * @brief Delete a single specified object
  115. *
  116. * @param mixed[] $requestParams Array of the parameters to submit with DELETE request.
  117. * Varies by object.
  118. *
  119. * @return string JSON formatted HTTP response or error message
  120. */
  121. protected function _deleteSingle($requestParams)
  122. {
  123. $requiredParams = array("id");
  124. return $this->client->request($requestParams, $this->_endpoint, "delete", $requiredParams, $options = NULL);
  125. }
  126. /**
  127. * @brief Delete multiple objects according to specific criteria
  128. *
  129. * @param mixed[] $requestParams The parameters to submit with DELETE request.
  130. * Varies by object.
  131. *
  132. * @return string JSON formatted HTTP response or error message
  133. */
  134. protected function _deleteMultiple($requestParams)
  135. {
  136. return $this->client->request($requestParams, $this->_endpointPlural, "delete", $requiredParams = NULL, $options = NULL);
  137. }
  138. /**
  139. * @brief Update an object's data
  140. *
  141. * @param mixed[] $requestParams The parameters to submit with PUT request.
  142. * Varies by object.
  143. *
  144. * @return string JSON formatted HTTP response or error message
  145. */
  146. protected function _update($requestParams)
  147. {
  148. $requiredParams = array("id");
  149. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  150. return $this->client->request($requestParams, $this->_endpointPlural, "put", $requiredParams, $options);
  151. }
  152. /**
  153. * @brief Retrieve meta for an object
  154. *
  155. * @param mixed[] $requestParams The parameters to submit with GET request. Ignored if not sent.
  156. *
  157. * @return string JSON formatted meta for an object
  158. */
  159. protected function _retrieveMeta()
  160. {
  161. return $this->client->request($requestParams = NULL, $this->_endpointPlural . "/" . self::META, "get", $requiredParams = NULL, $options = NULL);
  162. }
  163. /**
  164. * @brief Either create an object or merge with an existing object on unique field
  165. *
  166. * @param mixed[] $requestParams The parameters to submit with PUT request.
  167. * Varies by object.
  168. *
  169. * @return string JSON formatted HTTP response or error message
  170. */
  171. protected function _saveOrUpdate($requestParams)
  172. {
  173. $options["headers"] = self::retrieveContentTypeHeader(self::CONTENT_TYPE_FORM);
  174. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::SAVE_OR_UPDATE, "post", $requiredParams = NULL, $options);
  175. }
  176. /**
  177. * @brief Retrieve information (such as number of objects) about a collection
  178. *
  179. * @param mixed[] $requestParams Array of parameters to submit with GET request.
  180. * Varies by object.
  181. *
  182. * @return string JSON formatted HTTP response or error message
  183. */
  184. protected function _retrieveCollectionInfo($requestParams)
  185. {
  186. return $this->client->request($requestParams, $this->_endpointPlural . "/" . self::GET_INFO, "get", $requiredParams = NULL, $options = NULL);
  187. }
  188. protected static function retrieveContentTypeHeader($encoding)
  189. {
  190. if ($encoding == self::CONTENT_TYPE_FORM)
  191. {
  192. return array("Content-Type" => "application/x-www-form-urlencoded");
  193. }
  194. else if ($encoding == self::CONTENT_TYPE_JSON)
  195. {
  196. return array("Content-Type" => "application/json");
  197. }
  198. }
  199. }