CodeUnitCollectionIterator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/code-unit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\CodeUnit;
  11. use Iterator;
  12. final class CodeUnitCollectionIterator implements Iterator
  13. {
  14. /**
  15. * @psalm-var list<CodeUnit>
  16. */
  17. private $codeUnits;
  18. /**
  19. * @var int
  20. */
  21. private $position = 0;
  22. public function __construct(CodeUnitCollection $collection)
  23. {
  24. $this->codeUnits = $collection->asArray();
  25. }
  26. public function rewind(): void
  27. {
  28. $this->position = 0;
  29. }
  30. public function valid(): bool
  31. {
  32. return isset($this->codeUnits[$this->position]);
  33. }
  34. public function key(): int
  35. {
  36. return $this->position;
  37. }
  38. public function current(): CodeUnit
  39. {
  40. return $this->codeUnits[$this->position];
  41. }
  42. public function next(): void
  43. {
  44. $this->position++;
  45. }
  46. }