aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/bookmark/SearchResult.php
diff options
context:
space:
mode:
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}