]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/bookmark/SearchResult.php
c0bce311745a6e6329b75aa9ba93e9bdc4d3fa70
3 declare(strict_types
=1);
5 namespace Shaarli\Bookmark
;
8 * Read-only class used to represent search result, including pagination.
12 /** @var Bookmark[] List of result bookmarks with pagination applied */
15 /** @var int number of Bookmarks found, with pagination applied */
16 protected $resultCount;
18 /** @var int total number of result found */
19 protected $totalCount;
21 /** @var int pagination: limit number of result bookmarks */
24 /** @var int pagination: offset to apply to complete result list */
27 public function __construct(array $bookmarks, int $totalCount, int $offset, ?int $limit)
29 $this->bookmarks
= $bookmarks;
30 $this->resultCount
= count($bookmarks);
31 $this->totalCount
= $totalCount;
32 $this->limit
= $limit;
33 $this->offset
= $offset;
37 * Build a SearchResult from provided full result set and pagination settings.
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)
45 * @return SearchResult
47 public static function getSearchResult(
51 bool $allowOutOfBounds = false
53 $totalCount = count($bookmarks);
54 if (!$allowOutOfBounds && $offset > $totalCount) {
55 $offset = $limit === null ? 0 : $limit * -1;
58 if ($bookmarks instanceof BookmarkArray
) {
60 foreach ($bookmarks as $key => $value) {
61 $buffer[$key] = $value;
67 array_slice($bookmarks, $offset, $limit, true),
74 /** @return Bookmark[] List of result bookmarks with pagination applied */
75 public function getBookmarks(): array
77 return $this->bookmarks
;
80 /** @return int number of Bookmarks found, with pagination applied */
81 public function getResultCount(): int
83 return $this->resultCount
;
86 /** @return int total number of result found */
87 public function getTotalCount(): int
89 return $this->totalCount
;
92 /** @return int pagination: limit number of result bookmarks */
93 public function getLimit(): ?int
98 /** @return int pagination: offset to apply to complete result list */
99 public function getOffset(): int
101 return $this->offset
;
104 /** @return int Current page of result set in complete results */
105 public function getPage(): int
107 if (empty($this->limit
)) {
108 return $this->offset
=== 0 ? 1 : 2;
110 $base = $this->offset
>= 0 ? $this->offset
: $this->totalCount +
$this->offset
;
112 return (int) ceil($base / $this->limit
) +
1;
115 /** @return int Get the # of the last page */
116 public function getLastPage(): int
118 if (empty($this->limit
)) {
119 return $this->offset
=== 0 ? 1 : 2;
122 return (int) ceil($this->totalCount
/ $this->limit
);
125 /** @return bool Either the current page is the last one or not */
126 public function isLastPage(): bool
128 return $this->getPage() === $this->getLastPage();
131 /** @return bool Either the current page is the first one or not */
132 public function isFirstPage(): bool
134 return $this->offset
=== 0;