Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/Model/List/ListDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
45 changes: 23 additions & 22 deletions src/Model/List/PaginatedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? []);
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}
}
}
8 changes: 6 additions & 2 deletions tests/php/Model/List/PaginatedListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading