Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace Phpml\Dataset;
   6  
   7  use Phpml\Exception\FileException;
   8  
   9  class CsvDataset extends ArrayDataset
  10  {
  11      /**
  12       * @var array
  13       */
  14      protected $columnNames = [];
  15  
  16      /**
  17       * @throws FileException
  18       */
  19      public function __construct(string $filepath, int $features, bool $headingRow = true, string $delimiter = ',', int $maxLineLength = 0)
  20      {
  21          if (!file_exists($filepath)) {
  22              throw new FileException(sprintf('File "%s" missing.', basename($filepath)));
  23          }
  24  
  25          $handle = fopen($filepath, 'rb');
  26          if ($handle === false) {
  27              throw new FileException(sprintf('File "%s" can\'t be open.', basename($filepath)));
  28          }
  29  
  30          if ($headingRow) {
  31              $data = fgetcsv($handle, $maxLineLength, $delimiter);
  32              $this->columnNames = array_slice((array) $data, 0, $features);
  33          } else {
  34              $this->columnNames = range(0, $features - 1);
  35          }
  36  
  37          $samples = $targets = [];
  38          while (($data = fgetcsv($handle, $maxLineLength, $delimiter)) !== false) {
  39              $samples[] = array_slice((array) $data, 0, $features);
  40              $targets[] = $data[$features];
  41          }
  42  
  43          fclose($handle);
  44  
  45          parent::__construct($samples, $targets);
  46      }
  47  
  48      public function getColumnNames(): array
  49      {
  50          return $this->columnNames;
  51      }
  52  }