diff --git a/src/Model/List/ListDecorator.php b/src/Model/List/ListDecorator.php index 05ae19e033e..ddf26aa4fc8 100644 --- a/src/Model/List/ListDecorator.php +++ b/src/Model/List/ListDecorator.php @@ -126,21 +126,11 @@ public function last(): mixed return $this->list->last(); } - public function getTotalItems() + public function getTotalItems(): int { return $this->list->count(); } - /** - * @return int - * @depreated 5.4.0 Use getTotalItems() instead. - */ - public function TotalItems() - { - Deprecation::notice('5.4.0', 'Use getTotalItems() instead.'); - return $this->getTotalItems(); - } - public function count(): int { return $this->list->count(); diff --git a/src/Model/List/PaginatedList.php b/src/Model/List/PaginatedList.php index 4eb40434b8f..22598f99ad5 100644 --- a/src/Model/List/PaginatedList.php +++ b/src/Model/List/PaginatedList.php @@ -142,12 +142,20 @@ public function setPageStart($start) return $this; } + /** + * Returns the number of items on the current page of the list. + * For the total number of items in the unpaginated list, use getTotalItems(). + */ + public function count(): int + { + return count($this->getListForCurrentPage()); + } + /** * Returns the total number of items in the unpaginated list. - * - * @return int + * For the number of items on the current page of the list, use count(). */ - public function getTotalItems() + public function getTotalItems(): int { if ($this->totalItems === null) { $this->totalItems = count($this->list ?? []); @@ -214,15 +222,7 @@ public function setLimitItems($limit) public function getIterator(): Traversable { - $pageLength = $this->getPageLength(); - if ($this->limitItems && $pageLength) { - $tmptList = clone $this->list; - return new IteratorIterator( - $tmptList->limit($pageLength, $this->getPageStart()) - ); - } else { - return new IteratorIterator($this->list); - } + return new IteratorIterator($this->getListForCurrentPage()); } /** @@ -532,16 +532,6 @@ public function PrevLink() } } - /** - * Returns the total number of items in the list - * @depreated 5.4.0 Use getTotalItems() instead. - */ - public function TotalItems() - { - Deprecation::notice('5.4.0', 'Use getTotalItems() instead.'); - return $this->getTotalItems(); - } - /** * Set the request object for this list * @@ -559,4 +549,15 @@ public function getRequest() { return $this->request; } + + private function getListForCurrentPage() + { + $pageLength = $this->getPageLength(); + if ($this->limitItems && $pageLength) { + $tmptList = clone $this->list; + return $tmptList->limit($pageLength, $this->getPageStart()); + } else { + return $this->list; + } + } } diff --git a/tests/php/Model/List/PaginatedListTest.php b/tests/php/Model/List/PaginatedListTest.php index 6c6e7743976..7817397311d 100644 --- a/tests/php/Model/List/PaginatedListTest.php +++ b/tests/php/Model/List/PaginatedListTest.php @@ -218,10 +218,14 @@ public function testLimitItems() $list = new PaginatedList($list); $list->setCurrentPage(3); - $this->assertEquals(10, count($list->toArray())); + // toArray uses the iterator while count is a discrete method that doesn't use the iterator. + // They should both give the same results. + $this->assertCount(10, $list->toArray()); + $this->assertEquals(10, $list->count()); $list->setLimitItems(false); - $this->assertEquals(50, count($list->toArray())); + $this->assertCount(50, $list->toArray()); + $this->assertEquals(50, $list->count()); } public function testCurrentPage()