Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PaginatedResourceCollection usable without Resource object #131

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Resource/PaginatedResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
class PaginatedResourceCollection extends AbstractResource implements \IteratorAggregate, \Countable
{
/**
* @var string
* @var ?string
*/
private $resourceClass;

/**
* Provide your resource class name if you want that collection return
* those specific resource class rather than an HalResource
*
* @param Hal\HalResource $resource
* @param string $resourceClass
* @param ?string $resourceClass
*/
public function __construct(Hal\HalResource $resource, $resourceClass)
public function __construct(Hal\HalResource $resource, $resourceClass = null)
{
$this->resourceClass = (string) $resourceClass;
if (null !== $resourceClass) {
$this->resourceClass = (string) $resourceClass;
}

parent::__construct($resource, false);
}
Expand Down Expand Up @@ -90,21 +95,20 @@ public function next()
return new static($resource, $this->resourceClass);
}

/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
$data = current($this->resource->getAllResources()) ?: [];

foreach ($data as $item) {
yield new $this->resourceClass($item);
if (null !== $this->resourceClass) {
$item = new $this->resourceClass($item);
}

yield $item;
}
}

/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function count()
{
Expand Down
Loading