PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260805
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260805
260917 260913 260909 260829 260814 260805 110710 110731 110812 110815 110912 110913 110915 110926 110927 111002 111003 111011 111017 111029 111105 111206 111216 111220 120213 All 189 releases
s2member / src / includes / externals / aweber / aweber_collection.php

aweber_collection.php in s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions 260805, at src/includes/externals/aweber/aweber_collection.php

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