From fbff8a4239a2868180b846a98519da5cc9caf01f Mon Sep 17 00:00:00 2001 From: Walther Lalk <83255+dakota@users.noreply.github.com> Date: Wed, 29 Jul 2020 15:13:13 +0200 Subject: [PATCH] Allows accessing public properties (#22) * Allows accessing public properties This allows accessing public properties of the underlying `Lampager\PaginationResult` object (For example, `hasNext`, `nextCursor`, etc.) * Add typehint * Add test for public properties Co-authored-by: Walther Lalk --- src/PaginationResult.php | 20 ++++++++++++++++++++ tests/TestCase/PaginationResultTest.php | 17 +++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/PaginationResult.php b/src/PaginationResult.php index 08d14ad..cdeaff0 100644 --- a/src/PaginationResult.php +++ b/src/PaginationResult.php @@ -143,6 +143,26 @@ protected function getIterator(): Iterator return $iterator; } + /** + * @param string $name The name of the parameter to fetch + * + * @return mixed + */ + public function __get(string $name) + { + if (property_exists($this->result, $name)) { + return $this->result->{$name}; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], + E_USER_NOTICE + ); + + return null; + } + /** * Returns an array that can be used to describe the internal state of this * object. diff --git a/tests/TestCase/PaginationResultTest.php b/tests/TestCase/PaginationResultTest.php index 99eeca5..4c2c6d4 100644 --- a/tests/TestCase/PaginationResultTest.php +++ b/tests/TestCase/PaginationResultTest.php @@ -191,6 +191,23 @@ public function testDebugInfo(array $entities, $records, array $meta): void ], $actual); } + /** + * @param Entity[] $entities + * @param Entity[]|Traversable $records + * @param mixed[] $meta + * @dataProvider arrayProvider + * @dataProvider iteratorAggregateProvider + */ + public function testPublicProperties(array $entities, $records, array $meta): void + { + $paginationResult = new PaginationResult($records, $meta); + + $this->assertEquals($meta['hasPrevious'], $paginationResult->hasPrevious); + $this->assertEquals($meta['previousCursor'], $paginationResult->previousCursor); + $this->assertEquals($meta['hasNext'], $paginationResult->hasNext); + $this->assertEquals($meta['nextCursor'], $paginationResult->nextCursor); + } + public function arrayProvider(): Generator { yield 'Array iteration' => [