index/src/Collections/ArrayIterator.php

38 lines
744 B
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
// ArrayIterator.php
// Created: 2022-02-03
// Updated: 2022-02-03
namespace Index\Collections;
use Iterator;
class ArrayIterator implements Iterator {
private array $array;
private bool $wasValid = true;
public function __construct(array $array) {
$this->array = $array;
}
public function current(): mixed {
return current($this->array);
}
public function key(): mixed {
return key($this->array);
}
public function next(): void {
$this->wasValid = next($this->array) !== false;
}
public function rewind(): void {
$this->wasValid = reset($this->array) !== false;
}
public function valid(): bool {
return $this->wasValid;
}
}