key; } function _current() { return $this->item; } function next() { if ($this->toredown) return; try { $item = $this->_next($key); } catch (EOFException $e) { $item = false; } $this->valid = false; if ($item !== false) { $this->cook($item); $this->item = $item; if ($key !== null) { $this->key = $key; } else { $this->index++; $this->key = $this->index; } $this->valid = true; } else { try { $this->_teardown(); } catch (Exception $e) { } $this->toredown = true; } } function rewind() { if ($this->setup) { if (!$this->toredown) { try { $this->_teardown(); } catch (Exception $e) { } } $this->setup = false; $this->valid = false; $this->toredown = true; $this->index = 0; $this->key = null; $this->item = null; } } function valid() { if (!$this->setup) { try { $this->_setup(); } catch (Exception $e) { } $this->setup = true; $this->toredown = false; $this->beforeIter(); $this->next(); } return $this->valid; } /** * retourner la première valeur de la liste ou $default si aucun élément n'est * trouvé. * * si static::AUTO_REWIND est true, appeler rewind() à la fin pour s'assurer * que l'itérateur est fermé correctement. */ function get($default=null) { # obtenir le premier élément de la liste $this->rewind(); $value = $this->valid()? $this->current(): $default; if (static::AUTO_REWIND) $this->rewind(); return $value; } /** * retourner la première valeur de la liste, ou $default si aucun élément * n'est trouvé. * * si $rewind est true, appeler rewind() à la fin pour s'assurer que * l'itérateur est fermé correctement. * * retourner un tableau [$value, $have_next, $it_nexts] * - $have_next vaut true s'il y a encore des données qui suivent * - si $rewind==false, $it_nexts est un itérateur qui permet d'accéder aux * données suivantes */ function one($default=null, ?bool $rewind=null): array { if ($rewind === null) $rewind = static::AUTO_REWIND; $value = $default; $have_next = false; $it_nexts = new EmptyIterator(); $this->rewind(); if ($this->valid()) { $value = $this->current(); $this->next(); $have_next = $this->valid(); if ($have_next) { $next = $this->current(); $this->next(); if (!$rewind) { $it_nexts = new AppendIterator(); $it_nexts->append(new ArrayIterator([$next])); $it_nexts->append(new NoRewindIterator($this)); } } } if ($rewind) $this->rewind(); return [$value, $have_next, $it_nexts]; } }