Skip to content

Commit

Permalink
Allows accessing public properties (#22)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
dakota and dakota authored Jul 29, 2020
1 parent 6c31f6f commit fbff8a4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/PaginationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/PaginationResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,23 @@ public function testDebugInfo(array $entities, $records, array $meta): void
], $actual);
}

/**
* @param Entity[] $entities
* @param Entity[]|Traversable<Entity> $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' => [
Expand Down

0 comments on commit fbff8a4

Please sign in to comment.