Completed
Push — master ( bc2b20...9217c1 )
by Seth
01:48
created
src/CanvasArray_Exception.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -10,6 +10,6 @@
 block discarded – undo
10 10
  * @author Seth Battis <[email protected]>
11 11
  **/
12 12
 class CanvasArray_Exception extends CanvasObject_Exception {
13
-    const INVALID_PAGE_NUMBER = 200;
14
-    const INVALID_ARRAY_KEY = 201;
13
+	const INVALID_PAGE_NUMBER = 200;
14
+	const INVALID_ARRAY_KEY = 201;
15 15
 }
Please login to merge, or discard this patch.
src/CanvasObject_Exception.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -11,6 +11,6 @@
 block discarded – undo
11 11
  **/
12 12
 class CanvasObject_Exception extends CanvasPest_Exception
13 13
 {
14
-    /** Response values are read-only */
15
-    const IMMUTABLE = 101;
14
+	/** Response values are read-only */
15
+	const IMMUTABLE = 101;
16 16
 }
Please login to merge, or discard this patch.
src/CanvasPageLink_Exception.php 1 patch
Indentation   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -11,6 +11,6 @@
 block discarded – undo
11 11
  **/
12 12
 class CanvasPageLink_Exception extends CanvasArray_Exception
13 13
 {
14
-    /** Invalid parameters were passed to the constructor */
15
-    const INVALID_CONSTRUCTOR = 301;
14
+	/** Invalid parameters were passed to the constructor */
15
+	const INVALID_CONSTRUCTOR = 301;
16 16
 }
Please login to merge, or discard this patch.
src/CanvasObject.php 1 patch
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -18,214 +18,214 @@
 block discarded – undo
18 18
 class CanvasObject implements \ArrayAccess, \Serializable
19 19
 {
20 20
 
21
-    /** @var array $data Backing store */
22
-    private $data;
23
-
24
-    /**
25
-     * Construct a CanvasObject
26
-     *
27
-     * @param string|string[] $response JSON-encoded response from the Canvas
28
-     *                                  API or the resulting JSON-decoded
29
-     *                                  associative array
30
-     **/
31
-    public function __construct($response)
32
-    {
33
-        if (is_array($response)) {
34
-            $this->data = $response;
35
-        } else {
36
-            $this->data = json_decode($response, true);
37
-        }
38
-    }
39
-
40
-    /***************************************************************************
21
+	/** @var array $data Backing store */
22
+	private $data;
23
+
24
+	/**
25
+	 * Construct a CanvasObject
26
+	 *
27
+	 * @param string|string[] $response JSON-encoded response from the Canvas
28
+	 *                                  API or the resulting JSON-decoded
29
+	 *                                  associative array
30
+	 **/
31
+	public function __construct($response)
32
+	{
33
+		if (is_array($response)) {
34
+			$this->data = $response;
35
+		} else {
36
+			$this->data = json_decode($response, true);
37
+		}
38
+	}
39
+
40
+	/***************************************************************************
41 41
      * Object methods
42 42
      */
43 43
 
44
-    /**
45
-     * Whether a property exists
46
-     *
47
-     * @param string $key
48
-     *
49
-     * @return bool
50
-     *
51
-     * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
52
-     **/
53
-    public function __isset($key)
54
-    {
55
-        return isset($this->data[$key]);
56
-    }
57
-
58
-    /**
59
-     * Property to retrieve
60
-     *
61
-     * @param string $key
62
-     *
63
-     * @return mixed
64
-     *
65
-     * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
66
-     **/
67
-    public function __get($key)
68
-    {
69
-        return $this->data[$key];
70
-    }
71
-
72
-    /**
73
-     * Whether a property exists
74
-     *
75
-     * @deprecated Canvas objects are immutable
76
-     *
77
-     * @param string $key
78
-     * @param mixed $value
79
-     *
80
-     * @return void
81
-     *
82
-     * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
83
-     *
84
-     * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
85
-     **/
86
-    public function __set($key, $value)
87
-    {
88
-        throw new CanvasObject_Exception(
89
-            'Canvas objects are immutable',
90
-            CanvasObject_Exception::IMMUTABLE
91
-        );
92
-    }
93
-
94
-    /**
95
-     * Unset a property
96
-     *
97
-     * @deprecated Canvas objects are immutable
98
-     *
99
-     * @param string $key
100
-     *
101
-     * @return void
102
-     *
103
-     * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
104
-     *
105
-     * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
106
-     **/
107
-    public function __unset($key)
108
-    {
109
-        throw new CanvasObject_Exception(
110
-            'Canvas objects are immutable',
111
-            CanvasObject_Exception::IMMUTABLE
112
-        );
113
-    }
114
-
115
-    /***************************************************************************
44
+	/**
45
+	 * Whether a property exists
46
+	 *
47
+	 * @param string $key
48
+	 *
49
+	 * @return bool
50
+	 *
51
+	 * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
52
+	 **/
53
+	public function __isset($key)
54
+	{
55
+		return isset($this->data[$key]);
56
+	}
57
+
58
+	/**
59
+	 * Property to retrieve
60
+	 *
61
+	 * @param string $key
62
+	 *
63
+	 * @return mixed
64
+	 *
65
+	 * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
66
+	 **/
67
+	public function __get($key)
68
+	{
69
+		return $this->data[$key];
70
+	}
71
+
72
+	/**
73
+	 * Whether a property exists
74
+	 *
75
+	 * @deprecated Canvas objects are immutable
76
+	 *
77
+	 * @param string $key
78
+	 * @param mixed $value
79
+	 *
80
+	 * @return void
81
+	 *
82
+	 * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
83
+	 *
84
+	 * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
85
+	 **/
86
+	public function __set($key, $value)
87
+	{
88
+		throw new CanvasObject_Exception(
89
+			'Canvas objects are immutable',
90
+			CanvasObject_Exception::IMMUTABLE
91
+		);
92
+	}
93
+
94
+	/**
95
+	 * Unset a property
96
+	 *
97
+	 * @deprecated Canvas objects are immutable
98
+	 *
99
+	 * @param string $key
100
+	 *
101
+	 * @return void
102
+	 *
103
+	 * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
104
+	 *
105
+	 * @see http://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members Property overloading
106
+	 **/
107
+	public function __unset($key)
108
+	{
109
+		throw new CanvasObject_Exception(
110
+			'Canvas objects are immutable',
111
+			CanvasObject_Exception::IMMUTABLE
112
+		);
113
+	}
114
+
115
+	/***************************************************************************
116 116
      * ArrayAccess methods
117 117
      */
118 118
 
119
-    /**
120
-     * Whether an offset exists
121
-     *
122
-     * @param int|string $offset
123
-     *
124
-     * @return bool
125
-     *
126
-     * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists()
127
-     **/
128
-    public function offsetExists($offset)
129
-    {
130
-        return isset($this->data[$offset]);
131
-    }
132
-
133
-    /**
134
-     * Offset to retrieve
135
-     *
136
-     * @param int|string $offset
137
-     *
138
-     * @return mixed|null
139
-     *
140
-     * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetGet()
141
-     **/
142
-    public function offsetGet($offset)
143
-    {
144
-        return $this->data[$offset];
145
-    }
146
-
147
-    /**
148
-     * Assign a value to the specified offset
149
-     *
150
-     * @deprecated Canvas objects are immutable
151
-     *
152
-     * @param int|string $offset
153
-     * @param mixed $value
154
-     *
155
-     * @return void
156
-     *
157
-     * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
158
-     *
159
-     * @see http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet()
160
-     **/
161
-    public function offsetSet($offset, $value)
162
-    {
163
-        throw new CanvasObject_Exception(
164
-            'Canvas objects are immutable',
165
-            CanvasObject_Exception::IMMUTABLE
166
-        );
167
-    }
168
-
169
-    /**
170
-     * Unset an offset
171
-     *
172
-     * @deprecated Canvas objects are immutable
173
-     *
174
-     * @param int|string $offset
175
-     *
176
-     * @return void
177
-     *
178
-     * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
179
-     *
180
-     * @see http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset()
181
-     **/
182
-    public function offsetUnset($offset)
183
-    {
184
-        throw new CanvasObject_Exception(
185
-            'Canvas objects are immutable',
186
-            CanvasObject_Exception::IMMUTABLE
187
-        );
188
-    }
189
-
190
-    /***************************************************************************
119
+	/**
120
+	 * Whether an offset exists
121
+	 *
122
+	 * @param int|string $offset
123
+	 *
124
+	 * @return bool
125
+	 *
126
+	 * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists()
127
+	 **/
128
+	public function offsetExists($offset)
129
+	{
130
+		return isset($this->data[$offset]);
131
+	}
132
+
133
+	/**
134
+	 * Offset to retrieve
135
+	 *
136
+	 * @param int|string $offset
137
+	 *
138
+	 * @return mixed|null
139
+	 *
140
+	 * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetGet()
141
+	 **/
142
+	public function offsetGet($offset)
143
+	{
144
+		return $this->data[$offset];
145
+	}
146
+
147
+	/**
148
+	 * Assign a value to the specified offset
149
+	 *
150
+	 * @deprecated Canvas objects are immutable
151
+	 *
152
+	 * @param int|string $offset
153
+	 * @param mixed $value
154
+	 *
155
+	 * @return void
156
+	 *
157
+	 * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
158
+	 *
159
+	 * @see http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet()
160
+	 **/
161
+	public function offsetSet($offset, $value)
162
+	{
163
+		throw new CanvasObject_Exception(
164
+			'Canvas objects are immutable',
165
+			CanvasObject_Exception::IMMUTABLE
166
+		);
167
+	}
168
+
169
+	/**
170
+	 * Unset an offset
171
+	 *
172
+	 * @deprecated Canvas objects are immutable
173
+	 *
174
+	 * @param int|string $offset
175
+	 *
176
+	 * @return void
177
+	 *
178
+	 * @throws CanvasObject_Exception IMMUTABLE All calls to this method will cause an exception
179
+	 *
180
+	 * @see http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset()
181
+	 **/
182
+	public function offsetUnset($offset)
183
+	{
184
+		throw new CanvasObject_Exception(
185
+			'Canvas objects are immutable',
186
+			CanvasObject_Exception::IMMUTABLE
187
+		);
188
+	}
189
+
190
+	/***************************************************************************
191 191
      * Serializable methods
192 192
      */
193 193
 
194
-    /**
195
-     * String representation of CanvasObject
196
-     *
197
-     * @return string
198
-     *
199
-     * @see http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
200
-     **/
201
-    public function serialize()
202
-    {
203
-        return serialize($this->data);
204
-    }
205
-
206
-    /**
207
-     * Construct a CanvasObject from its string representation
208
-     *
209
-     * @param string $data
210
-     *
211
-     * @return void
212
-     *
213
-     * @see http://php.net/manual/en/serializable.unserialize.php Serializable::unsserialize()
214
-     **/
215
-    public function unserialize($data)
216
-    {
217
-        $this->data = unserialize($data);
218
-    }
219
-
220
-    /**************************************************************************/
221
-
222
-    /**
223
-     * An array representation of the CanvasObject
224
-     *
225
-     * @return array
226
-     **/
227
-    public function getArrayCopy()
228
-    {
229
-        return $this->data;
230
-    }
194
+	/**
195
+	 * String representation of CanvasObject
196
+	 *
197
+	 * @return string
198
+	 *
199
+	 * @see http://php.net/manual/en/serializable.serialize.php Serializable::serialize()
200
+	 **/
201
+	public function serialize()
202
+	{
203
+		return serialize($this->data);
204
+	}
205
+
206
+	/**
207
+	 * Construct a CanvasObject from its string representation
208
+	 *
209
+	 * @param string $data
210
+	 *
211
+	 * @return void
212
+	 *
213
+	 * @see http://php.net/manual/en/serializable.unserialize.php Serializable::unsserialize()
214
+	 **/
215
+	public function unserialize($data)
216
+	{
217
+		$this->data = unserialize($data);
218
+	}
219
+
220
+	/**************************************************************************/
221
+
222
+	/**
223
+	 * An array representation of the CanvasObject
224
+	 *
225
+	 * @return array
226
+	 **/
227
+	public function getArrayCopy()
228
+	{
229
+		return $this->data;
230
+	}
231 231
 }
Please login to merge, or discard this patch.
src/CanvasPest_Exception.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -11,12 +11,12 @@
 block discarded – undo
11 11
  **/
12 12
 class CanvasPest_Exception extends \Exception
13 13
 {
14
-    /** The API access method is not supported by the Canvas API */
15
-    const UNSUPPORTED_METHOD = 1;
14
+	/** The API access method is not supported by the Canvas API */
15
+	const UNSUPPORTED_METHOD = 1;
16 16
 
17
-    /** The API access token provided is invalid */
18
-    const INVALID_TOKEN = 2;
17
+	/** The API access token provided is invalid */
18
+	const INVALID_TOKEN = 2;
19 19
 
20
-    /** Unanticipated JSON response from API */
21
-    const INVALID_JSON_RESPONSE = 3;
20
+	/** Unanticipated JSON response from API */
21
+	const INVALID_JSON_RESPONSE = 3;
22 22
 }
Please login to merge, or discard this patch.
src/CanvasPestImmutable_Exception.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -11,8 +11,8 @@
 block discarded – undo
11 11
  **/
12 12
 class CanvasPestImmutable_Exception extends CanvasPest_Exception
13 13
 {
14
-    /**
15
-     * @const IMMUTABLE A request to the API that would change data was attempted
16
-     **/
17
-    const IMMUTABLE = 1001;
14
+	/**
15
+	 * @const IMMUTABLE A request to the API that would change data was attempted
16
+	 **/
17
+	const IMMUTABLE = 1001;
18 18
 }
Please login to merge, or discard this patch.
src/CanvasPestImmutable.php 1 patch
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -18,69 +18,69 @@
 block discarded – undo
18 18
  **/
19 19
 class CanvasPestImmutable extends CanvasPest
20 20
 {
21
-    /**
22
-     * {@inheritDoc}
23
-     *
24
-     * @deprecated CanvasPestImmutable only supports GET calls to the API
25
-     *
26
-     * @param string $path Path to the API endpoint of this call
27
-     * @param string|string[] $data (Optional) Query parameters for this call
28
-     * @param string|string[] $headers (Optional) Any additional HTTP headers
29
-     *                                 for this call
30
-     * @return void
31
-     *
32
-     * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
33
-     *         will cause an exception
34
-     **/
35
-    public function put($path, $data = array(), $headers = array())
36
-    {
37
-        throw new CanvasPestImmutable_Exception(
38
-            'Only GET calls to the API are allowed from CanvasPestImmutable.',
39
-            CanvasPestImmutable_Exception::IMMUTABLE
40
-        );
41
-    }
21
+	/**
22
+	 * {@inheritDoc}
23
+	 *
24
+	 * @deprecated CanvasPestImmutable only supports GET calls to the API
25
+	 *
26
+	 * @param string $path Path to the API endpoint of this call
27
+	 * @param string|string[] $data (Optional) Query parameters for this call
28
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers
29
+	 *                                 for this call
30
+	 * @return void
31
+	 *
32
+	 * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
33
+	 *         will cause an exception
34
+	 **/
35
+	public function put($path, $data = array(), $headers = array())
36
+	{
37
+		throw new CanvasPestImmutable_Exception(
38
+			'Only GET calls to the API are allowed from CanvasPestImmutable.',
39
+			CanvasPestImmutable_Exception::IMMUTABLE
40
+		);
41
+	}
42 42
 
43
-    /**
44
-     * {@inheritDoc}
45
-     *
46
-     * @deprecated CanvasPestImmutable only supports GET calls to the API
47
-     *
48
-     * @param string $path Path to the API endpoint of this call
49
-     * @param string|string[] $data (Optional) Query parameters for this call
50
-     * @param string|string[] $headers (Optional) Any additional HTTP headers
51
-     *                                 for this call
52
-     * @return void
53
-     *
54
-     * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
55
-     *         will cause an exception
56
-     **/
57
-    public function post($path, $data = array(), $headers = array())
58
-    {
59
-        throw new CanvasPestImmutable_Exception(
60
-            'Only GET calls to the API are allowed from CanvasPestImmutable.',
61
-            CanvasPestImmutable_Exception::IMMUTABLE
62
-        );
63
-    }
43
+	/**
44
+	 * {@inheritDoc}
45
+	 *
46
+	 * @deprecated CanvasPestImmutable only supports GET calls to the API
47
+	 *
48
+	 * @param string $path Path to the API endpoint of this call
49
+	 * @param string|string[] $data (Optional) Query parameters for this call
50
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers
51
+	 *                                 for this call
52
+	 * @return void
53
+	 *
54
+	 * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
55
+	 *         will cause an exception
56
+	 **/
57
+	public function post($path, $data = array(), $headers = array())
58
+	{
59
+		throw new CanvasPestImmutable_Exception(
60
+			'Only GET calls to the API are allowed from CanvasPestImmutable.',
61
+			CanvasPestImmutable_Exception::IMMUTABLE
62
+		);
63
+	}
64 64
 
65
-    /**
66
-     * {@inheritDoc}
67
-     *
68
-     * @deprecated CanvasPestImmutable only supports GET calls to the API
69
-     *
70
-     * @param string $path Path to the API endpoint of this call
71
-     * @param string|string[] $data (Optional) Query parameters for this call
72
-     * @param string|string[] $headers (Optional) Any additional HTTP headers
73
-     *                                 for this call
74
-     * @return void
75
-     *
76
-     * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
77
-     *         will cause an exception
78
-     **/
79
-    public function delete($path, $data = array(), $headers = array())
80
-    {
81
-        throw new CanvasPestImmutable_Exception(
82
-            'Only GET calls to the API are allowed from CanvasPestImmutable.',
83
-            CanvasPestImmutable_Exception::IMMUTABLE
84
-        );
85
-    }
65
+	/**
66
+	 * {@inheritDoc}
67
+	 *
68
+	 * @deprecated CanvasPestImmutable only supports GET calls to the API
69
+	 *
70
+	 * @param string $path Path to the API endpoint of this call
71
+	 * @param string|string[] $data (Optional) Query parameters for this call
72
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers
73
+	 *                                 for this call
74
+	 * @return void
75
+	 *
76
+	 * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method
77
+	 *         will cause an exception
78
+	 **/
79
+	public function delete($path, $data = array(), $headers = array())
80
+	{
81
+		throw new CanvasPestImmutable_Exception(
82
+			'Only GET calls to the API are allowed from CanvasPestImmutable.',
83
+			CanvasPestImmutable_Exception::IMMUTABLE
84
+		);
85
+	}
86 86
 }
Please login to merge, or discard this patch.
src/CanvasPest.php 1 patch
Indentation   +245 added lines, -245 removed lines patch added patch discarded remove patch
@@ -20,261 +20,261 @@
 block discarded – undo
20 20
 class CanvasPest extends \Battis\Educoder\Pest
21 21
 {
22 22
 
23
-    /** Name of the parameter controlling the number of responses per page */
24
-    const PARAM_PER_PAGE = 'per_page';
23
+	/** Name of the parameter controlling the number of responses per page */
24
+	const PARAM_PER_PAGE = 'per_page';
25 25
 
26
-    /** @var string[] $headers Additional headers to be passed to the API with each call */
27
-    protected $headers;
26
+	/** @var string[] $headers Additional headers to be passed to the API with each call */
27
+	protected $headers;
28 28
 
29
-    /**
30
-     * Construct a new CanvasPest
31
-     *
32
-     * @api
33
-     *
34
-     * @param string $apiInstanceUrl URL of the API instance (e.g.
35
-     *        `'https://canvas.instructure.com/api/v1'`)
36
-     * @param string $apiAuthorizationToken (Optional) API access token for the
37
-     *        API instance (if not provided now, it will need to be provided later)
38
-     *
39
-     * @see CanvasPest::setupToken() To configure the API access token later
40
-     **/
41
-    public function __construct($apiInstanceUrl, $apiAuthorizationToken = null)
42
-    {
43
-        parent::__construct($apiInstanceUrl);
44
-        if (!empty($apiAuthorizationToken)) {
45
-            $this->setupToken($apiAuthorizationToken);
46
-        }
47
-    }
29
+	/**
30
+	 * Construct a new CanvasPest
31
+	 *
32
+	 * @api
33
+	 *
34
+	 * @param string $apiInstanceUrl URL of the API instance (e.g.
35
+	 *        `'https://canvas.instructure.com/api/v1'`)
36
+	 * @param string $apiAuthorizationToken (Optional) API access token for the
37
+	 *        API instance (if not provided now, it will need to be provided later)
38
+	 *
39
+	 * @see CanvasPest::setupToken() To configure the API access token later
40
+	 **/
41
+	public function __construct($apiInstanceUrl, $apiAuthorizationToken = null)
42
+	{
43
+		parent::__construct($apiInstanceUrl);
44
+		if (!empty($apiAuthorizationToken)) {
45
+			$this->setupToken($apiAuthorizationToken);
46
+		}
47
+	}
48 48
 
49
-    /**
50
-     * Set up a new API access token to access this instance
51
-     *
52
-     * @param string $token API access token
53
-     *
54
-     * @throws CanvasPest_Exception INVALID_TOKEN on an empty or non-string token value
55
-     **/
56
-    public function setupToken($token)
57
-    {
58
-        if (is_string($token) && !empty($token)) {
59
-            $this->headers['Authorization'] = "Bearer $token";
60
-        } elseif ($this->throw_exceptions) {
61
-            throw new CanvasPest_Exception(
62
-                'API authorization token must be a non-zero-length string',
63
-                CanvasPest_Exception::INVALID_TOKEN
64
-            );
65
-        }
66
-    }
49
+	/**
50
+	 * Set up a new API access token to access this instance
51
+	 *
52
+	 * @param string $token API access token
53
+	 *
54
+	 * @throws CanvasPest_Exception INVALID_TOKEN on an empty or non-string token value
55
+	 **/
56
+	public function setupToken($token)
57
+	{
58
+		if (is_string($token) && !empty($token)) {
59
+			$this->headers['Authorization'] = "Bearer $token";
60
+		} elseif ($this->throw_exceptions) {
61
+			throw new CanvasPest_Exception(
62
+				'API authorization token must be a non-zero-length string',
63
+				CanvasPest_Exception::INVALID_TOKEN
64
+			);
65
+		}
66
+	}
67 67
 
68
-    /**
69
-     * Preprocess API call parameters before use
70
-     *
71
-     * Force maximum response page size, if not already defined.
72
-     *
73
-     * @param string[] $data Array of parameters for the next API call
74
-     *
75
-     * @return string[] Updated array of parameters
76
-     *
77
-     * @see CanvasArray::MAXIMUM_PER_PAGE Maximum number of responses per page
78
-     * @see CanvasPest::PARAM_PER_PAGE Page size parameter
79
-     **/
80
-    private function preprocessData($data)
81
-    {
82
-        if (is_array($data) && !array_key_exists(self::PARAM_PER_PAGE, $data)) {
83
-            $data[self::PARAM_PER_PAGE] = CanvasArray::MAXIMUM_PER_PAGE;
84
-        }
85
-        return $data;
86
-    }
68
+	/**
69
+	 * Preprocess API call parameters before use
70
+	 *
71
+	 * Force maximum response page size, if not already defined.
72
+	 *
73
+	 * @param string[] $data Array of parameters for the next API call
74
+	 *
75
+	 * @return string[] Updated array of parameters
76
+	 *
77
+	 * @see CanvasArray::MAXIMUM_PER_PAGE Maximum number of responses per page
78
+	 * @see CanvasPest::PARAM_PER_PAGE Page size parameter
79
+	 **/
80
+	private function preprocessData($data)
81
+	{
82
+		if (is_array($data) && !array_key_exists(self::PARAM_PER_PAGE, $data)) {
83
+			$data[self::PARAM_PER_PAGE] = CanvasArray::MAXIMUM_PER_PAGE;
84
+		}
85
+		return $data;
86
+	}
87 87
 
88
-    /**
89
-     * Prepare API request headers
90
-     *
91
-     * Flatten headers from an associative array to a numerically indexed array
92
-     * of `"Name: Value"` style entries like `CURLOPT_HTTPHEADER` expects.
93
-     * Numerically indexed arrays are not modified.
94
-     *
95
-     * Extended by CanvasPest to include the API access token in the
96
-     * `Authorization` header.
97
-     *
98
-     * @param string[] $headers
99
-     * @return string[]
100
-     **/
101
-    protected function prepHeaders($headers)
102
-    {
103
-        return parent::prepHeaders(array_merge($this->headers, $headers));
104
-    }
88
+	/**
89
+	 * Prepare API request headers
90
+	 *
91
+	 * Flatten headers from an associative array to a numerically indexed array
92
+	 * of `"Name: Value"` style entries like `CURLOPT_HTTPHEADER` expects.
93
+	 * Numerically indexed arrays are not modified.
94
+	 *
95
+	 * Extended by CanvasPest to include the API access token in the
96
+	 * `Authorization` header.
97
+	 *
98
+	 * @param string[] $headers
99
+	 * @return string[]
100
+	 **/
101
+	protected function prepHeaders($headers)
102
+	{
103
+		return parent::prepHeaders(array_merge($this->headers, $headers));
104
+	}
105 105
 
106
-    /**
107
-     * Parse the API response into an object (or collection of objects).
108
-     *
109
-     * For queries to individually identified endpoints (e.g.
110
-     * `accounts/1/users/123`), return a CanvasObject representing the API response
111
-     * describing _that_ individually identified object affected by the query.
112
-     *
113
-     * For queries to generic endpoints (e.g. `accounts/1/users`), return a
114
-     * traversable CanvasArray (of CanvasObjects) representing the API response
115
-     * describing the list of objects affected by the query.
116
-     *
117
-     * @param string $response JSON-encoded response from the API
118
-     *
119
-     * @return CanvasObject|CanvasArray
120
-     **/
121
-    protected function postprocessResponse($response)
122
-    {
123
-        if (substr($response, 0, 1) == '{') {
124
-            return new CanvasObject($response);
125
-        } elseif (substr($response, 0, 1) == '[') {
126
-            return new CanvasArray($response, $this);
127
-        } else {
128
-            throw new CanvasPest_Exception(
129
-                $response,
130
-                CanvasPest_Exception::INVALID_JSON_RESPONSE
131
-            );
132
-        }
133
-    }
106
+	/**
107
+	 * Parse the API response into an object (or collection of objects).
108
+	 *
109
+	 * For queries to individually identified endpoints (e.g.
110
+	 * `accounts/1/users/123`), return a CanvasObject representing the API response
111
+	 * describing _that_ individually identified object affected by the query.
112
+	 *
113
+	 * For queries to generic endpoints (e.g. `accounts/1/users`), return a
114
+	 * traversable CanvasArray (of CanvasObjects) representing the API response
115
+	 * describing the list of objects affected by the query.
116
+	 *
117
+	 * @param string $response JSON-encoded response from the API
118
+	 *
119
+	 * @return CanvasObject|CanvasArray
120
+	 **/
121
+	protected function postprocessResponse($response)
122
+	{
123
+		if (substr($response, 0, 1) == '{') {
124
+			return new CanvasObject($response);
125
+		} elseif (substr($response, 0, 1) == '[') {
126
+			return new CanvasArray($response, $this);
127
+		} else {
128
+			throw new CanvasPest_Exception(
129
+				$response,
130
+				CanvasPest_Exception::INVALID_JSON_RESPONSE
131
+			);
132
+		}
133
+	}
134 134
 
135
-    /**
136
-     * Reformat query parameters for Canvas
137
-     *
138
-     * Specifically, Canvas expects no numeric indices for base array parameters.
139
-     *
140
-     * @param mixed $data
141
-     *
142
-     * @return string
143
-     **/
144
-    protected function http_build_query($data)
145
-    {
146
-        return preg_replace('/%5B\d+%5D/simU', '[]', http_build_query($data));
147
-    }
135
+	/**
136
+	 * Reformat query parameters for Canvas
137
+	 *
138
+	 * Specifically, Canvas expects no numeric indices for base array parameters.
139
+	 *
140
+	 * @param mixed $data
141
+	 *
142
+	 * @return string
143
+	 **/
144
+	protected function http_build_query($data)
145
+	{
146
+		return preg_replace('/%5B\d+%5D/simU', '[]', http_build_query($data));
147
+	}
148 148
 
149
-    /**
150
-     * Make a GET call to the API
151
-     *
152
-     * For queries to individually identified endpoints (e.g.
153
-     * `accounts/1/users/123`), return a CanvasObject representing the API response
154
-     * describing _that_ individually identified object affected by the query.
155
-     *
156
-     * For queries to generic endpoints (e.g. `accounts/1/users`), return a
157
-     * traversable CanvasArray (of CanvasObjects) representing the API response
158
-     * describing the list of objects affected by the query.
159
-     *
160
-     * @api
161
-     *
162
-     * @param string $path Path to the API endpoint of this call
163
-     * @param string|string[] $data (Optional) Query parameters for this call
164
-     * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
165
-     *
166
-     * @return CanvasObject|CanvasArray
167
-     **/
168
-    public function get($path, $data = array(), $headers = array())
169
-    {
170
-        return $this->postprocessResponse(
171
-            parent::get($path, $this->preprocessData($data), $headers)
172
-        );
173
-    }
149
+	/**
150
+	 * Make a GET call to the API
151
+	 *
152
+	 * For queries to individually identified endpoints (e.g.
153
+	 * `accounts/1/users/123`), return a CanvasObject representing the API response
154
+	 * describing _that_ individually identified object affected by the query.
155
+	 *
156
+	 * For queries to generic endpoints (e.g. `accounts/1/users`), return a
157
+	 * traversable CanvasArray (of CanvasObjects) representing the API response
158
+	 * describing the list of objects affected by the query.
159
+	 *
160
+	 * @api
161
+	 *
162
+	 * @param string $path Path to the API endpoint of this call
163
+	 * @param string|string[] $data (Optional) Query parameters for this call
164
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
165
+	 *
166
+	 * @return CanvasObject|CanvasArray
167
+	 **/
168
+	public function get($path, $data = array(), $headers = array())
169
+	{
170
+		return $this->postprocessResponse(
171
+			parent::get($path, $this->preprocessData($data), $headers)
172
+		);
173
+	}
174 174
 
175
-    /**
176
-     * Make a POST call to the API
177
-     *
178
-     * For queries to individually identified endpoints (e.g.
179
-     * `accounts/1/users/123`), return a CanvasObject representing the API response
180
-     * describing _that_ individually identified object affected by the query.
181
-     *
182
-     * For queries to generic endpoints (e.g. `accounts/1/users`), return a
183
-     * traversable CanvasArray (of CanvasObjects) representing the API response
184
-     * describing the list of objects affected by the query.
185
-     *
186
-     * @api
187
-     *
188
-     * @param string $path Path to the API endpoint of this call
189
-     * @param string|string[] $data (Optional) Query parameters for this call
190
-     * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
191
-     *
192
-     * @return CanvasObject|CanvasArray
193
-     **/
194
-    public function post($path, $data = array(), $headers = array())
195
-    {
196
-        return $this->postprocessResponse(
197
-            parent::post($path, $this->preprocessData($data), $headers)
198
-        );
199
-    }
175
+	/**
176
+	 * Make a POST call to the API
177
+	 *
178
+	 * For queries to individually identified endpoints (e.g.
179
+	 * `accounts/1/users/123`), return a CanvasObject representing the API response
180
+	 * describing _that_ individually identified object affected by the query.
181
+	 *
182
+	 * For queries to generic endpoints (e.g. `accounts/1/users`), return a
183
+	 * traversable CanvasArray (of CanvasObjects) representing the API response
184
+	 * describing the list of objects affected by the query.
185
+	 *
186
+	 * @api
187
+	 *
188
+	 * @param string $path Path to the API endpoint of this call
189
+	 * @param string|string[] $data (Optional) Query parameters for this call
190
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
191
+	 *
192
+	 * @return CanvasObject|CanvasArray
193
+	 **/
194
+	public function post($path, $data = array(), $headers = array())
195
+	{
196
+		return $this->postprocessResponse(
197
+			parent::post($path, $this->preprocessData($data), $headers)
198
+		);
199
+	}
200 200
 
201
-    /**
202
-     * Make a PUT call to the API
203
-     *
204
-     * For queries to individually identified endpoints (e.g.
205
-     * `accounts/1/users/123`), return a CanvasObject representing the API response
206
-     * describing _that_ individually identified object affected by the query.
207
-     *
208
-     * For queries to generic endpoints (e.g. `accounts/1/users`), return a
209
-     * traversable CanvasArray (of CanvasObjects) representing the API response
210
-     * describing the list of objects affected by the query.
211
-     *
212
-     * @api
213
-     *
214
-     * @param string $path Path to the API endpoint of this call
215
-     * @param string|string[] $data (Optional) Query parameters for this call
216
-     * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
217
-     *
218
-     * @return CanvasObject|CanvasArray
219
-     **/
220
-    public function put($path, $data = array(), $headers = array())
221
-    {
222
-        return $this->postprocessResponse(
223
-            parent::put($path, $this->preprocessData($data), $headers)
224
-        );
225
-    }
201
+	/**
202
+	 * Make a PUT call to the API
203
+	 *
204
+	 * For queries to individually identified endpoints (e.g.
205
+	 * `accounts/1/users/123`), return a CanvasObject representing the API response
206
+	 * describing _that_ individually identified object affected by the query.
207
+	 *
208
+	 * For queries to generic endpoints (e.g. `accounts/1/users`), return a
209
+	 * traversable CanvasArray (of CanvasObjects) representing the API response
210
+	 * describing the list of objects affected by the query.
211
+	 *
212
+	 * @api
213
+	 *
214
+	 * @param string $path Path to the API endpoint of this call
215
+	 * @param string|string[] $data (Optional) Query parameters for this call
216
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
217
+	 *
218
+	 * @return CanvasObject|CanvasArray
219
+	 **/
220
+	public function put($path, $data = array(), $headers = array())
221
+	{
222
+		return $this->postprocessResponse(
223
+			parent::put($path, $this->preprocessData($data), $headers)
224
+		);
225
+	}
226 226
 
227
-    /**
228
-     * Make a DELETE call to the API
229
-     *
230
-     * For queries to individually identified endpoints (e.g.
231
-     * `accounts/1/users/123`), return a CanvasObject representing the API response
232
-     * describing _that_ individually identified object affected by the query.
233
-     *
234
-     * For queries to generic endpoints (e.g. `accounts/1/users`), return a
235
-     * traversable CanvasArray (of CanvasObjects) representing the API response
236
-     * describing the list of objects affected by the query.
237
-     *
238
-     * @api
239
-     *
240
-     * @param string $path Path to the API endpoint of this call
241
-     * @param string|string[] $data (Optional) Query parameters for this call
242
-     * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
243
-     *
244
-     * @return CanvasObject|CanvasArray
245
-     **/
246
-    public function delete($path, $data = array(), $headers = array())
247
-    {
248
-        if (!empty($data)) {
249
-            $pos = strpos($path, '?');
250
-            if ($pos !== false) {
251
-                $path = substr($path, 0, $pos);
252
-            }
253
-            $path .= '?' . $this->http_build_query($data);
254
-        }
255
-        return $this->postprocessResponse(
256
-            parent::delete($path, $headers)
257
-        );
258
-    }
227
+	/**
228
+	 * Make a DELETE call to the API
229
+	 *
230
+	 * For queries to individually identified endpoints (e.g.
231
+	 * `accounts/1/users/123`), return a CanvasObject representing the API response
232
+	 * describing _that_ individually identified object affected by the query.
233
+	 *
234
+	 * For queries to generic endpoints (e.g. `accounts/1/users`), return a
235
+	 * traversable CanvasArray (of CanvasObjects) representing the API response
236
+	 * describing the list of objects affected by the query.
237
+	 *
238
+	 * @api
239
+	 *
240
+	 * @param string $path Path to the API endpoint of this call
241
+	 * @param string|string[] $data (Optional) Query parameters for this call
242
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
243
+	 *
244
+	 * @return CanvasObject|CanvasArray
245
+	 **/
246
+	public function delete($path, $data = array(), $headers = array())
247
+	{
248
+		if (!empty($data)) {
249
+			$pos = strpos($path, '?');
250
+			if ($pos !== false) {
251
+				$path = substr($path, 0, $pos);
252
+			}
253
+			$path .= '?' . $this->http_build_query($data);
254
+		}
255
+		return $this->postprocessResponse(
256
+			parent::delete($path, $headers)
257
+		);
258
+	}
259 259
 
260
-    /**
261
-     * Make a PATCH call to the API
262
-     *
263
-     * @deprecated The Canvas API does not currently support PATCH calls
264
-     *
265
-     * @param string $path Path to the API endpoint of this call
266
-     * @param string|string[] $data (Optional) Query parameters for this call
267
-     * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
268
-     *
269
-     * @return void
270
-     *
271
-     * @throws CanvasPest_Exception UNSUPPORTED_METHOD All calls to this method will cause an exception
272
-     **/
273
-    public function patch($path, $data = array(), $headers = array())
274
-    {
275
-        throw new CanvasPest_Exception(
276
-            'The Canvas API does not support the PATCH method',
277
-            CanvasPest_Exception::UNSUPPORTED_METHOD
278
-        );
279
-    }
260
+	/**
261
+	 * Make a PATCH call to the API
262
+	 *
263
+	 * @deprecated The Canvas API does not currently support PATCH calls
264
+	 *
265
+	 * @param string $path Path to the API endpoint of this call
266
+	 * @param string|string[] $data (Optional) Query parameters for this call
267
+	 * @param string|string[] $headers (Optional) Any additional HTTP headers for this call
268
+	 *
269
+	 * @return void
270
+	 *
271
+	 * @throws CanvasPest_Exception UNSUPPORTED_METHOD All calls to this method will cause an exception
272
+	 **/
273
+	public function patch($path, $data = array(), $headers = array())
274
+	{
275
+		throw new CanvasPest_Exception(
276
+			'The Canvas API does not support the PATCH method',
277
+			CanvasPest_Exception::UNSUPPORTED_METHOD
278
+		);
279
+	}
280 280
 }
Please login to merge, or discard this patch.
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.