Skip to content

Commit 474c127

Browse files
authored
Fix #93: Fix KeysetPaginator to order by field names with underscore (#94)
1 parent fb6eb50 commit 474c127

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/Paginator/KeysetPaginator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
use Yiisoft\Data\Reader\Sort;
1818
use Yiisoft\Data\Reader\SortableDataInterface;
1919

20+
use Yiisoft\Strings\Inflector;
2021
use function array_reverse;
2122
use function count;
2223
use function is_callable;
2324
use function is_object;
2425
use function key;
2526
use function reset;
2627
use function sprintf;
27-
use function ucfirst;
2828

2929
/**
3030
* Keyset paginator.
@@ -306,7 +306,7 @@ private function previousPageExist(ReadableDataInterface $dataReader, Sort $sort
306306
*/
307307
private function getValueFromItem($item, string $field)
308308
{
309-
$methodName = 'get' . ucfirst($field);
309+
$methodName = 'get' . (new Inflector())->toPascalCase($field);
310310

311311
if (is_object($item) && is_callable([$item, $methodName])) {
312312
return $item->$methodName();

tests/Paginator/KeysetPaginatorTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,20 @@ public function testReadObjectsWithPublicProperties(): void
194194
$this->assertTrue($paginator->isOnFirstPage());
195195
}
196196

197-
public function testReadObjectsWithGetters(): void
197+
public function readObjectsWithGettersDataProvider(): array
198198
{
199-
$sort = Sort::only(['id', 'name'])->withOrderString('id');
199+
return [
200+
'order by id field' => ['id', 'getId'],
201+
'order by created_at field' => ['created_at', 'getCreatedAt'],
202+
];
203+
}
204+
205+
/**
206+
* @dataProvider readObjectsWithGettersDataProvider
207+
*/
208+
public function testReadObjectsWithGetters(string $orderByField, string $getter): void
209+
{
210+
$sort = Sort::only(['id', 'name', 'created_at'])->withOrderString($orderByField);
200211
$data = [
201212
$this->createObjectWithGetters(1, 'Codename Boris 1'),
202213
$this->createObjectWithGetters(2, 'Codename Boris 2'),
@@ -207,7 +218,7 @@ public function testReadObjectsWithGetters(): void
207218
$paginator = (new KeysetPaginator($dataReader))->withPageSize(2);
208219

209220
$this->assertSame([$data[0], $data[1]], $this->iterableToArray($paginator->read()));
210-
$this->assertSame((string) $data[1]->getId(), $paginator->getNextPageToken());
221+
$this->assertSame((string) $data[1]->$getter(), $paginator->getNextPageToken());
211222
$this->assertTrue($paginator->isOnFirstPage());
212223
}
213224

@@ -573,11 +584,13 @@ private function createObjectWithGetters(int $id, string $name): object
573584
return new class ($id, $name) {
574585
private int $id;
575586
private string $name;
587+
private int $createdAt;
576588

577-
public function __construct(int $id, string $name)
589+
public function __construct(int $id, string $name, int $createdAt = null)
578590
{
579591
$this->id = $id;
580592
$this->name = $name;
593+
$this->createdAt = $createdAt ?: time();
581594
}
582595

583596
public function getId(): string
@@ -589,6 +602,11 @@ public function getName(): string
589602
{
590603
return $this->name;
591604
}
605+
606+
public function getCreatedAt(): int
607+
{
608+
return $this->createdAt;
609+
}
592610
};
593611
}
594612

0 commit comments

Comments
 (0)