Skip to content

Commit 29f9568

Browse files
committed
FIX Make PageinatedList::count() give the number of items in the current page
1 parent 6bf1ae9 commit 29f9568

File tree

5 files changed

+32
-28
lines changed

5 files changed

+32
-28
lines changed

src/Model/List/ListDecorator.php

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

129-
/**
130-
* @return int
131-
*/
132-
public function TotalItems()
129+
public function getTotalItems(): int
133130
{
134131
return $this->list->count();
135132
}

src/Model/List/PaginatedList.php

Lines changed: 23 additions & 20 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,14 +532,6 @@ public function PrevLink()
532532
}
533533
}
534534

535-
/**
536-
* Returns the total number of items in the list
537-
*/
538-
public function TotalItems()
539-
{
540-
return $this->getTotalItems();
541-
}
542-
543535
/**
544536
* Set the request object for this list
545537
*
@@ -557,4 +549,15 @@ public function getRequest()
557549
{
558550
return $this->request;
559551
}
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+
}
560563
}

tests/php/Model/List/GroupedListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,6 @@ public function testTotalItems()
193193
)
194194
);
195195

196-
$this->assertEquals(4, $list->TotalItems());
196+
$this->assertEquals(4, $list->getTotalItems());
197197
}
198198
}

tests/php/Model/List/ListDecoratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function testLimit()
267267
public function testTotalItems()
268268
{
269269
$this->list->expects($this->once())->method('count')->willReturn(5);
270-
$this->assertSame(5, $this->decorator->TotalItems());
270+
$this->assertSame(5, $this->decorator->getTotalItems());
271271
}
272272

273273
public function testAdd()

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)