aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/SearchResult.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2021-02-04 10:57:44 +0100
committerGitHub <noreply@github.com>2021-02-04 10:57:44 +0100
commit8997ae6c8e24286f7d47981eaf905e80d2481c10 (patch)
tree9906b122998ca4420af68b1bb110033b99f7d8bf /application/bookmark/SearchResult.php
parent11edc143b42a7be09c0c9dc02730c83e8cbb73c2 (diff)
parent9b8c0a4560fa1d87cab1529099b1b4677e92e265 (diff)
downloadShaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.tar.gz
Shaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.tar.zst
Shaarli-8997ae6c8e24286f7d47981eaf905e80d2481c10.zip
Merge pull request #1697 from ArthurHoaro/feature/pagination
Handle pagination through BookmarkService
Diffstat (limited to 'application/bookmark/SearchResult.php')
-rw-r--r--application/bookmark/SearchResult.php136
1 files changed, 136 insertions, 0 deletions
diff --git a/application/bookmark/SearchResult.php b/application/bookmark/SearchResult.php
new file mode 100644
index 00000000..c0bce311
--- /dev/null
+++ b/application/bookmark/SearchResult.php
@@ -0,0 +1,136 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Bookmark;
6
7/**
8 * Read-only class used to represent search result, including pagination.
9 */
10class SearchResult
11{
12 /** @var Bookmark[] List of result bookmarks with pagination applied */
13 protected $bookmarks;
14
15 /** @var int number of Bookmarks found, with pagination applied */
16 protected $resultCount;
17
18 /** @var int total number of result found */
19 protected $totalCount;
20
21 /** @var int pagination: limit number of result bookmarks */
22 protected $limit;
23
24 /** @var int pagination: offset to apply to complete result list */
25 protected $offset;
26
27 public function __construct(array $bookmarks, int $totalCount, int $offset, ?int $limit)
28 {
29 $this->bookmarks = $bookmarks;
30 $this->resultCount = count($bookmarks);
31 $this->totalCount = $totalCount;
32 $this->limit = $limit;
33 $this->offset = $offset;
34 }
35
36 /**
37 * Build a SearchResult from provided full result set and pagination settings.
38 *
39 * @param Bookmark[] $bookmarks Full set of result which will be filtered
40 * @param int $offset Start recording results from $offset
41 * @param int|null $limit End recording results after $limit bookmarks is reached
42 * @param bool $allowOutOfBounds Set to false to display the last page if the offset is out of bound,
43 * return empty result set otherwise (default: false)
44 *
45 * @return SearchResult
46 */
47 public static function getSearchResult(
48 $bookmarks,
49 int $offset = 0,
50 ?int $limit = null,
51 bool $allowOutOfBounds = false
52 ): self {
53 $totalCount = count($bookmarks);
54 if (!$allowOutOfBounds && $offset > $totalCount) {
55 $offset = $limit === null ? 0 : $limit * -1;
56 }
57
58 if ($bookmarks instanceof BookmarkArray) {
59 $buffer = [];
60 foreach ($bookmarks as $key => $value) {
61 $buffer[$key] = $value;
62 }
63 $bookmarks = $buffer;
64 }
65
66 return new static(
67 array_slice($bookmarks, $offset, $limit, true),
68 $totalCount,
69 $offset,
70 $limit
71 );
72 }
73
74 /** @return Bookmark[] List of result bookmarks with pagination applied */
75 public function getBookmarks(): array
76 {
77 return $this->bookmarks;
78 }
79
80 /** @return int number of Bookmarks found, with pagination applied */
81 public function getResultCount(): int
82 {
83 return $this->resultCount;
84 }
85
86 /** @return int total number of result found */
87 public function getTotalCount(): int
88 {
89 return $this->totalCount;
90 }
91
92 /** @return int pagination: limit number of result bookmarks */
93 public function getLimit(): ?int
94 {
95 return $this->limit;
96 }
97
98 /** @return int pagination: offset to apply to complete result list */
99 public function getOffset(): int
100 {
101 return $this->offset;
102 }
103
104 /** @return int Current page of result set in complete results */
105 public function getPage(): int
106 {
107 if (empty($this->limit)) {
108 return $this->offset === 0 ? 1 : 2;
109 }
110 $base = $this->offset >= 0 ? $this->offset : $this->totalCount + $this->offset;
111
112 return (int) ceil($base / $this->limit) + 1;
113 }
114
115 /** @return int Get the # of the last page */
116 public function getLastPage(): int
117 {
118 if (empty($this->limit)) {
119 return $this->offset === 0 ? 1 : 2;
120 }
121
122 return (int) ceil($this->totalCount / $this->limit);
123 }
124
125 /** @return bool Either the current page is the last one or not */
126 public function isLastPage(): bool
127 {
128 return $this->getPage() === $this->getLastPage();
129 }
130
131 /** @return bool Either the current page is the first one or not */
132 public function isFirstPage(): bool
133 {
134 return $this->offset === 0;
135 }
136}