array_map() over the variadic constructor can produce non-sequential or string keys, which doesn't match the array<int, Contribution> property type; re-index with array_values(). Also documents the iterator/count accessors.
32 lines
760 B
PHP
32 lines
760 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace GitContributionGraph\Entity;
|
|
|
|
/**
|
|
* @implements \IteratorAggregate<int, Contribution>
|
|
*/
|
|
final class ContributionCollection implements \IteratorAggregate, \Countable
|
|
{
|
|
/** @var array<int, Contribution> */
|
|
private readonly array $contributions;
|
|
|
|
public function __construct(Contribution ...$contributions)
|
|
{
|
|
$this->contributions = array_values($contributions);
|
|
}
|
|
|
|
/** @return \ArrayIterator<int, Contribution> */
|
|
public function getIterator(): \ArrayIterator
|
|
{
|
|
return new \ArrayIterator($this->contributions);
|
|
}
|
|
|
|
/** Number of contributions held in this collection. */
|
|
public function count(): int
|
|
{
|
|
return count($this->contributions);
|
|
}
|
|
}
|