From 6c2bc9ed11d0de1363e718328352bf14221e31f8 Mon Sep 17 00:00:00 2001 From: Agung Sugiarto Date: Mon, 28 Sep 2020 17:42:00 +0700 Subject: [PATCH] get() method implement limit and offset --- src/Contracts/RepositoryInterface.php | 4 +++- src/Eloquent/BaseRepository.php | 4 ++-- tests/NewsRepositoryTest.php | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Contracts/RepositoryInterface.php b/src/Contracts/RepositoryInterface.php index b2d5a62..d71301d 100644 --- a/src/Contracts/RepositoryInterface.php +++ b/src/Contracts/RepositoryInterface.php @@ -8,9 +8,11 @@ interface RepositoryInterface * Execute the query as a "select" statement. * * @param array $columns + * @param int $limit + * @param int $offset * @return array */ - public function get(array $columns = ['*']); + public function get(array $columns = ['*'], int $limit = 0, int $offset = 0); /** * Execute the query and get the first result. diff --git a/src/Eloquent/BaseRepository.php b/src/Eloquent/BaseRepository.php index 7f79362..9b9f2d3 100644 --- a/src/Eloquent/BaseRepository.php +++ b/src/Eloquent/BaseRepository.php @@ -10,9 +10,9 @@ abstract class BaseRepository extends RepositoryAbstract implements RepositoryIn /** * @inheritdoc */ - public function get(array $columns = ['*']) + public function get(array $columns = ['*'], int $limit = 0, int $offset = 0) { - $results = $this->entity->select($columns)->findAll(); + $results = $this->entity->select($columns)->findAll($limit, $offset); $this->reset(); diff --git a/tests/NewsRepositoryTest.php b/tests/NewsRepositoryTest.php index 0bc74d6..d6c2c09 100644 --- a/tests/NewsRepositoryTest.php +++ b/tests/NewsRepositoryTest.php @@ -28,6 +28,14 @@ public function testRepositoryGet() $this->assertNotEmpty($this->repository->get()); } + public function testRepositoryGetWithLimitAndOffset() + { + $getLimitOfset = $this->repository->get(['*'], 5, 0); + + $this->assertNotEmpty($getLimitOfset); + $this->assertCount(5, $getLimitOfset); + } + public function testRepositoryFirst() { $this->assertNotEmpty($this->repository->first());