Skip to content

Commit 0267e60

Browse files
FIX Make PageinatedList::count() give the number of items in the current page (#11634)
1 parent ae1b7bd commit 0267e60

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

src/Model/List/ListDecorator.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,11 @@ public function last(): mixed
126126
return $this->list->last();
127127
}
128128

129-
public function getTotalItems()
129+
public function getTotalItems(): int
130130
{
131131
return $this->list->count();
132132
}
133133

134-
/**
135-
* @return int
136-
* @depreated 5.4.0 Use getTotalItems() instead.
137-
*/
138-
public function TotalItems()
139-
{
140-
Deprecation::notice('5.4.0', 'Use getTotalItems() instead.');
141-
return $this->getTotalItems();
142-
}
143-
144134
public function count(): int
145135
{
146136
return $this->list->count();

src/Model/List/PaginatedList.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,20 @@ public function setPageStart($start)
142142
return $this;
143143
}
144144

145+
/**
146+
* Returns the number of items on the current page of the list.
147+
* For the total number of items in the unpaginated list, use getTotalItems().
148+
*/
149+
public function count(): int
150+
{
151+
return count($this->getListForCurrentPage());
152+
}
153+
145154
/**
146155
* Returns the total number of items in the unpaginated list.
147-
*
148-
* @return int
156+
* For the number of items on the current page of the list, use count().
149157
*/
150-
public function getTotalItems()
158+
public function getTotalItems(): int
151159
{
152160
if ($this->totalItems === null) {
153161
$this->totalItems = count($this->list ?? []);
@@ -214,15 +222,7 @@ public function setLimitItems($limit)
214222

215223
public function getIterator(): Traversable
216224
{
217-
$pageLength = $this->getPageLength();
218-
if ($this->limitItems && $pageLength) {
219-
$tmptList = clone $this->list;
220-
return new IteratorIterator(
221-
$tmptList->limit($pageLength, $this->getPageStart())
222-
);
223-
} else {
224-
return new IteratorIterator($this->list);
225-
}
225+
return new IteratorIterator($this->getListForCurrentPage());
226226
}
227227

228228
/**
@@ -532,16 +532,6 @@ public function PrevLink()
532532
}
533533
}
534534

535-
/**
536-
* Returns the total number of items in the list
537-
* @depreated 5.4.0 Use getTotalItems() instead.
538-
*/
539-
public function TotalItems()
540-
{
541-
Deprecation::notice('5.4.0', 'Use getTotalItems() instead.');
542-
return $this->getTotalItems();
543-
}
544-
545535
/**
546536
* Set the request object for this list
547537
*
@@ -559,4 +549,15 @@ public function getRequest()
559549
{
560550
return $this->request;
561551
}
552+
553+
private function getListForCurrentPage()
554+
{
555+
$pageLength = $this->getPageLength();
556+
if ($this->limitItems && $pageLength) {
557+
$tmptList = clone $this->list;
558+
return $tmptList->limit($pageLength, $this->getPageStart());
559+
} else {
560+
return $this->list;
561+
}
562+
}
562563
}

tests/php/Model/List/PaginatedListTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,14 @@ public function testLimitItems()
218218
$list = new PaginatedList($list);
219219

220220
$list->setCurrentPage(3);
221-
$this->assertEquals(10, count($list->toArray()));
221+
// toArray uses the iterator while count is a discrete method that doesn't use the iterator.
222+
// They should both give the same results.
223+
$this->assertCount(10, $list->toArray());
224+
$this->assertEquals(10, $list->count());
222225

223226
$list->setLimitItems(false);
224-
$this->assertEquals(50, count($list->toArray()));
227+
$this->assertCount(50, $list->toArray());
228+
$this->assertEquals(50, $list->count());
225229
}
226230

227231
public function testCurrentPage()

0 commit comments

Comments
 (0)