38 lines
744 B
PHP
38 lines
744 B
PHP
|
<?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;
|
||
|
}
|
||
|
}
|