Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\Dataset;
   6  
   7  use Phpml\Exception\DatasetException;
   8  
   9  class FilesDataset extends ArrayDataset
  10  {
  11      public function __construct(string $rootPath)
  12      {
  13          if (!is_dir($rootPath)) {
  14              throw new DatasetException(sprintf('Dataset root folder "%s" missing.', $rootPath));
  15          }
  16  
  17          $this->scanRootPath($rootPath);
  18      }
  19  
  20      private function scanRootPath(string $rootPath): void
  21      {
  22          $dirs = glob($rootPath.DIRECTORY_SEPARATOR.'*', GLOB_ONLYDIR);
  23  
  24          if ($dirs === false) {
  25              throw new DatasetException(sprintf('An error occurred during directory "%s" scan', $rootPath));
  26          }
  27  
  28          foreach ($dirs as $dir) {
  29              $this->scanDir($dir);
  30          }
  31      }
  32  
  33      private function scanDir(string $dir): void
  34      {
  35          $target = basename($dir);
  36  
  37          $files = glob($dir.DIRECTORY_SEPARATOR.'*');
  38          if ($files === false) {
  39              return;
  40          }
  41  
  42          foreach (array_filter($files, 'is_file') as $file) {
  43              $this->samples[] = file_get_contents($file);
  44              $this->targets[] = $target;
  45          }
  46      }
  47  }