diff --git a/README.md b/README.md index cfdfec2..b4ebb17 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,8 @@ if ($response->ok()) { } ``` +See [Get the next items of a paged list](#get-the-next-items-of-a-paged-list) + ### Get the followers of the authenticated user ```php @@ -163,6 +165,8 @@ if ($response->ok()) { } ``` +See [Get the next items of a paged list](#get-the-next-items-of-a-paged-list) + ### Get the boards that the authenticated user follows ```php @@ -173,6 +177,8 @@ if ($response->ok()) { } ``` +See [Get the next items of a paged list](#get-the-next-items-of-a-paged-list) + ### Get the users that the authenticated user follows ```php @@ -183,6 +189,8 @@ if ($response->ok()) { } ``` +See [Get the next items of a paged list](#get-the-next-items-of-a-paged-list) + ### Get the interests that the authenticated user follows Example: [Modern architecture](https://www.pinterest.com/explore/901179409185) @@ -195,6 +203,8 @@ if ($response->ok()) { } ``` +See [Get the next items of a paged list](#get-the-next-items-of-a-paged-list) + ### Follow a user ```php @@ -261,6 +271,20 @@ if ($response->ok()) { } ``` +### Get the next items of a paged list + +```php +$hasMoreItems = $pagedList->hasNext(); +if (!$hasMoreItems) { + return; +} +$response = $api->getNextItems($pagedList); +if (!$response->ok()) { + echo $response->getError(); +} +$nextPagedList = $response->result(); +``` + ## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) for details. diff --git a/src/Pinterest/Api.php b/src/Pinterest/Api.php index a400fa8..44cfcd9 100644 --- a/src/Pinterest/Api.php +++ b/src/Pinterest/Api.php @@ -18,6 +18,7 @@ use Pinterest\Http\Request; use Pinterest\Http\Response; use Pinterest\Objects\Board; +use Pinterest\Objects\PagedList; use Pinterest\Objects\Pin; use Pinterest\Objects\User; @@ -501,4 +502,67 @@ public function deletePin($pinId) return $this->execute($request); } + + /** + * Get the next items for a paged list. + * + * @param PagedList $pagedList + * + * @return Response + */ + public function getNextItems(PagedList $pagedList) + { + if (!$pagedList->hasNext()) { + throw new InvalidArgumentException('The list has no more items'); + } + + $items = $pagedList->items(); + + if (empty($items)) { + throw new InvalidArgumentException( + 'Unable to detect object type because the list contains no items' + ); + } + + $item = reset($items); + $objectClassName = get_class($item); + $objectInstance = new $objectClassName(); + + $request = $this->buildRequestForPagedList($pagedList); + + return $this->execute($request, function (Response $response) use ($objectInstance) { + $mapper = new Mapper($objectInstance); + + return $mapper->toList($response); + }); + } + + /** + * Build a request to get the next items of a paged list. + * + * @param PagedList $pagedList + * + * @return Request + */ + private function buildRequestForPagedList(PagedList $pagedList) + { + $nextItemsUri = $pagedList->getNextUrl(); + + if (strpos($nextItemsUri, Authentication::BASE_URI) !== 0) { + throw new InvalidArgumentException( + 'The paged list has an invalid uri' + ); + } + + $params = array(); + $components = parse_url($nextItemsUri); + parse_str($components['query'], $params); + + $path = $components['path']; + $versionPath = '/v1/'; + $versionPathLength = strlen($versionPath); + $path = substr($path, $versionPathLength); + + return new Request('GET', $path, $params); + } } diff --git a/src/Pinterest/Http/Request.php b/src/Pinterest/Http/Request.php index a82ba58..227250d 100644 --- a/src/Pinterest/Http/Request.php +++ b/src/Pinterest/Http/Request.php @@ -70,7 +70,7 @@ public function __construct($method, $endpoint, array $params = array(), array $ /** * Sets the fields. * - * @param array The fields to return. + * @param array $fields The fields to return. * * @return Request The current Request instance. */