Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  }