<?php
namespace nur;

/**
 * Class shutils: des outils donnant le même confort que le shell
 */
class shutils {
  static function ls_all(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
    return array_filter(scandir($dir, $sorting_order),
      function ($file) use ($pattern) {
        if ($file === "." || $file === "..") return false;
        return $pattern === null || fnmatch($pattern, $file);
      });
  }

  static function ls_dirs(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
    return array_filter(self::ls_all($dir, $pattern, $sorting_order),
      function ($file) use ($dir) {
        return path::is_dir(path::join($dir, $file));
      }
    );
  }

  static function ls_files(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
    return array_filter(self::ls_all($dir, $pattern, $sorting_order),
      function ($file) use ($dir) {
        return path::is_file(path::join($dir, $file));
      }
    );
  }
}
