Skip to content
This repository has been archived by the owner on Nov 7, 2020. It is now read-only.

Commit

Permalink
Add method to get the next items of a paged list
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Mar 13, 2016
1 parent 811d5ef commit fc4358b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
64 changes: 64 additions & 0 deletions src/Pinterest/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Pinterest/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit fc4358b

Please sign in to comment.