@@ -10,6 +10,6 @@ |
||
| 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 | } |
@@ -18,54 +18,54 @@ |
||
| 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 | - * @return void |
|
| 27 | - * |
|
| 28 | - * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 29 | - **/ |
|
| 30 | - public function put($url, $data = array(), $headers = array()) |
|
| 31 | - { |
|
| 32 | - throw new CanvasPestImmutable_Exception( |
|
| 33 | - 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 34 | - CanvasPestImmutable_Exception::IMMUTABLE |
|
| 35 | - ); |
|
| 36 | - } |
|
| 21 | + /** |
|
| 22 | + * {@inheritDoc} |
|
| 23 | + * |
|
| 24 | + * @deprecated CanvasPestImmutable only supports GET calls to the API |
|
| 25 | + * |
|
| 26 | + * @return void |
|
| 27 | + * |
|
| 28 | + * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 29 | + **/ |
|
| 30 | + public function put($url, $data = array(), $headers = array()) |
|
| 31 | + { |
|
| 32 | + throw new CanvasPestImmutable_Exception( |
|
| 33 | + 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 34 | + CanvasPestImmutable_Exception::IMMUTABLE |
|
| 35 | + ); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * {@inheritDoc} |
|
| 40 | - * |
|
| 41 | - * @deprecated CanvasPestImmutable only supports GET calls to the API |
|
| 42 | - * |
|
| 43 | - * @return void |
|
| 44 | - * |
|
| 45 | - * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 46 | - **/ |
|
| 47 | - public function post($url, $data = array(), $headers = array()) |
|
| 48 | - { |
|
| 49 | - throw new CanvasPestImmutable_Exception( |
|
| 50 | - 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 51 | - CanvasPestImmutable_Exception::IMMUTABLE |
|
| 52 | - ); |
|
| 53 | - } |
|
| 38 | + /** |
|
| 39 | + * {@inheritDoc} |
|
| 40 | + * |
|
| 41 | + * @deprecated CanvasPestImmutable only supports GET calls to the API |
|
| 42 | + * |
|
| 43 | + * @return void |
|
| 44 | + * |
|
| 45 | + * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 46 | + **/ |
|
| 47 | + public function post($url, $data = array(), $headers = array()) |
|
| 48 | + { |
|
| 49 | + throw new CanvasPestImmutable_Exception( |
|
| 50 | + 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 51 | + CanvasPestImmutable_Exception::IMMUTABLE |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * {@inheritDoc} |
|
| 57 | - * |
|
| 58 | - * @deprecated CanvasPestImmutable only supports GET calls to the API |
|
| 59 | - * |
|
| 60 | - * @return void |
|
| 61 | - * |
|
| 62 | - * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 63 | - **/ |
|
| 64 | - public function delete($url, $data = array(), $headers = array()) |
|
| 65 | - { |
|
| 66 | - throw new CanvasPestImmutable_Exception( |
|
| 67 | - 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 68 | - CanvasPestImmutable_Exception::IMMUTABLE |
|
| 69 | - ); |
|
| 70 | - } |
|
| 55 | + /** |
|
| 56 | + * {@inheritDoc} |
|
| 57 | + * |
|
| 58 | + * @deprecated CanvasPestImmutable only supports GET calls to the API |
|
| 59 | + * |
|
| 60 | + * @return void |
|
| 61 | + * |
|
| 62 | + * @throws CanvasPestImmutable_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 63 | + **/ |
|
| 64 | + public function delete($url, $data = array(), $headers = array()) |
|
| 65 | + { |
|
| 66 | + throw new CanvasPestImmutable_Exception( |
|
| 67 | + 'Only GET calls to the API are allowed from CanvasPestImmutable.', |
|
| 68 | + CanvasPestImmutable_Exception::IMMUTABLE |
|
| 69 | + ); |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -16,187 +16,187 @@ |
||
| 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 | - if (is_numeric($this->params[self::PARAM_PAGE_NUMBER])) { |
|
| 167 | - return $this->params[self::PARAM_PAGE_NUMBER]; |
|
| 168 | - } elseif ($this->params[self::PARAM_PAGE_NUMBER] == 'first') { |
|
| 169 | - return 1; /* weirdly, api/v1/users/<user_id>/logins returns first instead of page numbers */ |
|
| 170 | - } else { |
|
| 171 | - return 1; // FIXME probably not good to assume that this is safe |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * The number of responses per page generating this pagination |
|
| 177 | - * |
|
| 178 | - * @return int |
|
| 179 | - **/ |
|
| 180 | - public function getPerPage() |
|
| 181 | - { |
|
| 182 | - return $this->params[self::PARAM_PER_PAGE]; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * An array representation of the CanvasArray |
|
| 187 | - * |
|
| 188 | - * @return array |
|
| 189 | - **/ |
|
| 190 | - public function getArrayCopy() |
|
| 191 | - { |
|
| 192 | - $arr = array(); |
|
| 193 | - $_key = $this->key; |
|
| 194 | - $_page = $this->page; |
|
| 195 | - foreach ($this as $obj) { |
|
| 196 | - $arr[] = $obj->getArrayCopy(); |
|
| 197 | - } |
|
| 198 | - $this->page = $_page; |
|
| 199 | - $this->key = $_key; |
|
| 200 | - return $arr; |
|
| 201 | - } |
|
| 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 | + if (is_numeric($this->params[self::PARAM_PAGE_NUMBER])) { |
|
| 167 | + return $this->params[self::PARAM_PAGE_NUMBER]; |
|
| 168 | + } elseif ($this->params[self::PARAM_PAGE_NUMBER] == 'first') { |
|
| 169 | + return 1; /* weirdly, api/v1/users/<user_id>/logins returns first instead of page numbers */ |
|
| 170 | + } else { |
|
| 171 | + return 1; // FIXME probably not good to assume that this is safe |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * The number of responses per page generating this pagination |
|
| 177 | + * |
|
| 178 | + * @return int |
|
| 179 | + **/ |
|
| 180 | + public function getPerPage() |
|
| 181 | + { |
|
| 182 | + return $this->params[self::PARAM_PER_PAGE]; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * An array representation of the CanvasArray |
|
| 187 | + * |
|
| 188 | + * @return array |
|
| 189 | + **/ |
|
| 190 | + public function getArrayCopy() |
|
| 191 | + { |
|
| 192 | + $arr = array(); |
|
| 193 | + $_key = $this->key; |
|
| 194 | + $_page = $this->page; |
|
| 195 | + foreach ($this as $obj) { |
|
| 196 | + $arr[] = $obj->getArrayCopy(); |
|
| 197 | + } |
|
| 198 | + $this->page = $_page; |
|
| 199 | + $this->key = $_key; |
|
| 200 | + return $arr; |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -11,6 +11,6 @@ |
||
| 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 | } |
@@ -11,6 +11,6 @@ |
||
| 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 | } |
@@ -18,214 +18,214 @@ |
||
| 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 | } |
@@ -11,12 +11,12 @@ |
||
| 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 | } |
@@ -20,257 +20,257 @@ |
||
| 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 | - * @return void |
|
| 266 | - * |
|
| 267 | - * @throws CanvasPest_Exception UNSUPPORTED_METHOD All calls to this method will cause an exception |
|
| 268 | - **/ |
|
| 269 | - public function patch($url, $data = array(), $headers = array()) |
|
| 270 | - { |
|
| 271 | - throw new CanvasPest_Exception( |
|
| 272 | - 'The Canvas API does not support the PATCH method', |
|
| 273 | - CanvasPest_Exception::UNSUPPORTED_METHOD |
|
| 274 | - ); |
|
| 275 | - } |
|
| 260 | + /** |
|
| 261 | + * Make a PATCH call to the API |
|
| 262 | + * |
|
| 263 | + * @deprecated The Canvas API does not currently support PATCH calls |
|
| 264 | + * |
|
| 265 | + * @return void |
|
| 266 | + * |
|
| 267 | + * @throws CanvasPest_Exception UNSUPPORTED_METHOD All calls to this method will cause an exception |
|
| 268 | + **/ |
|
| 269 | + public function patch($url, $data = array(), $headers = array()) |
|
| 270 | + { |
|
| 271 | + throw new CanvasPest_Exception( |
|
| 272 | + 'The Canvas API does not support the PATCH method', |
|
| 273 | + CanvasPest_Exception::UNSUPPORTED_METHOD |
|
| 274 | + ); |
|
| 275 | + } |
|
| 276 | 276 | } |
@@ -12,448 +12,448 @@ |
||
| 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, prev, current) |
|
| 29 | - * pages relative to the current page of responses |
|
| 30 | - **/ |
|
| 31 | - private $pagination = array(); |
|
| 32 | - |
|
| 33 | - /** @var CanvasObject[] $data Backing store */ |
|
| 34 | - private $data = array(); |
|
| 35 | - |
|
| 36 | - /** @var int $page Page number corresponding to current $key */ |
|
| 37 | - private $page = null; |
|
| 38 | - |
|
| 39 | - /** @var int $key Current key-value of iterator */ |
|
| 40 | - private $key = null; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Construct a CanvasArray |
|
| 44 | - * |
|
| 45 | - * @param string $jsonResponse A JSON-encoded response array from the Canvas API |
|
| 46 | - * @param CanvasPest $canvasPest An API object for making pagination calls |
|
| 47 | - **/ |
|
| 48 | - public function __construct($jsonResponse, $canvasPest) |
|
| 49 | - { |
|
| 50 | - $this->api = $canvasPest; |
|
| 51 | - |
|
| 52 | - /* parse Canvas page links */ |
|
| 53 | - if (preg_match_all('%<([^>]*)>\s*;\s*rel="([^"]+)"%', $this->api->lastHeader('link'), $links, PREG_SET_ORDER)) { |
|
| 54 | - foreach ($links as $link) { |
|
| 55 | - $this->pagination[$link[2]] = new CanvasPageLink($link[1], $link[2]); |
|
| 56 | - } |
|
| 57 | - } else { |
|
| 58 | - $this->pagination = array(); // might only be one page of results |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /* locate ourselves */ |
|
| 62 | - if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 63 | - $this->page = $this->pagination[CanvasPageLink::CURRENT]->getPageNumber(); |
|
| 64 | - } else { |
|
| 65 | - $this->page = 1; // assume only one page (since no pagination) |
|
| 66 | - } |
|
| 67 | - $this->key = $this->pageNumberToKey($this->page); |
|
| 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 | - * Convert a page number to an array key |
|
| 77 | - * |
|
| 78 | - * @param int $pageNumber 1-indexed page number |
|
| 79 | - * |
|
| 80 | - * @return int |
|
| 81 | - * |
|
| 82 | - * @throws CanvasArray_Exception INVALID_PAGE_NUMBER If $pageNumber < 1 |
|
| 83 | - **/ |
|
| 84 | - private function pageNumberToKey($pageNumber) |
|
| 85 | - { |
|
| 86 | - if ($pageNumber < 1) { |
|
| 87 | - throw new CanvasArray_Exception( |
|
| 88 | - "{$pageNumber} is not a valid page number", |
|
| 89 | - CanvasArray_Exception::INVALID_PAGE_NUMBER |
|
| 90 | - ); |
|
| 91 | - } |
|
| 92 | - if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 93 | - return ($pageNumber - 1) * $this->pagination[CanvasPageLink::CURRENT]->getPerPage(); |
|
| 94 | - } else { |
|
| 95 | - return 0; // assume only one page (since no pagination); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Convert an array key to a page number |
|
| 101 | - * |
|
| 102 | - * @param int $key Non-negative array key |
|
| 103 | - * |
|
| 104 | - * @return int |
|
| 105 | - * |
|
| 106 | - * @throws CanvasArray_Exception INVALID_ARRAY_KEY If $key < 0 |
|
| 107 | - **/ |
|
| 108 | - private function keyToPageNumber($key) |
|
| 109 | - { |
|
| 110 | - if ($key < 0) { |
|
| 111 | - throw new CanvasArray_Exception( |
|
| 112 | - "$key is not a valid array key", |
|
| 113 | - CanvasArray_Exception::INVALID_ARRAY_KEY |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 118 | - return ((int) ($key / $this->pagination[CanvasPageLink::CURRENT]->getPerPage())) + 1; |
|
| 119 | - } else { |
|
| 120 | - return 1; // assume single page if no pagination |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Request a page of responses from the API |
|
| 126 | - * |
|
| 127 | - * A page of responses will be requested if it appears that that page has not |
|
| 128 | - * yet been loaded (tested by checking if the initial element of the page has |
|
| 129 | - * been initialized in the $data array). |
|
| 130 | - * |
|
| 131 | - * @param int $pageNumber Page number to request |
|
| 132 | - * @param bool $forceRefresh (Optional) Force a refresh of backing data, even |
|
| 133 | - * if cached (defaults to `FALSE`) |
|
| 134 | - * |
|
| 135 | - * @return bool `TRUE` if the page is requested, `FALSE` if it is already cached |
|
| 136 | - * (and therefore not requested) |
|
| 137 | - **/ |
|
| 138 | - private function requestPageNumber($pageNumber, $forceRefresh = false) |
|
| 139 | - { |
|
| 140 | - if (!isset($this->data[$this->pageNumberToKey($pageNumber)]) || ($forceRefresh && isset($this->api))) { |
|
| 141 | - // assume one page if no pagination (and already loaded) |
|
| 142 | - if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 143 | - $params = $this->pagination[CanvasPageLink::CURRENT]->getParams(); |
|
| 144 | - $params[CanvasPageLink::PARAM_PAGE_NUMBER] = $pageNumber; |
|
| 145 | - $page = $this->api->get($this->pagination[CanvasPageLink::CURRENT]->getEndpoint(), $params); |
|
| 146 | - $this->data = array_replace($this->data, $page->data); |
|
| 147 | - return true; |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Request all pages from API |
|
| 155 | - * |
|
| 156 | - * This stores the entire API response locally, in preparation for, most |
|
| 157 | - * likely, serializing this object. |
|
| 158 | - * |
|
| 159 | - * @param bool $forceRefresh (Optional) Force a refresh of backing data, even |
|
| 160 | - * if cached (defaults to `FALSE`) |
|
| 161 | - * |
|
| 162 | - * @return void |
|
| 163 | - */ |
|
| 164 | - private function requestAllPages($forceRefresh = false) |
|
| 165 | - { |
|
| 166 | - $_page = $this->page; |
|
| 167 | - $_key = $this->key; |
|
| 168 | - |
|
| 169 | - if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 170 | - for ($page = 1; $page <= $this->pagination[CanvasPageLink::LAST]->getPageNumber(); $page++) { |
|
| 171 | - $this->requestPageNumber($page, $forceRefresh); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - $this->page = $_page; |
|
| 176 | - $this->key = $_key; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Rewind the iterator to a specific page of data |
|
| 181 | - * |
|
| 182 | - * If the page of data is already cached, it will not (by default) be reloaded |
|
| 183 | - * from the API, although this can be overridden with the $forceRefresh |
|
| 184 | - * parameter. |
|
| 185 | - * |
|
| 186 | - * @param int $pageNumber Page number to rewind to |
|
| 187 | - * @param bool $forceRefresh (Optional) Defaults to `FALSE` |
|
| 188 | - * |
|
| 189 | - * @return void |
|
| 190 | - **/ |
|
| 191 | - private function rewindToPageNumber($pageNumber, $forceRefresh = false) |
|
| 192 | - { |
|
| 193 | - $page = null; |
|
| 194 | - $key = $this->pageNumberToKey($pageNumber); |
|
| 195 | - if ($forceRefresh || !isset($this->data[$key])) { |
|
| 196 | - $page = $this->requestPageNumber($pageNumber, $forceRefresh); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $this->key = $key; |
|
| 200 | - $this->page = $pageNumber; |
|
| 201 | - $this->pagination[CanvasPageLink::PREV] = new CanvasPageLink( |
|
| 202 | - $pageNumber, |
|
| 203 | - $this->pagination[CanvasPageLink::FIRST], |
|
| 204 | - CanvasPageLink::PREV |
|
| 205 | - ); |
|
| 206 | - $this->pagination[CanvasPageLink::NEXT] = new CanvasPageLink( |
|
| 207 | - $pageNumber, |
|
| 208 | - $this->pagination[CanvasPageLink::FIRST], |
|
| 209 | - CanvasPageLink::NEXT |
|
| 210 | - ); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /*************************************************************************** |
|
| 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, prev, current) |
|
| 29 | + * pages relative to the current page of responses |
|
| 30 | + **/ |
|
| 31 | + private $pagination = array(); |
|
| 32 | + |
|
| 33 | + /** @var CanvasObject[] $data Backing store */ |
|
| 34 | + private $data = array(); |
|
| 35 | + |
|
| 36 | + /** @var int $page Page number corresponding to current $key */ |
|
| 37 | + private $page = null; |
|
| 38 | + |
|
| 39 | + /** @var int $key Current key-value of iterator */ |
|
| 40 | + private $key = null; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Construct a CanvasArray |
|
| 44 | + * |
|
| 45 | + * @param string $jsonResponse A JSON-encoded response array from the Canvas API |
|
| 46 | + * @param CanvasPest $canvasPest An API object for making pagination calls |
|
| 47 | + **/ |
|
| 48 | + public function __construct($jsonResponse, $canvasPest) |
|
| 49 | + { |
|
| 50 | + $this->api = $canvasPest; |
|
| 51 | + |
|
| 52 | + /* parse Canvas page links */ |
|
| 53 | + if (preg_match_all('%<([^>]*)>\s*;\s*rel="([^"]+)"%', $this->api->lastHeader('link'), $links, PREG_SET_ORDER)) { |
|
| 54 | + foreach ($links as $link) { |
|
| 55 | + $this->pagination[$link[2]] = new CanvasPageLink($link[1], $link[2]); |
|
| 56 | + } |
|
| 57 | + } else { |
|
| 58 | + $this->pagination = array(); // might only be one page of results |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /* locate ourselves */ |
|
| 62 | + if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 63 | + $this->page = $this->pagination[CanvasPageLink::CURRENT]->getPageNumber(); |
|
| 64 | + } else { |
|
| 65 | + $this->page = 1; // assume only one page (since no pagination) |
|
| 66 | + } |
|
| 67 | + $this->key = $this->pageNumberToKey($this->page); |
|
| 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 | + * Convert a page number to an array key |
|
| 77 | + * |
|
| 78 | + * @param int $pageNumber 1-indexed page number |
|
| 79 | + * |
|
| 80 | + * @return int |
|
| 81 | + * |
|
| 82 | + * @throws CanvasArray_Exception INVALID_PAGE_NUMBER If $pageNumber < 1 |
|
| 83 | + **/ |
|
| 84 | + private function pageNumberToKey($pageNumber) |
|
| 85 | + { |
|
| 86 | + if ($pageNumber < 1) { |
|
| 87 | + throw new CanvasArray_Exception( |
|
| 88 | + "{$pageNumber} is not a valid page number", |
|
| 89 | + CanvasArray_Exception::INVALID_PAGE_NUMBER |
|
| 90 | + ); |
|
| 91 | + } |
|
| 92 | + if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 93 | + return ($pageNumber - 1) * $this->pagination[CanvasPageLink::CURRENT]->getPerPage(); |
|
| 94 | + } else { |
|
| 95 | + return 0; // assume only one page (since no pagination); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Convert an array key to a page number |
|
| 101 | + * |
|
| 102 | + * @param int $key Non-negative array key |
|
| 103 | + * |
|
| 104 | + * @return int |
|
| 105 | + * |
|
| 106 | + * @throws CanvasArray_Exception INVALID_ARRAY_KEY If $key < 0 |
|
| 107 | + **/ |
|
| 108 | + private function keyToPageNumber($key) |
|
| 109 | + { |
|
| 110 | + if ($key < 0) { |
|
| 111 | + throw new CanvasArray_Exception( |
|
| 112 | + "$key is not a valid array key", |
|
| 113 | + CanvasArray_Exception::INVALID_ARRAY_KEY |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 118 | + return ((int) ($key / $this->pagination[CanvasPageLink::CURRENT]->getPerPage())) + 1; |
|
| 119 | + } else { |
|
| 120 | + return 1; // assume single page if no pagination |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Request a page of responses from the API |
|
| 126 | + * |
|
| 127 | + * A page of responses will be requested if it appears that that page has not |
|
| 128 | + * yet been loaded (tested by checking if the initial element of the page has |
|
| 129 | + * been initialized in the $data array). |
|
| 130 | + * |
|
| 131 | + * @param int $pageNumber Page number to request |
|
| 132 | + * @param bool $forceRefresh (Optional) Force a refresh of backing data, even |
|
| 133 | + * if cached (defaults to `FALSE`) |
|
| 134 | + * |
|
| 135 | + * @return bool `TRUE` if the page is requested, `FALSE` if it is already cached |
|
| 136 | + * (and therefore not requested) |
|
| 137 | + **/ |
|
| 138 | + private function requestPageNumber($pageNumber, $forceRefresh = false) |
|
| 139 | + { |
|
| 140 | + if (!isset($this->data[$this->pageNumberToKey($pageNumber)]) || ($forceRefresh && isset($this->api))) { |
|
| 141 | + // assume one page if no pagination (and already loaded) |
|
| 142 | + if (isset($this->pagination[CanvasPageLink::CURRENT])) { |
|
| 143 | + $params = $this->pagination[CanvasPageLink::CURRENT]->getParams(); |
|
| 144 | + $params[CanvasPageLink::PARAM_PAGE_NUMBER] = $pageNumber; |
|
| 145 | + $page = $this->api->get($this->pagination[CanvasPageLink::CURRENT]->getEndpoint(), $params); |
|
| 146 | + $this->data = array_replace($this->data, $page->data); |
|
| 147 | + return true; |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Request all pages from API |
|
| 155 | + * |
|
| 156 | + * This stores the entire API response locally, in preparation for, most |
|
| 157 | + * likely, serializing this object. |
|
| 158 | + * |
|
| 159 | + * @param bool $forceRefresh (Optional) Force a refresh of backing data, even |
|
| 160 | + * if cached (defaults to `FALSE`) |
|
| 161 | + * |
|
| 162 | + * @return void |
|
| 163 | + */ |
|
| 164 | + private function requestAllPages($forceRefresh = false) |
|
| 165 | + { |
|
| 166 | + $_page = $this->page; |
|
| 167 | + $_key = $this->key; |
|
| 168 | + |
|
| 169 | + if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 170 | + for ($page = 1; $page <= $this->pagination[CanvasPageLink::LAST]->getPageNumber(); $page++) { |
|
| 171 | + $this->requestPageNumber($page, $forceRefresh); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + $this->page = $_page; |
|
| 176 | + $this->key = $_key; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Rewind the iterator to a specific page of data |
|
| 181 | + * |
|
| 182 | + * If the page of data is already cached, it will not (by default) be reloaded |
|
| 183 | + * from the API, although this can be overridden with the $forceRefresh |
|
| 184 | + * parameter. |
|
| 185 | + * |
|
| 186 | + * @param int $pageNumber Page number to rewind to |
|
| 187 | + * @param bool $forceRefresh (Optional) Defaults to `FALSE` |
|
| 188 | + * |
|
| 189 | + * @return void |
|
| 190 | + **/ |
|
| 191 | + private function rewindToPageNumber($pageNumber, $forceRefresh = false) |
|
| 192 | + { |
|
| 193 | + $page = null; |
|
| 194 | + $key = $this->pageNumberToKey($pageNumber); |
|
| 195 | + if ($forceRefresh || !isset($this->data[$key])) { |
|
| 196 | + $page = $this->requestPageNumber($pageNumber, $forceRefresh); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $this->key = $key; |
|
| 200 | + $this->page = $pageNumber; |
|
| 201 | + $this->pagination[CanvasPageLink::PREV] = new CanvasPageLink( |
|
| 202 | + $pageNumber, |
|
| 203 | + $this->pagination[CanvasPageLink::FIRST], |
|
| 204 | + CanvasPageLink::PREV |
|
| 205 | + ); |
|
| 206 | + $this->pagination[CanvasPageLink::NEXT] = new CanvasPageLink( |
|
| 207 | + $pageNumber, |
|
| 208 | + $this->pagination[CanvasPageLink::FIRST], |
|
| 209 | + CanvasPageLink::NEXT |
|
| 210 | + ); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /*************************************************************************** |
|
| 214 | 214 | * ArrayObject methods |
| 215 | 215 | */ |
| 216 | 216 | |
| 217 | - /** |
|
| 218 | - * Get the number of CanvasObjects in the Canvas response |
|
| 219 | - * |
|
| 220 | - * @return int |
|
| 221 | - * |
|
| 222 | - * @see http://php.net/manual/en/arrayobject.count.php ArrayObject::count |
|
| 223 | - **/ |
|
| 224 | - public function count() |
|
| 225 | - { |
|
| 226 | - if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 227 | - $this->requestPageNumber($this->pagination[CanvasPageLink::LAST]->getPageNumber()); |
|
| 228 | - if (!end($this->data)) { |
|
| 229 | - return 0; |
|
| 230 | - } |
|
| 231 | - return key($this->data) + 1; |
|
| 232 | - } else { |
|
| 233 | - return count($this->data); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Creates a copy of the CanvasArray |
|
| 239 | - * |
|
| 240 | - * @return CanvasObject[] |
|
| 241 | - * |
|
| 242 | - * @see http://php.net/manual/en/arrayobject.getarraycopy.php ArrayObject::getArrayCopy |
|
| 243 | - **/ |
|
| 244 | - public function getArrayCopy() |
|
| 245 | - { |
|
| 246 | - $_key = $this->key; |
|
| 247 | - $this->rewindToPageNumber(1); |
|
| 248 | - while (isset($this->pagination[CanvasPageLink::NEXT])) { |
|
| 249 | - $this->rewindToPageNumber($this->pagination[CanvasPageLink::NEXT]); |
|
| 250 | - } |
|
| 251 | - $this->key = $_key; |
|
| 252 | - return $this->data; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /*************************************************************************** |
|
| 217 | + /** |
|
| 218 | + * Get the number of CanvasObjects in the Canvas response |
|
| 219 | + * |
|
| 220 | + * @return int |
|
| 221 | + * |
|
| 222 | + * @see http://php.net/manual/en/arrayobject.count.php ArrayObject::count |
|
| 223 | + **/ |
|
| 224 | + public function count() |
|
| 225 | + { |
|
| 226 | + if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 227 | + $this->requestPageNumber($this->pagination[CanvasPageLink::LAST]->getPageNumber()); |
|
| 228 | + if (!end($this->data)) { |
|
| 229 | + return 0; |
|
| 230 | + } |
|
| 231 | + return key($this->data) + 1; |
|
| 232 | + } else { |
|
| 233 | + return count($this->data); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Creates a copy of the CanvasArray |
|
| 239 | + * |
|
| 240 | + * @return CanvasObject[] |
|
| 241 | + * |
|
| 242 | + * @see http://php.net/manual/en/arrayobject.getarraycopy.php ArrayObject::getArrayCopy |
|
| 243 | + **/ |
|
| 244 | + public function getArrayCopy() |
|
| 245 | + { |
|
| 246 | + $_key = $this->key; |
|
| 247 | + $this->rewindToPageNumber(1); |
|
| 248 | + while (isset($this->pagination[CanvasPageLink::NEXT])) { |
|
| 249 | + $this->rewindToPageNumber($this->pagination[CanvasPageLink::NEXT]); |
|
| 250 | + } |
|
| 251 | + $this->key = $_key; |
|
| 252 | + return $this->data; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /*************************************************************************** |
|
| 256 | 256 | * ArrayAccess methods |
| 257 | 257 | */ |
| 258 | 258 | |
| 259 | - /** |
|
| 260 | - * Whether an offset exists |
|
| 261 | - * |
|
| 262 | - * @param int|string $offset |
|
| 263 | - * |
|
| 264 | - * @return bool |
|
| 265 | - * |
|
| 266 | - * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists |
|
| 267 | - **/ |
|
| 268 | - public function offsetExists($offset) |
|
| 269 | - { |
|
| 270 | - if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 271 | - $lastPageNumber = $this->pagination[CanvasPageLink::LAST]->getPageNumber(); |
|
| 272 | - if ($this->keyToPageNumber($offset) == $lastPageNumber && |
|
| 273 | - !isset($this->data[$this->pageNumberToKey($lastPageNumber)])) { |
|
| 274 | - $this->requestPageNumber($lastPageNumber); |
|
| 275 | - } |
|
| 276 | - return isset($this->data[$offset]) || ($offset >= 0 && $offset < $this->pageNumberToKey($lastPageNumber)); |
|
| 277 | - } else { |
|
| 278 | - return isset($this->data[$offset]); |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Offset to retrieve |
|
| 284 | - * |
|
| 285 | - * @param int|string $offset |
|
| 286 | - * |
|
| 287 | - * @return CanvasObject|null |
|
| 288 | - * |
|
| 289 | - * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetGet |
|
| 290 | - **/ |
|
| 291 | - public function offsetGet($offset) |
|
| 292 | - { |
|
| 293 | - if ($this->offsetExists($offset) && !isset($this->data[$offset])) { |
|
| 294 | - $this->requestPageNumber($this->keyToPageNumber($offset)); |
|
| 295 | - } |
|
| 296 | - return $this->data[$offset]; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Assign a value to the specified offset |
|
| 301 | - * |
|
| 302 | - * @deprecated Canvas responses are immutable |
|
| 303 | - * |
|
| 304 | - * @param int|string $offset |
|
| 305 | - * @param CanvasObject $value |
|
| 306 | - * |
|
| 307 | - * @return void |
|
| 308 | - * |
|
| 309 | - * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 310 | - * |
|
| 311 | - * @see http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet |
|
| 312 | - **/ |
|
| 313 | - public function offsetSet($offset, $value) |
|
| 314 | - { |
|
| 315 | - throw new CanvasArray_Exception( |
|
| 316 | - 'Canvas responses are immutable', |
|
| 317 | - CanvasArray_Exception::IMMUTABLE |
|
| 318 | - ); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Unset an offset |
|
| 323 | - * |
|
| 324 | - * @deprecated Canvas responses are immutable |
|
| 325 | - * |
|
| 326 | - * @param int|string $offset |
|
| 327 | - * |
|
| 328 | - * @return void |
|
| 329 | - * |
|
| 330 | - * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 331 | - * |
|
| 332 | - * @see http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset |
|
| 333 | - **/ |
|
| 334 | - public function offsetUnset($offset) |
|
| 335 | - { |
|
| 336 | - throw new CanvasArray_Exception( |
|
| 337 | - 'Canvas responses are immutable', |
|
| 338 | - CanvasArray_Exception::IMMUTABLE |
|
| 339 | - ); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /**************************************************************************/ |
|
| 343 | - |
|
| 344 | - /************************************************************************** |
|
| 259 | + /** |
|
| 260 | + * Whether an offset exists |
|
| 261 | + * |
|
| 262 | + * @param int|string $offset |
|
| 263 | + * |
|
| 264 | + * @return bool |
|
| 265 | + * |
|
| 266 | + * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetExists |
|
| 267 | + **/ |
|
| 268 | + public function offsetExists($offset) |
|
| 269 | + { |
|
| 270 | + if (isset($this->pagination[CanvasPageLink::LAST])) { |
|
| 271 | + $lastPageNumber = $this->pagination[CanvasPageLink::LAST]->getPageNumber(); |
|
| 272 | + if ($this->keyToPageNumber($offset) == $lastPageNumber && |
|
| 273 | + !isset($this->data[$this->pageNumberToKey($lastPageNumber)])) { |
|
| 274 | + $this->requestPageNumber($lastPageNumber); |
|
| 275 | + } |
|
| 276 | + return isset($this->data[$offset]) || ($offset >= 0 && $offset < $this->pageNumberToKey($lastPageNumber)); |
|
| 277 | + } else { |
|
| 278 | + return isset($this->data[$offset]); |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Offset to retrieve |
|
| 284 | + * |
|
| 285 | + * @param int|string $offset |
|
| 286 | + * |
|
| 287 | + * @return CanvasObject|null |
|
| 288 | + * |
|
| 289 | + * @see http://php.net/manual/en/arrayaccess.offsetexists.php ArrayAccess::offsetGet |
|
| 290 | + **/ |
|
| 291 | + public function offsetGet($offset) |
|
| 292 | + { |
|
| 293 | + if ($this->offsetExists($offset) && !isset($this->data[$offset])) { |
|
| 294 | + $this->requestPageNumber($this->keyToPageNumber($offset)); |
|
| 295 | + } |
|
| 296 | + return $this->data[$offset]; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Assign a value to the specified offset |
|
| 301 | + * |
|
| 302 | + * @deprecated Canvas responses are immutable |
|
| 303 | + * |
|
| 304 | + * @param int|string $offset |
|
| 305 | + * @param CanvasObject $value |
|
| 306 | + * |
|
| 307 | + * @return void |
|
| 308 | + * |
|
| 309 | + * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 310 | + * |
|
| 311 | + * @see http://php.net/manual/en/arrayaccess.offsetset.php ArrayAccess::offsetSet |
|
| 312 | + **/ |
|
| 313 | + public function offsetSet($offset, $value) |
|
| 314 | + { |
|
| 315 | + throw new CanvasArray_Exception( |
|
| 316 | + 'Canvas responses are immutable', |
|
| 317 | + CanvasArray_Exception::IMMUTABLE |
|
| 318 | + ); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Unset an offset |
|
| 323 | + * |
|
| 324 | + * @deprecated Canvas responses are immutable |
|
| 325 | + * |
|
| 326 | + * @param int|string $offset |
|
| 327 | + * |
|
| 328 | + * @return void |
|
| 329 | + * |
|
| 330 | + * @throws CanvasArray_Exception IMMUTABLE All calls to this method will cause an exception |
|
| 331 | + * |
|
| 332 | + * @see http://php.net/manual/en/arrayaccess.offsetunset.php ArrayAccess::offsetUnset |
|
| 333 | + **/ |
|
| 334 | + public function offsetUnset($offset) |
|
| 335 | + { |
|
| 336 | + throw new CanvasArray_Exception( |
|
| 337 | + 'Canvas responses are immutable', |
|
| 338 | + CanvasArray_Exception::IMMUTABLE |
|
| 339 | + ); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /**************************************************************************/ |
|
| 343 | + |
|
| 344 | + /************************************************************************** |
|
| 345 | 345 | * Iterator methods |
| 346 | 346 | */ |
| 347 | 347 | |
| 348 | - /** |
|
| 349 | - * Return the current element |
|
| 350 | - * |
|
| 351 | - * @return CanvasObject |
|
| 352 | - * |
|
| 353 | - * @see http://php.net/manual/en/iterator.current.php Iterator::current |
|
| 354 | - **/ |
|
| 355 | - public function current() |
|
| 356 | - { |
|
| 357 | - if (!isset($this->data[$this->key])) { |
|
| 358 | - $this->requestPageNumber($this->keyToPageNumber($this->key)); |
|
| 359 | - } |
|
| 360 | - return $this->data[$this->key]; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Return the key of the current element |
|
| 365 | - * |
|
| 366 | - * @return int |
|
| 367 | - * |
|
| 368 | - * @see http://php.net/manual/en/iterator.key.php Iterator::key |
|
| 369 | - **/ |
|
| 370 | - public function key() |
|
| 371 | - { |
|
| 372 | - return $this->key; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Move forward to next element |
|
| 377 | - * |
|
| 378 | - * @return void |
|
| 379 | - * |
|
| 380 | - * @see http://php.net/manual/en/iterator.next.php Iterator::next |
|
| 381 | - **/ |
|
| 382 | - public function next() |
|
| 383 | - { |
|
| 384 | - $this->key++; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Rewind the iterator to the first element |
|
| 389 | - * |
|
| 390 | - * @return void |
|
| 391 | - * |
|
| 392 | - * @see http://php.net/manual/en/iterator.rewind.php Iterator::rewind |
|
| 393 | - **/ |
|
| 394 | - public function rewind() |
|
| 395 | - { |
|
| 396 | - $this->key = 0; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Checks if current position is valid |
|
| 401 | - * |
|
| 402 | - * @return bool |
|
| 403 | - * |
|
| 404 | - * @see http://php.net/manual/en/iterator.valid.php Iterator::valid |
|
| 405 | - **/ |
|
| 406 | - public function valid() |
|
| 407 | - { |
|
| 408 | - return ($this->offsetExists($this->key)); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /**************************************************************************/ |
|
| 412 | - |
|
| 413 | - /*************************************************************************** |
|
| 348 | + /** |
|
| 349 | + * Return the current element |
|
| 350 | + * |
|
| 351 | + * @return CanvasObject |
|
| 352 | + * |
|
| 353 | + * @see http://php.net/manual/en/iterator.current.php Iterator::current |
|
| 354 | + **/ |
|
| 355 | + public function current() |
|
| 356 | + { |
|
| 357 | + if (!isset($this->data[$this->key])) { |
|
| 358 | + $this->requestPageNumber($this->keyToPageNumber($this->key)); |
|
| 359 | + } |
|
| 360 | + return $this->data[$this->key]; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Return the key of the current element |
|
| 365 | + * |
|
| 366 | + * @return int |
|
| 367 | + * |
|
| 368 | + * @see http://php.net/manual/en/iterator.key.php Iterator::key |
|
| 369 | + **/ |
|
| 370 | + public function key() |
|
| 371 | + { |
|
| 372 | + return $this->key; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Move forward to next element |
|
| 377 | + * |
|
| 378 | + * @return void |
|
| 379 | + * |
|
| 380 | + * @see http://php.net/manual/en/iterator.next.php Iterator::next |
|
| 381 | + **/ |
|
| 382 | + public function next() |
|
| 383 | + { |
|
| 384 | + $this->key++; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Rewind the iterator to the first element |
|
| 389 | + * |
|
| 390 | + * @return void |
|
| 391 | + * |
|
| 392 | + * @see http://php.net/manual/en/iterator.rewind.php Iterator::rewind |
|
| 393 | + **/ |
|
| 394 | + public function rewind() |
|
| 395 | + { |
|
| 396 | + $this->key = 0; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Checks if current position is valid |
|
| 401 | + * |
|
| 402 | + * @return bool |
|
| 403 | + * |
|
| 404 | + * @see http://php.net/manual/en/iterator.valid.php Iterator::valid |
|
| 405 | + **/ |
|
| 406 | + public function valid() |
|
| 407 | + { |
|
| 408 | + return ($this->offsetExists($this->key)); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /**************************************************************************/ |
|
| 412 | + |
|
| 413 | + /*************************************************************************** |
|
| 414 | 414 | * Serializable methods |
| 415 | 415 | */ |
| 416 | 416 | |
| 417 | - /** |
|
| 418 | - * String representation of CanvasArray |
|
| 419 | - * |
|
| 420 | - * @return string |
|
| 421 | - * |
|
| 422 | - * @see http://php.net/manual/en/serializable.serialize.php Serializable::serialize() |
|
| 423 | - **/ |
|
| 424 | - public function serialize() |
|
| 425 | - { |
|
| 426 | - $this->requestAllPages(); |
|
| 427 | - return serialize( |
|
| 428 | - array( |
|
| 429 | - 'page' => $this->page, |
|
| 430 | - 'key' => $this->key, |
|
| 431 | - 'data' => $this->data |
|
| 432 | - ) |
|
| 433 | - ); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Construct a CanvasArray from its string representation |
|
| 438 | - * |
|
| 439 | - * The data in the unserialized CanvasArray is static and cannot be refreshed, |
|
| 440 | - * as the CanvasPest API connection is _not_ serialized to preserve the |
|
| 441 | - * security of API access tokens. |
|
| 442 | - * |
|
| 443 | - * @param string $data |
|
| 444 | - * |
|
| 445 | - * @return string |
|
| 446 | - * |
|
| 447 | - * @see http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize() |
|
| 448 | - **/ |
|
| 449 | - public function unserialize($data) |
|
| 450 | - { |
|
| 451 | - $_data = unserialize($data); |
|
| 452 | - $this->page = $_data['page']; |
|
| 453 | - $this->key = $_data['key']; |
|
| 454 | - $this->data = $_data['data']; |
|
| 455 | - $this->api = null; |
|
| 456 | - $this->endpoint = null; |
|
| 457 | - $this->pagination = array(); |
|
| 458 | - } |
|
| 417 | + /** |
|
| 418 | + * String representation of CanvasArray |
|
| 419 | + * |
|
| 420 | + * @return string |
|
| 421 | + * |
|
| 422 | + * @see http://php.net/manual/en/serializable.serialize.php Serializable::serialize() |
|
| 423 | + **/ |
|
| 424 | + public function serialize() |
|
| 425 | + { |
|
| 426 | + $this->requestAllPages(); |
|
| 427 | + return serialize( |
|
| 428 | + array( |
|
| 429 | + 'page' => $this->page, |
|
| 430 | + 'key' => $this->key, |
|
| 431 | + 'data' => $this->data |
|
| 432 | + ) |
|
| 433 | + ); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Construct a CanvasArray from its string representation |
|
| 438 | + * |
|
| 439 | + * The data in the unserialized CanvasArray is static and cannot be refreshed, |
|
| 440 | + * as the CanvasPest API connection is _not_ serialized to preserve the |
|
| 441 | + * security of API access tokens. |
|
| 442 | + * |
|
| 443 | + * @param string $data |
|
| 444 | + * |
|
| 445 | + * @return string |
|
| 446 | + * |
|
| 447 | + * @see http://php.net/manual/en/serializable.unserialize.php Serializable::unserialize() |
|
| 448 | + **/ |
|
| 449 | + public function unserialize($data) |
|
| 450 | + { |
|
| 451 | + $_data = unserialize($data); |
|
| 452 | + $this->page = $_data['page']; |
|
| 453 | + $this->key = $_data['key']; |
|
| 454 | + $this->data = $_data['data']; |
|
| 455 | + $this->api = null; |
|
| 456 | + $this->endpoint = null; |
|
| 457 | + $this->pagination = array(); |
|
| 458 | + } |
|
| 459 | 459 | } |