Add all possible links next, prev, first and last
This commit is contained in:
parent
c6ed40c9f8
commit
a0ab7a2578
|
@ -285,7 +285,6 @@ class Sharees {
|
||||||
|
|
||||||
//Pagination
|
//Pagination
|
||||||
$start = ($page - 1) * $perPage;
|
$start = ($page - 1) * $perPage;
|
||||||
$end = $page * $perPage;
|
|
||||||
$total = sizeof($sharees);
|
$total = sizeof($sharees);
|
||||||
|
|
||||||
$sharees = array_slice($sharees, $start, $perPage);
|
$sharees = array_slice($sharees, $start, $perPage);
|
||||||
|
@ -294,18 +293,16 @@ class Sharees {
|
||||||
$response->setTotalItems($total);
|
$response->setTotalItems($total);
|
||||||
$response->setItemsPerPage($perPage);
|
$response->setItemsPerPage($perPage);
|
||||||
|
|
||||||
if ($total > $end) {
|
$links = $this->getPaginationLinks($page, $total, [
|
||||||
$params = [
|
|
||||||
'search' => $search,
|
'search' => $search,
|
||||||
'itemType' => $itemType,
|
'itemType' => $itemType,
|
||||||
'existingShares' => $existingShares,
|
'existingShares' => $existingShares,
|
||||||
'shareType' => $shareTypes,
|
'shareType' => $shareTypes,
|
||||||
'page' => $page + 1,
|
|
||||||
'limit' => $perPage,
|
'limit' => $perPage,
|
||||||
];
|
]);
|
||||||
|
|
||||||
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?' . http_build_query($params);
|
if (!empty($links)) {
|
||||||
$response->addHeader('Link', '<' . $url . '> rel="next"');
|
$response->addHeader('Link', implode(', ', $links));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
|
@ -319,10 +316,37 @@ class Sharees {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function filterSharees($potentialSharees, $existingSharees) {
|
protected function filterSharees($potentialSharees, $existingSharees) {
|
||||||
$sharees = array_map(function ($sharee) use ($existingSharees) {
|
$sharees = array_filter($potentialSharees, function ($sharee) use ($existingSharees) {
|
||||||
return in_array($sharee['value']['shareWith'], $existingSharees) ? null : $sharee;
|
return in_array($sharee['value']['shareWith'], $existingSharees) ? null : $sharee;
|
||||||
}, $potentialSharees);
|
});
|
||||||
|
|
||||||
return array_filter($sharees);
|
return $sharees;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a bunch of pagination links for the current page
|
||||||
|
*
|
||||||
|
* @param int $page Current page
|
||||||
|
* @param int $total Number of total items that need to be paginated
|
||||||
|
* @param array $params Parameters for the URL
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getPaginationLinks($page, $total, array $params) {
|
||||||
|
$url = $this->urlGenerator->getAbsoluteURL('/ocs/v1.php/apps/files_sharing/api/v1/sharees') . '?';
|
||||||
|
|
||||||
|
$links = [];
|
||||||
|
if ($page > 1) {
|
||||||
|
$params['page'] = 1;
|
||||||
|
$links[] = '<' . $url . http_build_query($params) . '>; rel="first"';
|
||||||
|
$params['page'] = $page - 1;
|
||||||
|
$links[] = '<' . $url . http_build_query($params) . '>; rel="prev"';
|
||||||
|
}
|
||||||
|
if ($page * $params['limit'] < $total) {
|
||||||
|
$params['page'] = $page + 1;
|
||||||
|
$links[] = '<' . $url . http_build_query($params) . '>; rel="next"';
|
||||||
|
$params['page'] = ceil($total / $params['limit']);
|
||||||
|
$links[] = '<' . $url . http_build_query($params) . '>; rel="last"';
|
||||||
|
}
|
||||||
|
return $links;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -672,7 +672,7 @@ class ShareesTest extends TestCase {
|
||||||
$headers = $ocs->getHeaders();
|
$headers = $ocs->getHeaders();
|
||||||
$this->assertArrayHasKey('Link', $headers);
|
$this->assertArrayHasKey('Link', $headers);
|
||||||
$this->assertStringStartsWith('<', $headers['Link']);
|
$this->assertStringStartsWith('<', $headers['Link']);
|
||||||
$this->assertStringEndsWith('> rel="next"', $headers['Link']);
|
$this->assertStringEndsWith('"', $headers['Link']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -735,4 +735,52 @@ class ShareesTest extends TestCase {
|
||||||
public function testFilterSharees($potentialSharees, $existingSharees, $expectedSharees) {
|
public function testFilterSharees($potentialSharees, $existingSharees, $expectedSharees) {
|
||||||
$this->assertEquals($expectedSharees, $this->invokePrivate($this->sharees, 'filterSharees', [$potentialSharees, $existingSharees]));
|
$this->assertEquals($expectedSharees, $this->invokePrivate($this->sharees, 'filterSharees', [$potentialSharees, $existingSharees]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dataGetPaginationLinks() {
|
||||||
|
return [
|
||||||
|
[1, 1, ['limit' => 2], []],
|
||||||
|
[1, 3, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=2>; rel="next"',
|
||||||
|
'<?limit=2&page=2>; rel="last"',
|
||||||
|
]],
|
||||||
|
[1, 21, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=2>; rel="next"',
|
||||||
|
'<?limit=2&page=11>; rel="last"',
|
||||||
|
]],
|
||||||
|
[2, 21, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=1>; rel="first"',
|
||||||
|
'<?limit=2&page=1>; rel="prev"',
|
||||||
|
'<?limit=2&page=3>; rel="next"',
|
||||||
|
'<?limit=2&page=11>; rel="last"',
|
||||||
|
]],
|
||||||
|
[5, 21, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=1>; rel="first"',
|
||||||
|
'<?limit=2&page=4>; rel="prev"',
|
||||||
|
'<?limit=2&page=6>; rel="next"',
|
||||||
|
'<?limit=2&page=11>; rel="last"',
|
||||||
|
]],
|
||||||
|
[10, 21, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=1>; rel="first"',
|
||||||
|
'<?limit=2&page=9>; rel="prev"',
|
||||||
|
'<?limit=2&page=11>; rel="next"',
|
||||||
|
'<?limit=2&page=11>; rel="last"',
|
||||||
|
]],
|
||||||
|
[11, 21, ['limit' => 2], [
|
||||||
|
'<?limit=2&page=1>; rel="first"',
|
||||||
|
'<?limit=2&page=10>; rel="prev"',
|
||||||
|
]],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataGetPaginationLinks
|
||||||
|
*
|
||||||
|
* @param int $page
|
||||||
|
* @param int $total
|
||||||
|
* @param array $params
|
||||||
|
* @param array $expected
|
||||||
|
*/
|
||||||
|
public function testGetPaginationLinks($page, $total, $params, $expected) {
|
||||||
|
$this->assertEquals($expected, $this->invokePrivate($this->sharees, 'getPaginationLinks', [$page, $total, $params]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue