src/Infraestructure/Iterator/ArrayIterator.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Infraestructure\Iterator;
  3. use Iterator;
  4. class ArrayIterator implements Iterator {
  5. private $array = [];
  6. private $position = 0;
  7. public function __construct($array) {
  8. // Asegúrate de que los índices del array sean numéricos y consecutivos
  9. $this->array = array_values($array);
  10. $this->position = 0;
  11. }
  12. // Restablece el iterador al primer elemento
  13. public function rewind() {
  14. $this->position = 0;
  15. }
  16. // Devuelve el elemento actual del array
  17. public function current() {
  18. return $this->array[$this->position];
  19. }
  20. // Devuelve la clave del elemento actual del array
  21. public function key() {
  22. return $this->position;
  23. }
  24. // Avanza el iterador al siguiente elemento
  25. public function next() {
  26. ++$this->position;
  27. }
  28. // Comprueba si la posición actual es válida
  29. public function valid() {
  30. return isset($this->array[$this->position]);
  31. }
  32. }