aweber_collection.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. class AWeberCollection extends AWeberResponse implements ArrayAccess, Iterator, Countable {
  3. protected $pageSize = 100;
  4. protected $pageStart = 0;
  5. protected function _updatePageSize() {
  6. # grab the url, or prev and next url and pull ws.size from it
  7. $url = $this->url;
  8. if (array_key_exists('next_collection_link', $this->data)) {
  9. $url = $this->data['next_collection_link'];
  10. } elseif (array_key_exists('prev_collection_link', $this->data)) {
  11. $url = $this->data['prev_collection_link'];
  12. }
  13. # scan querystring for ws_size
  14. $url_parts = parse_url($url);
  15. # we have a query string
  16. if (array_key_exists('query', $url_parts)) {
  17. parse_str($url_parts['query'], $params);
  18. # we have a ws_size
  19. if (array_key_exists('ws_size', $params)) {
  20. # set pageSize
  21. $this->pageSize = $params['ws_size'];
  22. return;
  23. }
  24. }
  25. # we dont have one, just count the # of entries
  26. $this->pageSize = count($this->data['entries']);
  27. }
  28. public function __construct($response, $url, $adapter) {
  29. parent::__construct($response, $url, $adapter);
  30. $this->_updatePageSize();
  31. }
  32. /**
  33. * @var array Holds list of keys that are not publicly accessible
  34. */
  35. protected $_privateData = array(
  36. 'entries',
  37. 'start',
  38. 'next_collection_link',
  39. );
  40. /**
  41. * getById
  42. *
  43. * Gets an entry object of this collection type with the given id
  44. * @param mixed $id ID of the entry you are requesting
  45. * @access public
  46. * @return AWeberEntry
  47. */
  48. public function getById($id) {
  49. $data = $this->adapter->request('GET', "{$this->url}/{$id}");
  50. $url = "{$this->url}/{$id}";
  51. return new AWeberEntry($data, $url, $this->adapter);
  52. }
  53. /** getParentEntry
  54. *
  55. * Gets an entry's parent entry
  56. * Returns NULL if no parent entry
  57. */
  58. public function getParentEntry(){
  59. $url_parts = explode('/', $this->url);
  60. $size = count($url_parts);
  61. # Remove collection id and slash from end of url
  62. $url = substr($this->url, 0, -strlen($url_parts[$size-1])-1);
  63. try {
  64. $data = $this->adapter->request('GET', $url);
  65. return new AWeberEntry($data, $url, $this->adapter);
  66. } catch (Exception $e) {
  67. return NULL;
  68. }
  69. }
  70. /**
  71. * _type
  72. *
  73. * Interpret what type of resources are held in this collection by
  74. * analyzing the URL
  75. *
  76. * @access protected
  77. * @return void
  78. */
  79. protected function _type() {
  80. $urlParts = explode('/', $this->url);
  81. $type = array_pop($urlParts);
  82. return $type;
  83. }
  84. /**
  85. * create
  86. *
  87. * Invoke the API method to CREATE a new entry resource.
  88. *
  89. * Note: Not all entry resources are eligible to be created, please
  90. * refer to the AWeber API Reference Documentation at
  91. * https://labs.aweber.com/docs/reference/1.0 for more
  92. * details on which entry resources may be created and what
  93. * attributes are required for creating resources.
  94. *
  95. * @access public
  96. * @param params mixed associtative array of key/value pairs.
  97. * @return AWeberEntry(Resource) The new resource created
  98. */
  99. public function create($kv_pairs) {
  100. # Create Resource
  101. $params = array_merge(array('ws.op' => 'create'), $kv_pairs);
  102. $data = $this->adapter->request('POST', $this->url, $params, array('return' => 'headers'));
  103. # Return new Resource
  104. $url = $data['Location'];
  105. $resource_data = $this->adapter->request('GET', $url);
  106. return new AWeberEntry($resource_data, $url, $this->adapter);
  107. }
  108. /**
  109. * find
  110. *
  111. * Invoke the API 'find' operation on a collection to return a subset
  112. * of that collection. Not all collections support the 'find' operation.
  113. * refer to https://labs.aweber.com/docs/reference/1.0 for more information.
  114. *
  115. * @param mixed $search_data Associative array of key/value pairs used as search filters
  116. * * refer to https://labs.aweber.com/docs/reference/1.0 for a
  117. * complete list of valid search filters.
  118. * * filtering on attributes that require additional permissions to
  119. * display requires an app authorized with those additional permissions.
  120. * @access public
  121. * @return AWeberCollection
  122. */
  123. public function find($search_data) {
  124. # invoke find operation
  125. $params = array_merge($search_data, array('ws.op' => 'find'));
  126. $data = $this->adapter->request('GET', $this->url, $params);
  127. # get total size
  128. $ts_params = array_merge($params, array('ws.show' => 'total_size'));
  129. $total_size = $this->adapter->request('GET', $this->url, $ts_params, array('return' => 'integer'));
  130. $data['total_size'] = $total_size;
  131. # return collection
  132. return $this->readResponse($data, $this->url);
  133. }
  134. /*
  135. * ArrayAccess Functions
  136. *
  137. * Allows this object to be accessed via bracket notation (ie $obj[$x])
  138. * http://php.net/manual/en/class.arrayaccess.php
  139. */
  140. public function offsetSet($offset, $value) {}
  141. public function offsetUnset($offset) {}
  142. public function offsetExists($offset) {
  143. if ($offset >=0 && $offset < $this->total_size) {
  144. return true;
  145. }
  146. return false;
  147. }
  148. protected function _fetchCollectionData($offset) {
  149. # we dont have a next page, we're done
  150. if (!array_key_exists('next_collection_link', $this->data)) {
  151. return null;
  152. }
  153. # snag query string args from collection
  154. $parsed = parse_url($this->data['next_collection_link']);
  155. # parse the query string to get params
  156. $pairs = explode('&', $parsed['query']);
  157. foreach ($pairs as $pair) {
  158. list($key, $val) = explode('=', $pair);
  159. $params[$key] = $val;
  160. }
  161. # calculate new args
  162. $limit = $params['ws.size'];
  163. $pagination_offset = intval($offset / $limit) * $limit;
  164. $params['ws.start'] = $pagination_offset;
  165. # fetch data, exclude query string
  166. $url_parts = explode('?', $this->url);
  167. $data = $this->adapter->request('GET', $url_parts[0], $params);
  168. $this->pageStart = $params['ws.start'];
  169. $this->pageSize = $params['ws.size'];
  170. $collection_data = array('entries', 'next_collection_link', 'prev_collection_link', 'ws.start');
  171. foreach ($collection_data as $item) {
  172. if (!array_key_exists($item, $this->data)) {
  173. continue;
  174. }
  175. if (!array_key_exists($item, $data)) {
  176. continue;
  177. }
  178. $this->data[$item] = $data[$item];
  179. }
  180. }
  181. public function offsetGet($offset) {
  182. if (!$this->offsetExists($offset)) {
  183. return null;
  184. }
  185. $limit = $this->pageSize;
  186. $pagination_offset = intval($offset / $limit) * $limit;
  187. # load collection page if needed
  188. if ($pagination_offset !== $this->pageStart) {
  189. $this->_fetchCollectionData($offset);
  190. }
  191. $entry = $this->data['entries'][$offset - $pagination_offset];
  192. # we have an entry, cast it to an AWeberEntry and return it
  193. $entry_url = $this->adapter->app->removeBaseUri($entry['self_link']);
  194. return new AWeberEntry($entry, $entry_url, $this->adapter);
  195. }
  196. /*
  197. * Iterator
  198. */
  199. protected $_iterationKey = 0;
  200. public function current() {
  201. return $this->offsetGet($this->_iterationKey);
  202. }
  203. public function key() {
  204. return $this->_iterationKey;
  205. }
  206. public function next() {
  207. $this->_iterationKey++;
  208. }
  209. public function rewind() {
  210. $this->_iterationKey = 0;
  211. }
  212. public function valid() {
  213. return $this->offsetExists($this->key());
  214. }
  215. /*
  216. * Countable interface methods
  217. * Allows PHP's count() and sizeOf() functions to act on this object
  218. * http://www.php.net/manual/en/class.countable.php
  219. */
  220. public function count() {
  221. return $this->total_size;
  222. }
  223. }