path = $path; if (! empty($extension)) { $this->extension = '.' . ltrim($extension, '.'); } if ($flags !== null) { $this->flags = $flags; } } /** * Check whether the given path is a directory and is readable * * @param string $path The path of the directory * * @return bool */ public static function isReadable($path) { return is_dir($path) && is_readable($path); } public function hasChildren(): bool { return static::isReadable($this->current); } public function getChildren(): DirectoryIterator { return new static($this->current, $this->extension, $this->flags); } #[\ReturnTypeWillChange] public function current() { return $this->current; } public function next(): void { $path = null; do { $this->files->next(); $skip = false; if (! $this->files->valid()) { $file = false; $path = false; break; } else { $file = $this->files->current(); do { if ($this->skipHidden && $file[0] === '.') { $skip = true; break; } $path = $this->path . '/' . $file; if (is_dir($path)) { if ($this->flags & static::FILES_FIRST === static::FILES_FIRST) { $this->queue[] = array($path, $file); $skip = true; } break; } if ($this->skipEmpty && ! filesize($path)) { $skip = true; break; } if ($this->extension && ! StringHelper::endsWith($file, $this->extension)) { $skip = true; break; } } while (0); } } while ($skip); /** @noinspection PhpUndefinedVariableInspection */ if ($path === false && ! empty($this->queue)) { list($path, $file) = array_shift($this->queue); } $this->current = $path; $this->key = $file; } #[\ReturnTypeWillChange] public function key() { return $this->key; } public function valid(): bool { return $this->current !== false; } public function rewind(): void { if ($this->files === null) { $files = scandir($this->path); natcasesort($files); $this->files = new ArrayIterator($files); } $this->files->rewind(); $this->queue = array(); $this->next(); } }