Completed
Branch master (9217c1)
by Seth
02:14
created
src/CanvasArray.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -258,7 +258,7 @@
 block discarded – undo
258 258
     /**
259 259
      * Whether an offset exists
260 260
      *
261
-     * @param int|string $offset
261
+     * @param integer $offset
262 262
      *
263 263
      * @return bool
264 264
      *
Please login to merge, or discard this patch.
Indentation   +429 added lines, -429 removed lines patch added patch discarded remove patch
@@ -12,445 +12,445 @@
 block discarded – undo
12 12
  **/
13 13
 class CanvasArray implements \Iterator, \ArrayAccess, \Serializable
14 14
 {
15
-    /** The maximum supported number of responses per page */
16
-    const MAXIMUM_PER_PAGE = 100;
17
-
18
-    /** @var CanvasPest $api Canvas API (for paging through the array) */
19
-    protected $api;
20
-
21
-    /**
22
-     * @var string $endpoint API endpoint whose response is represented by this
23
-     *      object
24
-     **/
25
-    private $endpoint = null;
26
-
27
-    /**
28
-     * @var CanvasPageLink[] $pagination The canonical (first, last, next,
29
-     *      prev, current) pages relative to the current page of responses
30
-     **/
31
-    private $pagination = [];
32
-
33
-    /**
34
-     * @var array Cached pagination per each page response
35
-     */
36
-    private $paginationPerPage = [];
37
-
38
-    /** @var CanvasObject[] $data Backing store */
39
-    private $data = [];
40
-
41
-    /** @var int $page Page number corresponding to current $key */
42
-    private $page = null;
43
-
44
-    /** @var int $key Current key-value of iterator */
45
-    private $key = null;
46
-
47
-    /**
48
-     * Construct a CanvasArray
49
-     *
50
-     * @param string $jsonResponse A JSON-encoded response array from the
51
-     *                             Canvas API
52
-     * @param CanvasPest $canvasPest An API object for making pagination calls
53
-     **/
54
-    public function __construct($jsonResponse, $canvasPest)
55
-    {
56
-        $this->api = $canvasPest;
57
-
58
-        $this->pagination = $this->parsePageLinks();
59
-
60
-        /* locate ourselves */
61
-        if (isset($this->pagination[CanvasPageLink::CURRENT])) {
62
-            $this->page = $this->pagination[CanvasPageLink::CURRENT]->getPageNumber();
63
-        } else {
64
-            $this->page = 1; // assume only one page (since no pagination)
65
-        }
66
-        $this->key = $this->pageNumberToKey($this->page);
67
-        $this->paginationPerPage[$this->page] = $this->pagination;
68
-
69
-        /* parse the JSON response string */
70
-        $key = $this->key;
71
-        foreach (json_decode($jsonResponse, true) as $item) {
72
-            $this->data[$key++] = new CanvasObject($item, $this->api);
73
-        }
74
-    }
75
-
76
-    /**
77
-     * Parse the API response link headers into pagination information
78
-     *
79
-     * @param  boolean|string[] $headers (Optional, defaults to `$this->api->lastHeader('link')`)
80
-     * @return CanvasPageLink[]
81
-     */
82
-    protected function parsePageLinks($headers = false)
83
-    {
84
-        $pagination = [];
85
-        if (!$headers) {
86
-            $headers = $this->api->lastHeader('link');
87
-        }
88
-
89
-        /* parse Canvas page links */
90
-        if (preg_match_all('%<([^>]*)>\s*;\s*rel="([^"]+)"%', $headers, $links, PREG_SET_ORDER)) {
91
-            foreach ($links as $link) {
92
-                $pagination[$link[2]] = new CanvasPageLink($link[1], $link[2]);
93
-            }
94
-        }
95
-
96
-        return $pagination;
97
-    }
98
-    /**
99
-     * Convert a page number to an array key
100
-     *
101
-     * @param int $pageNumber 1-indexed page number
102
-     *
103
-     * @return int
104
-     *
105
-     * @throws CanvasArray_Exception INVALID_PAGE_NUMBER If $pageNumber < 1
106
-     **/
107
-    protected function pageNumberToKey($pageNumber)
108
-    {
109
-        if ($pageNumber < 1) {
110
-            throw new CanvasArray_Exception(
111
-                "{$pageNumber} is not a valid page number",
112
-                CanvasArray_Exception::INVALID_PAGE_NUMBER
113
-            );
114
-        }
115
-        if (isset($this->pagination[CanvasPageLink::CURRENT])) {
116
-            return ($pageNumber - 1) * $this->pagination[CanvasPageLink::CURRENT]->getPerPage();
117
-        } else {
118
-            return 0; // assume only one page (since no pagination);
119
-        }
120
-    }
121
-
122
-    /**
123
-     * Convert an array key to a page number
124
-     *
125
-     * @param int $key Non-negative array key
126
-     *
127
-     * @return int
128
-     *
129
-     * @throws CanvasArray_Exception INVALID_ARRAY_KEY If $key < 0
130
-     **/
131
-    protected function keyToPageNumber($key)
132
-    {
133
-        if ($key < 0) {
134
-            throw new CanvasArray_Exception(
135
-                "$key is not a valid array key",
136
-                CanvasArray_Exception::INVALID_ARRAY_KEY
137
-            );
138
-        }
139
-
140
-        if (isset($this->pagination[CanvasPageLink::CURRENT])) {
141
-            return ((int) ($key / $this->pagination[CanvasPageLink::CURRENT]->getPerPage())) + 1;
142
-        } else {
143
-            return 1; // assume single page if no pagination
144
-        }
145
-    }
146
-
147
-    /**
148
-     * Request a page of responses from the API
149
-     *
150
-     * A page of responses will be requested if it appears that that page has
151
-     * not yet been loaded (tested by checking if the initial element of the
152
-     * page has been initialized in the $data array).
153
-     *
154
-     * @param int $pageNumber Page number to request
155
-     * @param bool $forceRefresh (Optional) Force a refresh of backing data,
156
-     *                           even if cached (defaults to `FALSE`)
157
-     *
158
-     * @return bool `TRUE` if the page is requested, `FALSE` if it is already
159
-     *                     cached (and therefore not requested)
160
-     **/
161
-    protected function requestPageNumber($pageNumber, $forceRefresh = false)
162
-    {
163
-        if (!isset($this->data[$this->pageNumberToKey($pageNumber)]) || ($forceRefresh && isset($this->api))) {
164
-            // assume one page if no pagination (and already loaded)
165
-            if (isset($this->pagination[CanvasPageLink::CURRENT])) {
166
-                $params = $this->pagination[CanvasPageLink::CURRENT]->getParams();
167
-                $params[CanvasPageLink::PARAM_PAGE_NUMBER] = $pageNumber;
168
-                $page = $this->api->get($this->pagination[CanvasPageLink::CURRENT]->getEndpoint(), $params);
169
-                $this->data = array_replace($this->data, $page->data);
170
-                $pagination = $this->parsePageLinks();
171
-                $this->paginationPerPage[$pagination[CanvasPageLink::CURRENT]->getPageNumber()] = $pagination;
172
-                return true;
173
-            }
174
-        }
175
-        return false;
176
-    }
177
-
178
-    /**
179
-     * Request all pages from API
180
-     *
181
-     * This stores the entire API response locally, in preparation for, most
182
-     * likely, serializing this object.
183
-     *
184
-     * @param bool $forceRefresh (Optional) Force a refresh of backing data,
185
-     *                           even if cached (defaults to `FALSE`)
186
-     *
187
-     * @return void
188
-     */
189
-    protected function requestAllPages($forceRefresh = false)
190
-    {
191
-        $_page = $this->page;
192
-        $_key = $this->key;
193
-
194
-        /* first fall-back: just keep going from where we are */
195
-        $nextPageNumber = false;
196
-        if (isset($this->pagination[CanvasPageLink::NEXT])) {
197
-            $nextPageNumber = $this->pagination[CanvasPageLink::NEXT]->getPageNumber();
198
-        }
199
-
200
-        /* best case: start at the beginning and request every page */
201
-        if (isset($this->pagination[CanvasPageLink::FIRST])) {
202
-            $first = $this->pagination[CanvasPageLink::FIRST]->getPageNumber();
203
-            $this->requestPageNumber($first, $forceRefresh);
204
-            if (isset($this->paginationPerPage[$first][CanvasPageLink::NEXT])) {
205
-                $nextPageNumber = $this->paginationPerPage[$first][CanvasPageLink::NEXT]->getPageNumber();
206
-            }
207
-        }
208
-
209
-        /* welp, here goes... let's hope we have a next page! */
210
-        while ($nextPageNumber !== false) {
211
-            $this->requestPageNumber($nextPageNumber, true);
212
-            if (isset($this->paginationPerPage[$nextPageNumber][CanvasPageLink::NEXT])) {
213
-                $nextPageNumber = $this->paginationPerPage[$nextPageNumber][CanvasPageLink::NEXT]->getPageNumber();
214
-            } else {
215
-                $nextPageNumber = false;
216
-            }
217
-        }
218
-
219
-        $this->page = $_page;
220
-        $this->key = $_key;
221
-    }
222
-
223
-    /***************************************************************************
15
+	/** The maximum supported number of responses per page */
16
+	const MAXIMUM_PER_PAGE = 100;
17
+
18
+	/** @var CanvasPest $api Canvas API (for paging through the array) */
19
+	protected $api;
20
+
21
+	/**
22
+	 * @var string $endpoint API endpoint whose response is represented by this
23
+	 *      object
24
+	 **/
25
+	private $endpoint = null;
26
+
27
+	/**
28
+	 * @var CanvasPageLink[] $pagination The canonical (first, last, next,
29
+	 *      prev, current) pages relative to the current page of responses
30
+	 **/
31
+	private $pagination = [];
32
+
33
+	/**
34
+	 * @var array Cached pagination per each page response
35
+	 */
36
+	private $paginationPerPage = [];
37
+
38
+	/** @var CanvasObject[] $data Backing store */
39
+	private $data = [];
40
+
41
+	/** @var int $page Page number corresponding to current $key */
42
+	private $page = null;
43
+
44
+	/** @var int $key Current key-value of iterator */
45
+	private $key = null;
46
+
47
+	/**
48
+	 * Construct a CanvasArray
49
+	 *
50
+	 * @param string $jsonResponse A JSON-encoded response array from the
51
+	 *                             Canvas API
52
+	 * @param CanvasPest $canvasPest An API object for making pagination calls
53
+	 **/
54
+	public function __construct($jsonResponse, $canvasPest)
55
+	{
56
+		$this->api = $canvasPest;
57
+
58
+		$this->pagination = $this->parsePageLinks();
59
+
60
+		/* locate ourselves */
61
+		if (isset($this->pagination[CanvasPageLink::CURRENT])) {
62
+			$this->page = $this->pagination[CanvasPageLink::CURRENT]->getPageNumber();
63
+		} else {
64
+			$this->page = 1; // assume only one page (since no pagination)
65
+		}
66
+		$this->key = $this->pageNumberToKey($this->page);
67
+		$this->paginationPerPage[$this->page] = $this->pagination;
68
+
69
+		/* parse the JSON response string */
70
+		$key = $this->key;
71
+		foreach (json_decode($jsonResponse, true) as $item) {
72
+			$this->data[$key++] = new CanvasObject($item, $this->api);
73
+		}
74
+	}
75
+
76
+	/**
77
+	 * Parse the API response link headers into pagination information
78
+	 *
79
+	 * @param  boolean|string[] $headers (Optional, defaults to `$this->api->lastHeader('link')`)
80
+	 * @return CanvasPageLink[]
81
+	 */
82
+	protected function parsePageLinks($headers = false)
83
+	{
84
+		$pagination = [];
85
+		if (!$headers) {
86
+			$headers = $this->api->lastHeader('link');
87
+		}
88
+
89
+		/* parse Canvas page links */
90
+		if (preg_match_all('%<([^>]*)>\s*;\s*rel="([^"]+)"%', $headers, $links, PREG_SET_ORDER)) {
91
+			foreach ($links as $link) {
92
+				$pagination[$link[2]] = new CanvasPageLink($link[1], $link[2]);
93
+			}
94
+		}
95
+
96
+		return $pagination;
97
+	}
98
+	/**
99
+	 * Convert a page number to an array key
100
+	 *
101
+	 * @param int $pageNumber 1-indexed page number
102
+	 *
103
+	 * @return int
104
+	 *
105
+	 * @throws CanvasArray_Exception INVALID_PAGE_NUMBER If $pageNumber < 1
106
+	 **/
107
+	protected function pageNumberToKey($pageNumber)
108
+	{
109
+		if ($pageNumber < 1) {
110
+			throw new CanvasArray_Exception(
111
+				"{$pageNumber} is not a valid page number",
112
+				CanvasArray_Exception::INVALID_PAGE_NUMBER
113
+			);
114
+		}
115
+		if (isset($this->pagination[CanvasPageLink::CURRENT])) {
116
+			return ($pageNumber - 1) * $this->pagination[CanvasPageLink::CURRENT]->getPerPage();
117
+		} else {
118
+			return 0; // assume only one page (since no pagination);
119
+		}
120
+	}
121
+
122
+	/**
123
+	 * Convert an array key to a page number
124
+	 *
125
+	 * @param int $key Non-negative array key
126
+	 *
127
+	 * @return int
128
+	 *
129
+	 * @throws CanvasArray_Exception INVALID_ARRAY_KEY If $key < 0
130
+	 **/
131
+	protected function keyToPageNumber($key)
132
+	{
133
+		if ($key < 0) {
134
+			throw new CanvasArray_Exception(
135
+				"$key is not a valid array key",
136
+				CanvasArray_Exception::INVALID_ARRAY_KEY
137
+			);
138
+		}
139
+
140
+		if (isset($this->pagination[CanvasPageLink::CURRENT])) {
141
+			return ((int) ($key / $this->pagination[CanvasPageLink::CURRENT]->getPerPage())) + 1;
142
+		} else {
143
+			return 1; // assume single page if no pagination
144
+		}
145
+	}
146
+
147
+	/**
148
+	 * Request a page of responses from the API
149
+	 *
150
+	 * A page of responses will be requested if it appears that that page has
151
+	 * not yet been loaded (tested by checking if the initial element of the
152
+	 * page has been initialized in the $data array).
153
+	 *
154
+	 * @param int $pageNumber Page number to request
155
+	 * @param bool $forceRefresh (Optional) Force a refresh of backing data,
156
+	 *                           even if cached (defaults to `FALSE`)
157
+	 *
158
+	 * @return bool `TRUE` if the page is requested, `FALSE` if it is already
159
+	 *                     cached (and therefore not requested)
160
+	 **/
161
+	protected function requestPageNumber($pageNumber, $forceRefresh = false)
162
+	{
163
+		if (!isset($this->data[$this->pageNumberToKey($pageNumber)]) || ($forceRefresh && isset($this->api))) {
164
+			// assume one page if no pagination (and already loaded)
165
+			if (isset($this->pagination[CanvasPageLink::CURRENT])) {
166
+				$params = $this->pagination[CanvasPageLink::CURRENT]->getParams();
167
+				$params[CanvasPageLink::PARAM_PAGE_NUMBER] = $pageNumber;
168
+				$page = $this->api->get($this->pagination[CanvasPageLink::CURRENT]->getEndpoint(), $params);
169
+				$this->data = array_replace($this->data, $page->data);
170
+				$pagination = $this->parsePageLinks();
171
+				$this->paginationPerPage[$pagination[CanvasPageLink::CURRENT]->getPageNumber()] = $pagination;
172
+				return true;
173
+			}
174
+		}
175
+		return false;
176
+	}
177
+
178
+	/**
179
+	 * Request all pages from API
180
+	 *
181
+	 * This stores the entire API response locally, in preparation for, most
182
+	 * likely, serializing this object.
183
+	 *
184
+	 * @param bool $forceRefresh (Optional) Force a refresh of backing data,
185
+	 *                           even if cached (defaults to `FALSE`)
186
+	 *
187
+	 * @return void
188
+	 */
189
+	protected function requestAllPages($forceRefresh = false)
190
+	{
191
+		$_page = $this->page;
192
+		$_key = $this->key;
193
+
194
+		/* first fall-back: just keep going from where we are */
195
+		$nextPageNumber = false;
196
+		if (isset($this->pagination[CanvasPageLink::NEXT])) {
197
+			$nextPageNumber = $this->pagination[CanvasPageLink::NEXT]->getPageNumber();
198
+		}
199
+
200
+		/* best case: start at the beginning and request every page */
201
+		if (isset($this->pagination[CanvasPageLink::FIRST])) {
202
+			$first = $this->pagination[CanvasPageLink::FIRST]->getPageNumber();
203
+			$this->requestPageNumber($first, $forceRefresh);
204
+			if (isset($this->paginationPerPage[$first][CanvasPageLink::NEXT])) {
205
+				$nextPageNumber = $this->paginationPerPage[$first][CanvasPageLink::NEXT]->getPageNumber();
206
+			}
207
+		}
208
+
209
+		/* welp, here goes... let's hope we have a next page! */
210
+		while ($nextPageNumber !== false) {
211
+			$this->requestPageNumber($nextPageNumber, true);
212
+			if (isset($this->paginationPerPage[$nextPageNumber][CanvasPageLink::NEXT])) {
213
+				$nextPageNumber = $this->paginationPerPage[$nextPageNumber][CanvasPageLink::NEXT]->getPageNumber();
214
+			} else {
215
+				$nextPageNumber = false;
216
+			}
217
+		}
218
+
219
+		$this->page = $_page;
220
+		$this->key = $_key;
221
+	}
222
+
223
+	/***************************************************************************
224 224
      * ArrayObject methods
225 225
      */
226 226
 
227
-    /**
228
-     * Get the number of CanvasObjects in the Canvas response
229
-     *
230
-     * @return int
231
-     *
232
-     * @see http://php.net/manual/en/arrayobject.count.php ArrayObject::count
233
-     **/
234
-    public function count()
235
-    {
236
-        $this->requestAllPages();
237
-        return count($this->data);
238
-    }
239
-
240
-    /**
241
-     * Creates a copy of the CanvasArray
242
-     *
243
-     * @return CanvasObject[]
244
-     *
245
-     * @see http://php.net/manual/en/arrayobject.getarraycopy.php
246
-     *      ArrayObject::getArrayCopy
247
-     **/
248
-    public function getArrayCopy()
249
-    {
250
-        $this->requestAllPages();
251
-        return $this->data;
252
-    }
253
-
254
-    /***************************************************************************
227
+	/**
228
+	 * Get the number of CanvasObjects in the Canvas response
229
+	 *
230
+	 * @return int
231
+	 *
232
+	 * @see http://php.net/manual/en/arrayobject.count.php ArrayObject::count
233
+	 **/
234
+	public function count()
235
+	{
236
+		$this->requestAllPages();
237
+		return count($this->data);
238
+	}
239
+
240
+	/**
241
+	 * Creates a copy of the CanvasArray
242
+	 *
243
+	 * @return CanvasObject[]
244
+	 *
245
+	 * @see http://php.net/manual/en/arrayobject.getarraycopy.php
246
+	 *      ArrayObject::getArrayCopy
247
+	 **/
248
+	public function getArrayCopy()
249
+	{
250
+		$this->requestAllPages();
251
+		return $this->data;
252
+	}
253
+
254
+	/***************************************************************************
255 255
      * ArrayAccess methods
256 256
      */
257 257
 
258
-    /**
259
-     * Whether an offset exists
260
-     *
261
-     * @param int|string $offset
262
-     *
263
-     * @return bool
264
-     *
265
-     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
266
-     *      ArrayAccess::offsetExists
267
-     **/
268
-    public function offsetExists($offset)
269
-    {
270
-        if (!isset($this->data[$offset])) {
271
-            $this->requestAllPages();
272
-        }
273
-        return isset($this->data[$offset]);
274
-    }
275
-
276
-    /**
277
-     * Offset to retrieve
278
-     *
279
-     * @param int|string $offset
280
-     *
281
-     * @return CanvasObject|null
282
-     *
283
-     * @see http://php.net/manual/en/arrayaccess.offsetexists.php
284
-     *      ArrayAccess::offsetGet
285
-     **/
286
-    public function offsetGet($offset)
287
-    {
288
-        return $this->data[$offset];
289
-    }
290
-
291
-    /**
292
-     * Assign a value to the specified offset
293
-     *
294
-     * @deprecated CanvasObject and CanvasArray responses are immutable
295
-     *
296
-     * @param int|string $offset
297
-     * @param CanvasObject $value
298
-     *
299
-     * @return void
300
-     *
301
-     * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception
302
-     *
303
-     * @see http://php.net/manual/en/arrayaccess.offsetset.php
304
-     *      ArrayAccess::offsetSet
305
-     **/
306
-    public function offsetSet($offset, $value)
307
-    {
308
-        throw new CanvasArray_Exception(
309
-            'Canvas responses are immutable',
310
-            CanvasArray_Exception::IMMUTABLE
311
-        );
312
-    }
313
-
314
-    /**
315
-     * Unset an offset
316
-     *
317
-     * @deprecated CanvasObject and CanvasArray responses are immutable
318
-     *
319
-     * @param int|string $offset
320
-     *
321
-     * @return void
322
-     *
323
-     * @throws CanvasArray_Exception IMMUTABLE All calls to this method will
324
-     *         cause an exception
325
-     *
326
-     * @see http://php.net/manual/en/arrayaccess.offsetunset.php
327
-     *      ArrayAccess::offsetUnset
328
-     **/
329
-    public function offsetUnset($offset)
330
-    {
331
-        throw new CanvasArray_Exception(
332
-            'Canvas responses are immutable',
333
-            CanvasArray_Exception::IMMUTABLE
334
-        );
335
-    }
336
-
337
-    /**************************************************************************/
338
-
339
-    /**************************************************************************
258
+	/**
259
+	 * Whether an offset exists
260
+	 *
261
+	 * @param int|string $offset
262
+	 *
263
+	 * @return bool
264
+	 *
265
+	 * @see http://php.net/manual/en/arrayaccess.offsetexists.php
266
+	 *      ArrayAccess::offsetExists
267
+	 **/
268
+	public function offsetExists($offset)
269
+	{
270
+		if (!isset($this->data[$offset])) {
271
+			$this->requestAllPages();
272
+		}
273
+		return isset($this->data[$offset]);
274
+	}
275
+
276
+	/**
277
+	 * Offset to retrieve
278
+	 *
279
+	 * @param int|string $offset
280
+	 *
281
+	 * @return CanvasObject|null
282
+	 *
283
+	 * @see http://php.net/manual/en/arrayaccess.offsetexists.php
284
+	 *      ArrayAccess::offsetGet
285
+	 **/
286
+	public function offsetGet($offset)
287
+	{
288
+		return $this->data[$offset];
289
+	}
290
+
291
+	/**
292
+	 * Assign a value to the specified offset
293
+	 *
294
+	 * @deprecated CanvasObject and CanvasArray responses are immutable
295
+	 *
296
+	 * @param int|string $offset
297
+	 * @param CanvasObject $value
298
+	 *
299
+	 * @return void
300
+	 *
301
+	 * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception
302
+	 *
303
+	 * @see http://php.net/manual/en/arrayaccess.offsetset.php
304
+	 *      ArrayAccess::offsetSet
305
+	 **/
306
+	public function offsetSet($offset, $value)
307
+	{
308
+		throw new CanvasArray_Exception(
309
+			'Canvas responses are immutable',
310
+			CanvasArray_Exception::IMMUTABLE
311
+		);
312
+	}
313
+
314
+	/**
315
+	 * Unset an offset
316
+	 *
317
+	 * @deprecated CanvasObject and CanvasArray responses are immutable
318
+	 *
319
+	 * @param int|string $offset
320
+	 *
321
+	 * @return void
322
+	 *
323
+	 * @throws CanvasArray_Exception IMMUTABLE All calls to this method will
324
+	 *         cause an exception
325
+	 *
326
+	 * @see http://php.net/manual/en/arrayaccess.offsetunset.php
327
+	 *      ArrayAccess::offsetUnset
328
+	 **/
329
+	public function offsetUnset($offset)
330
+	{
331
+		throw new CanvasArray_Exception(
332
+			'Canvas responses are immutable',
333
+			CanvasArray_Exception::IMMUTABLE
334
+		);
335
+	}
336
+
337
+	/**************************************************************************/
338
+
339
+	/**************************************************************************
340 340
      * Iterator methods
341 341
      */
342 342
 
343
-    /**
344
-     * Return the current element
345
-     *
346
-     * @return CanvasObject
347
-     *
348
-     * @see http://php.net/manual/en/iterator.current.php Iterator::current
349
-     **/
350
-    public function current()
351
-    {
352
-        if (!isset($this->data[$this->key])) {
353
-            $this->requestPageNumber($this->keyToPageNumber($this->key));
354
-        }
355
-        return $this->data[$this->key];
356
-    }
357
-
358
-    /**
359
-     * Return the key of the current element
360
-     *
361
-     * @return int
362
-     *
363
-     * @see http://php.net/manual/en/iterator.key.php Iterator::key
364
-     **/
365
-    public function key()
366
-    {
367
-        return $this->key;
368
-    }
369
-
370
-    /**
371
-     * Move forward to next element
372
-     *
373
-     * @return void
374
-     *
375
-     * @see http://php.net/manual/en/iterator.next.php Iterator::next
376
-     **/
377
-    public function next()
378
-    {
379
-        $this->key++;
380
-    }
381
-
382
-    /**
383
-     * Rewind the iterator to the first element
384
-     *
385
-     * @return void
386
-     *
387
-     * @see http://php.net/manual/en/iterator.rewind.php Iterator::rewind
388
-     **/
389
-    public function rewind()
390
-    {
391
-        $this->key = 0;
392
-    }
393
-
394
-    /**
395
-     * Checks if current position is valid
396
-     *
397
-     * @return bool
398
-     *
399
-     * @see http://php.net/manual/en/iterator.valid.php Iterator::valid
400
-     **/
401
-    public function valid()
402
-    {
403
-        return ($this->offsetExists($this->key));
404
-    }
405
-
406
-    /**************************************************************************/
407
-
408
-    /***************************************************************************
343
+	/**
344
+	 * Return the current element
345
+	 *
346
+	 * @return CanvasObject
347
+	 *
348
+	 * @see http://php.net/manual/en/iterator.current.php Iterator::current
349
+	 **/
350
+	public function current()
351
+	{
352
+		if (!isset($this->data[$this->key])) {
353
+			$this->requestPageNumber($this->keyToPageNumber($this->key));
354
+		}
355
+		return $this->data[$this->key];
356
+	}
357
+
358
+	/**
359
+	 * Return the key of the current element
360
+	 *
361
+	 * @return int
362
+	 *
363
+	 * @see http://php.net/manual/en/iterator.key.php Iterator::key
364
+	 **/
365
+	public function key()
366
+	{
367
+		return $this->key;
368
+	}
369
+
370
+	/**
371
+	 * Move forward to next element
372
+	 *
373
+	 * @return void
374
+	 *
375
+	 * @see http://php.net/manual/en/iterator.next.php Iterator::next
376
+	 **/
377
+	public function next()
378
+	{
379
+		$this->key++;
380
+	}
381
+
382
+	/**
383
+	 * Rewind the iterator to the first element
384
+	 *
385
+	 * @return void
386
+	 *
387
+	 * @see http://php.net/manual/en/iterator.rewind.php Iterator::rewind
388
+	 **/
389
+	public function rewind()
390
+	{
391
+		$this->key = 0;
392
+	}
393
+
394
+	/**
395
+	 * Checks if current position is valid
396
+	 *
397
+	 * @return bool
398
+	 *
399
+	 * @see http://php.net/manual/en/iterator.valid.php Iterator::valid
400
+	 **/
401
+	public function valid()
402
+	{
403
+		return ($this->offsetExists($this->key));
404
+	}
405
+
406
+	/**************************************************************************/
407
+
408
+	/***************************************************************************
409 409
      * Serializable methods
410 410
      */
411 411
 
412
-    /**
413
-     * String representation of CanvasArray
414
-     *
415
-     * @return string
416
-     *
417
-     * @see http://php.net/manual/en/serializable.serialize.php
418
-     *      Serializable::serialize()
419
-     **/
420
-    public function serialize()
421
-    {
422
-        $this->requestAllPages();
423
-        return serialize(
424
-            array(
425
-                'page' => $this->page,
426
-                'key' => $this->key,
427
-                'data' => $this->data
428
-            )
429
-        );
430
-    }
431
-
432
-    /**
433
-     * Construct a CanvasArray from its string representation
434
-     *
435
-     * The data in the unserialized CanvasArray is static and cannot be
436
-     * refreshed, as the CanvasPest API connection is _not_ serialized to
437
-     * preserve the security of API access tokens.
438
-     *
439
-     * @param string $data
440
-     *
441
-     * @return string
442
-     *
443
-     * @see http://php.net/manual/en/serializable.unserialize.php
444
-     *      Serializable::unserialize()
445
-     **/
446
-    public function unserialize($data)
447
-    {
448
-        $_data = unserialize($data);
449
-        $this->page = $_data['page'];
450
-        $this->key = $_data['key'];
451
-        $this->data = $_data['data'];
452
-        $this->api = null;
453
-        $this->endpoint = null;
454
-        $this->pagination = array();
455
-    }
412
+	/**
413
+	 * String representation of CanvasArray
414
+	 *
415
+	 * @return string
416
+	 *
417
+	 * @see http://php.net/manual/en/serializable.serialize.php
418
+	 *      Serializable::serialize()
419
+	 **/
420
+	public function serialize()
421
+	{
422
+		$this->requestAllPages();
423
+		return serialize(
424
+			array(
425
+				'page' => $this->page,
426
+				'key' => $this->key,
427
+				'data' => $this->data
428
+			)
429
+		);
430
+	}
431
+
432
+	/**
433
+	 * Construct a CanvasArray from its string representation
434
+	 *
435
+	 * The data in the unserialized CanvasArray is static and cannot be
436
+	 * refreshed, as the CanvasPest API connection is _not_ serialized to
437
+	 * preserve the security of API access tokens.
438
+	 *
439
+	 * @param string $data
440
+	 *
441
+	 * @return string
442
+	 *
443
+	 * @see http://php.net/manual/en/serializable.unserialize.php
444
+	 *      Serializable::unserialize()
445
+	 **/
446
+	public function unserialize($data)
447
+	{
448
+		$_data = unserialize($data);
449
+		$this->page = $_data['page'];
450
+		$this->key = $_data['key'];
451
+		$this->data = $_data['data'];
452
+		$this->api = null;
453
+		$this->endpoint = null;
454
+		$this->pagination = array();
455
+	}
456 456
 }
Please login to merge, or discard this patch.
src/CanvasPageLink.php 1 patch
Indentation   +177 added lines, -177 removed lines patch added patch discarded remove patch
@@ -16,181 +16,181 @@
 block discarded – undo
16 16
  **/
17 17
 class CanvasPageLink
18 18
 {
19
-    /** Name of the current page link */
20
-    const CURRENT = 'current';
21
-
22
-    /** Name of the first page link */
23
-    const FIRST = 'first';
24
-
25
-    /** Name of the last page link */
26
-    const LAST = 'last';
27
-
28
-    /** Name of the next page link */
29
-    const NEXT = 'next';
30
-
31
-    /** Name of the previous page link */
32
-    const PREV = 'prev';
33
-
34
-
35
-    /** @var string $name Name of the page link */
36
-    private $name;
37
-
38
-    /** @var string $endpoint Path of the API endpoint being paginated */
39
-    private $endpoint;
40
-
41
-    /** @var array $params Query parameters for the page link API call */
42
-    private $params;
43
-
44
-    /** Name of the page number parameter in the page link */
45
-    const PARAM_PAGE_NUMBER = 'page';
46
-
47
-    /** Name of the parameter describing the number of responses per page in the page link */
48
-    const PARAM_PER_PAGE = 'per_page';
49
-
50
-    /**
51
-     * Construct a new Canvas page link object.
52
-     *
53
-     * CanvasPageLinks can be constructed with two possible parameter lists:
54
-     *
55
-     * 1. `__construct(string $pageUrl, string $pageName)` which expects a
56
-     * non-empty string representing the URL of the API endpoint to retrieve the
57
-     * page and a non-empty string representing the canonical name of the page
58
-     * relative to the current page.
59
-     * 2. `__construct(int $pageNumber, CanvasPageLink $modelCanvasPageLink, string $pageName)`
60
-     * which expects a page number greater than zero, any CanvasPageLink object
61
-     * relative to the current page (to be used as a model) and a non-empty string
62
-     * representing the canonical name of the page relative to the current page.
63
-     *
64
-     * @throws CanvasPageLink_Exception INVALID_CONSTRUCTOR If $pageUrl or
65
-     *         $pageName is empty or a non-string
66
-     * @throws CanvasPageLink_Exception INVALID_CONSTRUCTOR If $pageNumber is
67
-     *         not a number greater than zero, $modelCanvasPageLink is not an
68
-     *         instance of CanvasPageLink or $pageName is empty or a non-string
69
-     */
70
-    public function __construct()
71
-    {
72
-        switch (func_num_args()) {
73
-            case 2: /* __construct($pageUrl, $pageName) */
74
-                $pageUrl = func_get_arg(0);
75
-                $this->name = func_get_arg(1);
76
-                if (is_string($pageUrl) && !empty($pageUrl) && is_string($this->name) && !empty($this->name)) {
77
-                    $this->endpoint = preg_replace('%.*/api/v1(/.*)$%', '$1', parse_url($pageUrl, PHP_URL_PATH));
78
-                    parse_str(parse_url($pageUrl, PHP_URL_QUERY), $this->params);
79
-                } else {
80
-                    throw new CanvasPageLink_Exception(
81
-                        'Expected two non-empty strings for page URL and name',
82
-                        CanvasPageLink_Exception::INVALID_CONSTRUCTOR
83
-                    );
84
-                }
85
-                break;
86
-
87
-            case 3: /* __construct($pageNumber, $modelCanvasPageLink, $pageName) */
88
-                $pageNumber = func_get_arg(0);
89
-                $model = func_get_arg(1);
90
-                $this->name = func_get_arg(2);
91
-                if (is_int($pageNumber) &&
92
-                    $pageNumber > 0 &&
93
-                    $model instanceof CanvasPageLink &&
94
-                    is_string($this->name) &&
95
-                    !empty($this->name)
96
-                ) {
97
-                    $this->endpoint = $model->endpoint;
98
-                    $this->params = $model->params;
99
-                    switch ($this->name) {
100
-                        case self::PREV:
101
-                            $this->params[self::PARAM_PAGE_NUMBER] = $pageNumber - 1;
102
-                            break;
103
-
104
-                        case self::NEXT:
105
-                            $this->params[self::PARAM_PAGE_NUMBER] = $pageNumber + 1;
106
-                            break;
107
-
108
-                        case self::FIRST:
109
-                            $this->params[self::PARAM_PAGE_NUMBER] = 1;
110
-                            break;
111
-
112
-                        case self::LAST:
113
-                        default:
114
-                            throw new CanvasPageLink_Exception(
115
-                                "'{$this->name}' cannot be converted to a page number",
116
-                                CanvasPageLink_Exception::INVALID_CONSTRUCTOR
117
-                            );
118
-                    }
119
-                } else {
120
-                    throw new CanvasPageLink_Exception(
121
-                        'Expected a page number, a model CanvasPageLink object and a non-empty string page name',
122
-                        CanvasPageLink_Exception::INVALID_CONSTRUCTOR
123
-                    );
124
-                }
125
-                break;
126
-        }
127
-    }
128
-
129
-    /**
130
-     * Canonical name of this page link
131
-     *
132
-     * @return string
133
-     **/
134
-    public function getName()
135
-    {
136
-        return $this->name;
137
-    }
138
-
139
-    /**
140
-     * API endpoint being paginated
141
-     *
142
-     * @return string
143
-     **/
144
-    public function getEndpoint()
145
-    {
146
-        return $this->endpoint;
147
-    }
148
-
149
-    /**
150
-     * Query parameters to retrieve the linked page
151
-     *
152
-     * @return array
153
-     **/
154
-    public function getParams()
155
-    {
156
-        return $this->params;
157
-    }
158
-
159
-    /**
160
-     * The (1-indexed) page number of this page
161
-     *
162
-     * @return int
163
-     **/
164
-    public function getPageNumber()
165
-    {
166
-        return $this->params[self::PARAM_PAGE_NUMBER];
167
-    }
168
-
169
-    /**
170
-     * The number of responses per page generating this pagination
171
-     *
172
-     * @return int
173
-     **/
174
-    public function getPerPage()
175
-    {
176
-        return $this->params[self::PARAM_PER_PAGE];
177
-    }
178
-
179
-    /**
180
-     * An array representation of the CanvasArray
181
-     *
182
-     * @return array
183
-     **/
184
-    public function getArrayCopy()
185
-    {
186
-        $arr = array();
187
-        $_key = $this->key;
188
-        $_page = $this->page;
189
-        foreach ($this as $obj) {
190
-            $arr[] = $obj->getArrayCopy();
191
-        }
192
-        $this->page = $_page;
193
-        $this->key = $_key;
194
-        return $arr;
195
-    }
19
+	/** Name of the current page link */
20
+	const CURRENT = 'current';
21
+
22
+	/** Name of the first page link */
23
+	const FIRST = 'first';
24
+
25
+	/** Name of the last page link */
26
+	const LAST = 'last';
27
+
28
+	/** Name of the next page link */
29
+	const NEXT = 'next';
30
+
31
+	/** Name of the previous page link */
32
+	const PREV = 'prev';
33
+
34
+
35
+	/** @var string $name Name of the page link */
36
+	private $name;
37
+
38
+	/** @var string $endpoint Path of the API endpoint being paginated */
39
+	private $endpoint;
40
+
41
+	/** @var array $params Query parameters for the page link API call */
42
+	private $params;
43
+
44
+	/** Name of the page number parameter in the page link */
45
+	const PARAM_PAGE_NUMBER = 'page';
46
+
47
+	/** Name of the parameter describing the number of responses per page in the page link */
48
+	const PARAM_PER_PAGE = 'per_page';
49
+
50
+	/**
51
+	 * Construct a new Canvas page link object.
52
+	 *
53
+	 * CanvasPageLinks can be constructed with two possible parameter lists:
54
+	 *
55
+	 * 1. `__construct(string $pageUrl, string $pageName)` which expects a
56
+	 * non-empty string representing the URL of the API endpoint to retrieve the
57
+	 * page and a non-empty string representing the canonical name of the page
58
+	 * relative to the current page.
59
+	 * 2. `__construct(int $pageNumber, CanvasPageLink $modelCanvasPageLink, string $pageName)`
60
+	 * which expects a page number greater than zero, any CanvasPageLink object
61
+	 * relative to the current page (to be used as a model) and a non-empty string
62
+	 * representing the canonical name of the page relative to the current page.
63
+	 *
64
+	 * @throws CanvasPageLink_Exception INVALID_CONSTRUCTOR If $pageUrl or
65
+	 *         $pageName is empty or a non-string
66
+	 * @throws CanvasPageLink_Exception INVALID_CONSTRUCTOR If $pageNumber is
67
+	 *         not a number greater than zero, $modelCanvasPageLink is not an
68
+	 *         instance of CanvasPageLink or $pageName is empty or a non-string
69
+	 */
70
+	public function __construct()
71
+	{
72
+		switch (func_num_args()) {
73
+			case 2: /* __construct($pageUrl, $pageName) */
74
+				$pageUrl = func_get_arg(0);
75
+				$this->name = func_get_arg(1);
76
+				if (is_string($pageUrl) && !empty($pageUrl) && is_string($this->name) && !empty($this->name)) {
77
+					$this->endpoint = preg_replace('%.*/api/v1(/.*)$%', '$1', parse_url($pageUrl, PHP_URL_PATH));
78
+					parse_str(parse_url($pageUrl, PHP_URL_QUERY), $this->params);
79
+				} else {
80
+					throw new CanvasPageLink_Exception(
81
+						'Expected two non-empty strings for page URL and name',
82
+						CanvasPageLink_Exception::INVALID_CONSTRUCTOR
83
+					);
84
+				}
85
+				break;
86
+
87
+			case 3: /* __construct($pageNumber, $modelCanvasPageLink, $pageName) */
88
+				$pageNumber = func_get_arg(0);
89
+				$model = func_get_arg(1);
90
+				$this->name = func_get_arg(2);
91
+				if (is_int($pageNumber) &&
92
+					$pageNumber > 0 &&
93
+					$model instanceof CanvasPageLink &&
94
+					is_string($this->name) &&
95
+					!empty($this->name)
96
+				) {
97
+					$this->endpoint = $model->endpoint;
98
+					$this->params = $model->params;
99
+					switch ($this->name) {
100
+						case self::PREV:
101
+							$this->params[self::PARAM_PAGE_NUMBER] = $pageNumber - 1;
102
+							break;
103
+
104
+						case self::NEXT:
105
+							$this->params[self::PARAM_PAGE_NUMBER] = $pageNumber + 1;
106
+							break;
107
+
108
+						case self::FIRST:
109
+							$this->params[self::PARAM_PAGE_NUMBER] = 1;
110
+							break;
111
+
112
+						case self::LAST:
113
+						default:
114
+							throw new CanvasPageLink_Exception(
115
+								"'{$this->name}' cannot be converted to a page number",
116
+								CanvasPageLink_Exception::INVALID_CONSTRUCTOR
117
+							);
118
+					}
119
+				} else {
120
+					throw new CanvasPageLink_Exception(
121
+						'Expected a page number, a model CanvasPageLink object and a non-empty string page name',
122
+						CanvasPageLink_Exception::INVALID_CONSTRUCTOR
123
+					);
124
+				}
125
+				break;
126
+		}
127
+	}
128
+
129
+	/**
130
+	 * Canonical name of this page link
131
+	 *
132
+	 * @return string
133
+	 **/
134
+	public function getName()
135
+	{
136
+		return $this->name;
137
+	}
138
+
139
+	/**
140
+	 * API endpoint being paginated
141
+	 *
142
+	 * @return string
143
+	 **/
144
+	public function getEndpoint()
145
+	{
146
+		return $this->endpoint;
147
+	}
148
+
149
+	/**
150
+	 * Query parameters to retrieve the linked page
151
+	 *
152
+	 * @return array
153
+	 **/
154
+	public function getParams()
155
+	{
156
+		return $this->params;
157
+	}
158
+
159
+	/**
160
+	 * The (1-indexed) page number of this page
161
+	 *
162
+	 * @return int
163
+	 **/
164
+	public function getPageNumber()
165
+	{
166
+		return $this->params[self::PARAM_PAGE_NUMBER];
167
+	}
168
+
169
+	/**
170
+	 * The number of responses per page generating this pagination
171
+	 *
172
+	 * @return int
173
+	 **/
174
+	public function getPerPage()
175
+	{
176
+		return $this->params[self::PARAM_PER_PAGE];
177
+	}
178
+
179
+	/**
180
+	 * An array representation of the CanvasArray
181
+	 *
182
+	 * @return array
183
+	 **/
184
+	public function getArrayCopy()
185
+	{
186
+		$arr = array();
187
+		$_key = $this->key;
188
+		$_page = $this->page;
189
+		foreach ($this as $obj) {
190
+			$arr[] = $obj->getArrayCopy();
191
+		}
192
+		$this->page = $_page;
193
+		$this->key = $_key;
194
+		return $arr;
195
+	}
196 196
 }
Please login to merge, or discard this patch.