PluginProbe
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions / 260909
s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions v260909
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 260909, at src/includes/externals/aweber/aweber_collection.php

283 lines 8.9 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 //260816 Suppress PHP 8.1+ tentative ArrayAccess return-type deprecations while preserving PHP 5.6 compatibility.
165 #[\ReturnTypeWillChange]
166 public function offsetSet($offset, $value) {}
167 #[\ReturnTypeWillChange]
168 public function offsetUnset($offset) {}
169 #[\ReturnTypeWillChange]
170 public function offsetExists($offset) {
171
172 if ($offset >=0 && $offset < $this->total_size) {
173 return true;
174 }
175 return false;
176 }
177 protected function _fetchCollectionData($offset) {
178
179 # we dont have a next page, we're done
180 if (!array_key_exists('next_collection_link', $this->data)) {
181 return null;
182 }
183
184 # snag query string args from collection
185 $parsed = parse_url($this->data['next_collection_link']);
186
187 # parse the query string to get params
188 $pairs = explode('&', $parsed['query']);
189 foreach ($pairs as $pair) {
190 list($key, $val) = explode('=', $pair);
191 $params[$key] = $val;
192 }
193
194 # calculate new args
195 $limit = $params['ws.size'];
196 $pagination_offset = intval($offset / $limit) * $limit;
197 $params['ws.start'] = $pagination_offset;
198
199 # fetch data, exclude query string
200 $url_parts = explode('?', $this->url);
201 $data = $this->adapter->request('GET', $url_parts[0], $params);
202 $this->pageStart = $params['ws.start'];
203 $this->pageSize = $params['ws.size'];
204
205 $collection_data = array('entries', 'next_collection_link', 'prev_collection_link', 'ws.start');
206
207 foreach ($collection_data as $item) {
208 if (!array_key_exists($item, $this->data)) {
209 continue;
210 }
211 if (!array_key_exists($item, $data)) {
212 continue;
213 }
214 $this->data[$item] = $data[$item];
215 }
216 }
217
218 #[\ReturnTypeWillChange]
219 public function offsetGet($offset) {
220
221 if (!$this->offsetExists($offset)) {
222 return null;
223 }
224
225 $limit = $this->pageSize;
226 $pagination_offset = intval($offset / $limit) * $limit;
227
228 # load collection page if needed
229 if ($pagination_offset !== $this->pageStart) {
230 $this->_fetchCollectionData($offset);
231 }
232
233 $entry = $this->data['entries'][$offset - $pagination_offset];
234
235 # we have an entry, cast it to an AWeberEntry and return it
236 $entry_url = $this->adapter->app->removeBaseUri($entry['self_link']);
237 return new AWeberEntry($entry, $entry_url, $this->adapter);
238 }
239
240 /*
241 * Iterator
242 */
243 protected $_iterationKey = 0;
244
245 //260816 Suppress PHP 8.1+ tentative Iterator return-type deprecations while preserving PHP 5.6 compatibility.
246 #[\ReturnTypeWillChange]
247 public function current() {
248 return $this->offsetGet($this->_iterationKey);
249 }
250
251 #[\ReturnTypeWillChange]
252 public function key() {
253 return $this->_iterationKey;
254 }
255
256 #[\ReturnTypeWillChange]
257 public function next() {
258 $this->_iterationKey++;
259 }
260
261 #[\ReturnTypeWillChange]
262 public function rewind() {
263 $this->_iterationKey = 0;
264 }
265
266 #[\ReturnTypeWillChange]
267 public function valid() {
268 return $this->offsetExists($this->key());
269 }
270
271 /*
272 * Countable interface methods
273 * Allows PHP's count() and sizeOf() functions to act on this object
274 * http://www.php.net/manual/en/class.countable.php
275 */
276
277 //260816 Suppress PHP 8.1+ tentative Countable return-type deprecation while preserving PHP 5.6 compatibility.
278 #[\ReturnTypeWillChange]
279 public function count() {
280 return $this->total_size;
281 }
282 }
283