PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/premise/lib/aweber_api/aweber_collection.php

https://gitlab.com/iamgraeme/royalmile
PHP | 297 lines | 130 code | 29 blank | 138 comment | 12 complexity | 10cf7740ca0ec0aaf13782f0c1889b30 MD5 | raw file
  1. <?php
  2. class AWeberCollection extends AWeberResponse implements ArrayAccess, Iterator, Countable {
  3. protected $pageSize = 100;
  4. protected $_entries = array();
  5. /**
  6. * @var array Holds list of keys that are not publicly accessible
  7. */
  8. protected $_privateData = array(
  9. 'entries',
  10. 'start',
  11. 'next_collection_link',
  12. );
  13. /**
  14. * getById
  15. *
  16. * Gets an entry object of this collection type with the given id
  17. * @param mixed $id ID of the entry you are requesting
  18. * @access public
  19. * @return AWeberEntry
  20. */
  21. public function getById($id) {
  22. $data = $this->adapter->request('GET', "{$this->url}/{$id}");
  23. return $this->_makeEntry($data, $id, "{$this->url}/{$id}");
  24. }
  25. /**
  26. * create
  27. *
  28. * Invoke the API method to CREATE a new entry resource.
  29. *
  30. * Note: Not all entry resources are eligible to be created, please
  31. * refer to the AWeber API Reference Documentation at
  32. * https://labs.aweber.com/docs/reference/1.0 for more
  33. * details on which entry resources may be created and what
  34. * attributes are required for creating resources.
  35. *
  36. * @access public
  37. * @param params mixed associtative array of key/value pairs.
  38. * @return AWeberEntry(Resource) The new resource created
  39. */
  40. public function create($kv_pairs) {
  41. # Create Resource
  42. $params = array_merge(array('ws.op' => 'create'), $kv_pairs);
  43. $data = $this->adapter->request('POST', $this->url, $params, array('return' => 'headers'));
  44. $this->_entries = array();
  45. # Return new Resource
  46. $url = $data['Location'];
  47. $resource_data = $this->adapter->request('GET', $url);
  48. return new AWeberEntry($resource_data, $url, $this->adapter);
  49. }
  50. /**
  51. * find
  52. *
  53. * Invoke the API 'find' operation on a collection to return a subset
  54. * of that collection. Not all collections support the 'find' operation.
  55. * refer to https://labs.aweber.com/docs/reference/1.0 for more information.
  56. *
  57. * @param mixed $search_data Associative array of key/value pairs used as search filters
  58. * * refer to https://labs.aweber.com/docs/reference/1.0 for a
  59. * complete list of valid search filters.
  60. * * filtering on attributes that require additional permissions to
  61. * display requires an app authorized with those additional permissions.
  62. * @access public
  63. * @return AWeberCollection
  64. */
  65. public function find($search_data) {
  66. # invoke find operation
  67. $params = array_merge($search_data, array('ws.op' => 'find'));
  68. $data = $this->adapter->request('GET', $this->url, $params);
  69. # get total size
  70. $ts_params = array_merge($params, array('ws.show' => 'total_size'));
  71. $total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer'));
  72. $data['total_size'] = $total_size;
  73. # return collection
  74. return $this->readResponse($data, $this->url);
  75. }
  76. /** getParentEntry
  77. *
  78. * Gets an entry's parent entry
  79. * Returns NULL if no parent entry
  80. */
  81. public function getParentEntry(){
  82. $url_parts = explode('/', $this->url);
  83. $size = count($url_parts);
  84. #Remove collection id and slash from end of url
  85. $url = substr($this->url, 0, -strlen($url_parts[$size-1])-1);
  86. try {
  87. $data = $this->adapter->request('GET', $url);
  88. return new AWeberEntry($data, $url, $this->adapter);
  89. } catch (Exception $e) {
  90. return NULL;
  91. }
  92. }
  93. /**
  94. * _getPageParams
  95. *
  96. * Returns an array of GET params used to set the page of a collection
  97. * request
  98. * @param int $start Which entry offset should this page start on?
  99. * @param int $size How many entries should be included in this page?
  100. * @access protected
  101. * @return void
  102. */
  103. protected function _getPageParams($start=0, $size=20) {
  104. if ($start > 0) {
  105. $params = array(
  106. 'ws.start' => $start,
  107. 'ws.size' => $size,
  108. );
  109. ksort($params);
  110. } else {
  111. $params = array();
  112. }
  113. return $params;
  114. }
  115. /**
  116. * _type
  117. *
  118. * Interpret what type of resources are held in this collection by
  119. * analyzing the URL
  120. *
  121. * @access protected
  122. * @return void
  123. */
  124. protected function _type() {
  125. $urlParts = explode('/', $this->url);
  126. $type = array_pop($urlParts);
  127. return $type;
  128. }
  129. /**
  130. * _calculatePageSize
  131. *
  132. * Calculates the page size of this collection based on the data in the
  133. * next and prev links.
  134. *
  135. * @access protected
  136. * @return integer
  137. */
  138. protected function _calculatePageSize() {
  139. if (array_key_exists('next_collection_link', $this->data)) {
  140. $url = $this->data['next_collection_link'];
  141. $urlParts = parse_url($url);
  142. if (empty($urlParts['query'])) return $this->pageSize;
  143. $query = array();
  144. parse_str($urlParts['query'], $query);
  145. if (empty($query['ws_size'])) return $this->pageSize;
  146. $this->pageSize = $query['ws_size'];
  147. }
  148. return $this->pageSize;
  149. }
  150. /**
  151. * _loadPageForOffset
  152. *
  153. * Makes a request for an additional page of entries, based on the given
  154. * offset. Calculates the start / size of the page needed to get that
  155. * offset, requests for it, and then merges the data into it internal
  156. * collection of entry data.
  157. *
  158. * @param mixed $offset The offset requested, 0 to total_size-1
  159. * @access protected
  160. * @return void
  161. */
  162. protected function _loadPageForOffset($offset) {
  163. $this->_calculatePageSize();
  164. $start = round($offset / $this->pageSize) * $this->pageSize;
  165. $params = $this->_getPageParams($start, $this->pageSize);
  166. // Loading page
  167. $data = $this->adapter->request('GET', $this->url, $params);
  168. $this->adapter->debug = false;
  169. $rekeyed = array();
  170. foreach ($data['entries'] as $key => $entry) {
  171. $rekeyed[$key+$data['start']] = $entry;
  172. }
  173. $this->data['entries'] = array_merge($this->data['entries'], $rekeyed);
  174. }
  175. /**
  176. * _getEntry
  177. *
  178. * Makes sure that entry offset's page is loaded, then returns it. Returns
  179. * null if the entry can't be loaded, even after requesting the needed
  180. * page.
  181. *
  182. * @param mixed $offset Offset being requested.
  183. * @access protected
  184. * @return void
  185. */
  186. protected function _getEntry($offset) {
  187. if (empty($this->data['entries'][$offset])) {
  188. $this->_loadPageForOffset($offset);
  189. }
  190. return (empty($this->data['entries'][$offset]))? null :
  191. $this->data['entries'][$offset];
  192. }
  193. /**
  194. * _makeEntry
  195. *
  196. * Creates an entry object from the given entry data. Optionally can take
  197. * the id AND URL of the entry, though that data can be infered from the
  198. * context in which _makeEntry is being called.
  199. *
  200. * @param mixed $data Array of data returned from an API request for
  201. * entry, or as part of the entries array in this collection.
  202. * @param mixed $id ID of the entry. (Optional)
  203. * @param mixed $url URL used to retrieve this entry (Optional)
  204. * @access protected
  205. * @return void
  206. */
  207. protected function _makeEntry($data, $id = false, $url = false) {
  208. if ((!$url) or (!$id)) {
  209. // if either the url or id is omitted, grab the url from the
  210. // self_link of the resource
  211. $url = $this->adapter->app->removeBaseUri($data['self_link']);
  212. } else {
  213. $url = "{$this->url}/{$id}";
  214. }
  215. return new AWeberEntry($data, $url, $this->adapter);
  216. }
  217. /**
  218. * ArrayAccess interface methods
  219. *
  220. * Allows this object to be accessed via bracket notation (ie $obj[$x])
  221. * http://php.net/manual/en/class.arrayaccess.php
  222. */
  223. public function offsetSet($offset, $value) { }
  224. public function offsetUnset($offset) {}
  225. public function offsetExists($offset) {
  226. if ($offset >=0 && $offset < $this->total_size) {
  227. return true;
  228. }
  229. return false;
  230. }
  231. public function offsetGet($offset) {
  232. if (!$this->offsetExists($offset)) return null;
  233. if (!empty($this->_entries[$offset])) return $this->_entries[$offset];
  234. $this->_entries[$offset] = $this->_makeEntry($this->_getEntry($offset));
  235. return $this->_entries[$offset];
  236. }
  237. /**
  238. * Iterator interface methods
  239. *
  240. * Provides iterator functionality.
  241. * http://php.net/manual/en/class.iterator.php
  242. */
  243. protected $_iterationKey = 0;
  244. public function current() {
  245. return $this->offsetGet($this->_iterationKey);
  246. }
  247. public function key() {
  248. return $this->_iterationKey;
  249. }
  250. public function next() {
  251. $this->_iterationKey++;
  252. }
  253. public function rewind() {
  254. $this->_iterationKey = 0;
  255. }
  256. public function valid() {
  257. return $this->offsetExists($this->key());
  258. }
  259. /**
  260. * Countable interface methods
  261. *
  262. * Allows PHP's count() and sizeOf() functions to act on this object
  263. * http://www.php.net/manual/en/class.countable.php
  264. */
  265. public function count() {
  266. return $this->total_size;
  267. }
  268. }